使用 Java 通过 ssh 运行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/570450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:20:30  来源:igfitidea点击:

Running commands over ssh with Java

javasshopenssh

提问by Ichorus

Scenerio: I'd like to run commands on remote machines from a Java program over ssh (I am using OpenSSH on my development machine). I'd also like to make the ssh connection by passing the password rather than setting up keys as I would with 'expect'.
Problem: When trying to do the 'expect' like password login the Process that is created with ProcessBuilder cannot seem to see the password prompt. When running regular non-ssh commands (e.g 'ls') I can get the streams and interact with them just fine. I am combining standard error and standard out into one stream with redirectErrorStream(true);so I am not missing it in standard error...When I run ssh with the '-v' option, I see all of the logging in the stream but I do not see the prompt. This is my first time trying to use ProcessBuilder for something like this. I know it would be easier to use Python, Perl or good ol' expect but my boss wants to utilize what we are trying to get back (remote log files and running scripts) within an existing Java program so I am kind of stuck.

Scenerio:我想通过 ssh 从 Java 程序在远程机器上运行命令(我在我的开发机器上使用 OpenSSH)。我还想通过传递密码来建立 ssh 连接,而不是像“期望”那样设置密钥。
问题:当尝试执行像密码登录这样的“期望”操作时,使用 ProcessBuilder 创建的进程似乎看不到密码提示。当运行常规的非 ssh 命令(例如“ls”)时,我可以获取流并与它们进行很好的交互。我将标准错误和标准输出合并为一个流redirectErrorStream(true);所以我没有在标准错误中遗漏它......当我使用“-v”选项运行 ssh 时,我看到了流中的所有日志记录,但我没有看到提示。这是我第一次尝试使用 ProcessBuilder 来做这样的事情。我知道使用 Python、Perl 或好的 ol'expect 会更容易,但我的老板想在现有的 Java 程序中利用我们试图恢复的内容(远程日志文件和运行脚本),所以我有点卡住了。

Thanks in advance for the help!

在此先感谢您的帮助!

采纳答案by Jason Cohen

The prompt might only be shown when sshis connected to a TTY, which it isn't in the case of Java.

提示可能仅在ssh连接到 TTY 时显示,而在 Java 中则不然。

There's probably a way to supply the password on the command-line in your sshapplication. That will be the way to get past the prompt.

可能有一种方法可以在ssh应用程序的命令行上提供密码。这将是绕过提示的方法。

Alternately, consider connecting directly to the host server from native Java code rather than running an external application. There's a million librariesthat will do this.

或者,考虑从本机 Java 代码直接连接到主机服务器,而不是运行外部应用程序。有一百万个图书馆会这样做。

回答by Ryan Graham

Most security minded programs don't use stdin/stdout for capturing passwords, they capture the TTY or some equivalent method.

大多数具有安全意识的程序不使用 stdin/stdout 来捕获密码,而是捕获 TTY 或某种等效方法。

回答by Douglas Leeder

Rather than using an external ssh program, why not use a Java ssh library:

与其使用外部 ssh 程序,不如使用 Java ssh 库:

Are two I found with google - that'll avoid the problem that openssh will be working very hard to prevent entering the password on stdin - it'll be opening the terminal directly. expect has to work very hard to simulate a tty in order to work.

我在谷歌上找到了两个 - 这将避免 openssh 将非常努力地防止在 stdin 上输入密码的问题 - 它会直接打开终端。expect 必须非常努力地模拟 tty 才能工作。

回答by Jason Day

Why not use a Java ssh client? Thisone is BSD-licensed, and there are more clients listed here.

为什么不使用 Java ssh 客户端?这个是 BSD 许可的,这里列出更多的客户端。

回答by Jason Day

I've used the trilead ssh client with a lot of success. I have also tried jsch (another java ssh client) and tunneling to the native ssh client under linux. Trilead was by far the best.

我已经成功地使用了 trilead ssh 客户端。我还尝试过 jsch(另一个 java ssh 客户端)并在 linux 下通过隧道连接到本机 ssh 客户端。Trilead 是迄今为止最好的。

回答by Bruce Blackshaw

You can run commands using edtFTPj/PRO, as well as performing file transfers via SFTP. It's Java.

您可以使用edtFTPj/PRO运行命令,也可以通过 SFTP 执行文件传输。是爪哇。

回答by mshafrir

Take a look at the very recently released SSHD, which is based on the Apache MINA project.

看看最近发布的SSHD,它基于 Apache MINA 项目。

回答by shikhar

plug: see thisexample of executing a command over ssh using sshj

插件:请参阅使用sshj通过 ssh 执行命令的示例

回答by rogodeter

Echoing others' suggestion to use a Java SSH library. But wanted to comment on Cohen's response. Sending your password over the command line when establishing the connection is insecure and also not permitted by many sshd servers (based on configuration).

回应他人使用 Java SSH 库的建议。但想对科恩的回应发表评论。在建立连接时通过命令行发送密码是不安全的,而且许多 sshd 服务器也不允许(基于配置)。

You might want to look into setting up keys for this, so you can perform ssh commands between the machines without a password.

您可能需要为此设置密钥,以便无需密码即可在机器之间执行 ssh 命令。

Basic steps- use openssh to create a keypair (I've done RSA but I know there's a better method now) - create a .ssh directory in your home folder on the SOURCE machine - create a .ssh directory in your home folder on the TARGET machine - keep your private key in your source machine's .ssh folder - copy your public key into a file called authorized_keys in the target's .ssh folder

基本步骤- 使用 openssh 创建密钥对(我已经完成了 RSA,但我知道现在有更好的方法) - 在 SOURCE 机器的主文件夹中创建一个 .ssh 目录 - 在你的主文件夹中创建一个 .ssh 目录目标机器 - 将您的私钥保存在源机器的 .ssh 文件夹中 - 将您的公钥复制到目标机器的 .ssh 文件夹中名为 authorized_keys 的文件中

Some instructions can be found here

一些说明可以在这里找到