【问题标题】:How to run SSH commands on remote system through java program (followup)如何通过java程序在远程系统上运行SSH命令(后续)
【发布时间】:2013-02-15 14:30:44
【问题描述】:

我正在尝试通过 Java 程序在远程主机中运行命令/可执行文件。这是我从相关线程中获得的代码。我可以连接到远程主机并通过 BufferedReader in 读取流。但是,我无法使用 out.println("ls /home/ubuntu"); 发送/执行命令。你有什么建议?我正在使用 MAC OS 系统。

Process p = Runtime.exec("ssh myhost");
PrintStream out = new PrintStream(p.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream());
Thread.sleep(3000);
while (in.ready()) {
   String s = in.readLine();
   System.out.println(s);
}

out.println("ls /home/ubuntu");
Thread.sleep(3000);
while (in.ready()) {
    String s = in.readLine();
    System.out.println(s);
}
out.println("exit");

【问题讨论】:

    标签: java command-line ssh command


    【解决方案1】:

    让它工作。

    PrintStream out = new PrintStream(p.getOutputStream());
    

    对我不起作用,所以我将其更改为这个。

    PrintWriter out = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(p.getOutputStream())), true);
    

    【讨论】:

      【解决方案2】:

      我使用了一个库 JSch,但我想 sshxcute 在我浏览文档时看起来更好。

      【讨论】:

      • 谢谢,我也去看看。
      猜你喜欢
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      相关资源
      最近更新 更多