如何在远程系统上使用Java运行SSH命令? [英] How do I run SSH commands on remote system using Java?

查看:668
本文介绍了如何在远程系统上使用Java运行SSH命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这种Java应用程序的新手,并寻找一些关于如何使用SSH连接到远程服务器,执行命令以及使用Java作为编程语言返回输出的示例代码。

I am new to this kind of Java application and looking for some sample code on how to connect to a remote server using SSH , execute commands, and get output back using Java as programming language.

推荐答案

查看Runtime.exec()Javadoc

Have a look at Runtime.exec() Javadoc

Process p = Runtime.getRuntime().exec("ssh myhost");
PrintStream out = new PrintStream(p.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));

out.println("ls -l /home/me");
while (in.ready()) {
  String s = in.readLine();
  System.out.println(s);
}
out.println("exit");

p.waitFor();

这篇关于如何在远程系统上使用Java运行SSH命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆