使用 Java 通过 ssh 运行命令

发布于 2024-07-13 18:51:25 字数 485 浏览 5 评论 0原文

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

先谢谢您的帮助!

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.

Thanks in advance for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

乖乖哒 2024-07-20 18:51:25

仅当 ssh 连接到 TTY 时才会显示该提示,而 Java 则不会。

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

或者,考虑从本机 Java 代码直接连接到主机服务器,而不是运行外部应用程序。 一百万个库可以做到这一点。

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

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

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

二货你真萌 2024-07-20 18:51:25

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

是我通过谷歌找到的两个 - 这将避免 openssh 将非常努力地阻止输入密码的问题在标准输入上 - 它将直接打开终端。 Expect 必须非常努力地模拟 tty 才能工作。

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

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.

葮薆情 2024-07-20 18:51:25

为什么不使用 Java ssh 客户端? 这个是 BSD 许可的,并且列出了更多客户端 此处

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

别想她 2024-07-20 18:51:25

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

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

晒暮凉 2024-07-20 18:51:25

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

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

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

可以在此处

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).

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

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

Some instructions can be found here

月竹挽风 2024-07-20 18:51:25

您可以使用 edtFTPj/PRO 运行命令,以及通过以下方式执行文件传输SFTP。 这是Java。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文