java模拟linux命令scp

要导入包ganymed-ssh2-build210.jar

package testScp;
import java.io.IOException;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;

public class Scp {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String user = "root";
        String pass = "`12345678qwer";
        String host = "192.168.2.2";

        Connection con = new Connection(host);
        try {
            con.connect();
            boolean isAuthed = con.authenticateWithPassword(user, pass);
            System.out.println("isAuthed===="+isAuthed);
            SCPClient scpClient = con.createSCPClient();
            scpClient.put("F:\\path.txt", "/root/"); //从本地复制文件到远程目录
            System.out.println("成功传输!");
 }catch (IOException e) {
        e.printStackTrace();
    }
        con.close();
 }
 }

你可能感兴趣的:(java)