通过java运行时和cygwin发送和接收多个ssh命令
嘿,当我尝试用 java 构建一个在远程 Linux 服务器上执行命令并返回输出进行处理的程序时,我遇到了以下问题...
基本上我已经安装了带有 SSH 客户端的 Cygwin 并想要执行以下操作
: Cygwin,
发送命令“user@ip”;
返回输出;
发送命令“密码”;
返回输出;
发送多个其他命令,
返回输出;
...等等...
到目前为止:
Process proc = Runtime.getRuntime().exec("C:/Power Apps/Cygwin/Cygwin.bat");
效果很好,只是我不知道如何尝试下一步。
有什么帮助吗?
Hey I have run into the following problem when attempting to build a program in java which executes commands on a remote linux server and returns the output for processing...
Basically I have installed Cygwin with an SSH client and want to do the following:
Open Cygwin,
Send command "user@ip";
Return output;
Send command "password";
Return output;
Send multiple other commands,
Return output;
...etc...
So far:
Process proc = Runtime.getRuntime().exec("C:/Power Apps/Cygwin/Cygwin.bat");
Works nicely except I am at a loss as to how to attempt the next steps.
Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以使用 Plink:
在此处下载
有一套很好的说明链接在这里
您可以使用如下命令:
plink root@myserver -pw passw /etc/backups/do-backup.sh
You could also use Plink:
Download here
There is a good set of instructions link here
You can use a command like:
plink root@myserver -pw passw /etc/backups/do-backup.sh
在java中使用ssh实现。几年前我使用过 Ganymede,现在也许有更好的选择。 (?)
使用 Ganymede,您将获得一个要读取的输入流和一个要写入的输出流。
您可以在输入流上创建 LineInputReader 并使用它来读取表示远程服务器输出的字符串。然后使用正则表达式模式/匹配器来解析响应。
在输出流上创建一个 PrintWriter 并使用 println() 发送命令。
它简单而且实际上非常强大(如果你知道正则表达式......它可能需要一些尝试和错误才能得到它的正确......)
Use a ssh implementation in java. I used Ganymede a couple of years ago, there are perhaps better alternatives now. (?)
Using Ganymede, you will get an input stream to read from, and an output stream to write to.
You can create a LineInputReader on the input stream and use that to read Strings representing the output from the remote server. Then use a regexp Pattern/Matcher to parse responses.
Create a PrintWriter on the output stream and use println() to send your commands.
Its simple and actually quite powerful (if you know regexp... It might require some trial and error to get it right...)
快速方法:不要通过 cygwin。将您的登录信息和命令作为参数传递给 ssh。
更好的方法:安装并使用开源且非常成熟的Sun Grid Engine并使用其DRMAA 绑定 Java 来执行您的命令。您也可以考虑切换到脚本语言(您的任务非常像脚本)。如果您DRMAA有Perl、Ruby 和 < a href="http://www.drmaa.org/implementations.php" rel="nofollow noreferrer">其他绑定。
The quick way: Don't go through cygwin. Pass your login info and commands as arguments to ssh.
A better way: Install and use the open source and very mature Sun Grid Engine and use its DRMAA binding for Java to exec your commands. You might also consider switching to a scripting language (yours is a very script like task). If you do DRMAA has Perl, Ruby and other bindings as well.