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:
You can create a key pair with
ssh-keygen
without a pass-phrase. UseauthPublickey(java.lang.String username, java.lang.String... locations)
to select the key for the connection.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
.
精彩评论