开发者

Send ssh command from Java code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

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 question

can 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/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜