Send ssh command from Java code [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this questioncan anybody show moe how to send from java ssh command ( example ssh root@192.168.0.2 "ls" ) ? What 开发者_如何学JAVAclass do I need ?
Using sshj:
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("nameOfServer");
ssh.authPublickey("userId");
Session session = ssh.startSession();
Command cmd = session.exec("yourCommand");
System.out.println(cmd.getOutputAsString());
session.close();
ssh.disconnect();
You can use JSch or any other Java library. Google will help you.
Although, usually I find it more convenient to execute ssh commands from build script. E.g., there's an Ant task for that.
an other lib we use is http://www.ganymed.ethz.ch/ssh2/
精彩评论