开发者

SCP upload file problem

I need to upload file with SCP. I found on stackoverflow this library http://code.google.com/p/commons-net-ssh/ I download .jar and add to BuildPath, but how to add username and password to this ? I tried this but it doesn't wor开发者_开发问答k. If you have any other library that do this it will be fine . Any help ?

SSHClient ssh = new SSHClient();
//ssh.useCompression(); 
ssh.loadKnownHosts();
ssh.connect("localhost");
try {
    ssh.authPublickey(System.getProperty("user.name"));
    new SCPDownloadClient(ssh).copy("ten", "/tmp");
} finally {
    ssh.disconnect();
}


There are two solutions:

  1. You can create a key pair with ssh-keygen without a pass-phrase. Use authPublickey(java.lang.String username, java.lang.String... locations) to select the key for the connection.

  2. You can save the password in a file which the Java code can read.

The security of the two solutions is about the same: It depends entirely on who can read the password/key file. Make it readable only by the user who runs the Java code.


You can use the authPassword method to authenticate.


SCPClient scp = conn.createSCPClient();
scp.put(localFileLoc,zipFileName,""/temp,"0777");

This helps me to use SCP. I have used ganymed-ssh2-build251beta1.jar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜