简体   繁体   English

如何使用Java SSH到没有密码的主机上

[英]how to ssh onto a host without password using java

Im currently using a java application to run commands on a unix box by invoking an instance of the bash as follows -- 我目前正在通过调用bash的实例使用Java应用程序在Unix框上运行命令-

proc = Runtime.getRuntime().exec("/bin/bash", null, wd);

and Im executing commands on the box by Printwriter as follows -- 我通过Printwriter在盒子上执行命令如下-

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);

Now, as a requirement I need to ssh onto another host, invoke an instance of that host's bash and run some commands. 现在,根据需要,我需要SSH到另一台主机上,调用该主机的bash实例并运行一些命令。 If this can be done without having to enter the password, it would be great. 如果无需输入密码即可完成此操作,那就太好了。 If not, how do I make the application to enter the 'password' at the right time? 如果没有,如何使应用程序在正确的时间输入“密码”? Reading the output of the shh command and based on that, writing out the password does not seem appealing. 读取shh命令的输出并基于此,写出密码似乎并不吸引人。

Any pointers on the above would be of great help. 上面的任何指示都将有很大帮助。

Regards p1nG 关于p1nG

使用用Java编写的ssh库,而不是在运行时执行ssh程序。

You can use SSH-2 for java , which seems to be actively maintained. 您可以将SSH-2用于java ,它似乎已经得到了积极维护。

Alternatively, set up a pubic/private key for password-less authentication , and store the private key in the ~/.ssh folder of the user running the java program. 或者,为无密码身份验证设置公共/私钥,并将私钥存储在运行Java程序的用户的〜/ .ssh文件夹中。

You can setup password-less SSH authentication. 您可以设置无密码的SSH身份验证。 Here is a step by step guide for setting up the same. 这是设置指南的分步指南。 http://manjeetdahiya.com/2011/03/03/passwordless-ssh-login/ http://manjeetdahiya.com/2011/03/03/passwordless-ssh-login/

I can't imagine how you're going to avoid supplying a password. 我无法想象您将如何避免提供密码。 If you could log in to a system without supplying a password just by somehow telling it that supplying a password would be inconvenient for you, this would rather destroy the value of any security. 如果仅通过某种方式告诉系统提供密码对您来说不方便而无需提供密码就可以登录系统,那么这将破坏任何安全性的价值。 Security Guard: "Do you have an ID that allows you access to this building?" 安全卫士:“您是否具有允许您访问此建筑物的ID?” Visitor: "Yes officer, but it's too much trouble to get it out of my wallet." 访客:“是的,但是要从我的钱包里拿出来太麻烦了。” Guard: "Oh, okay then, go rght ahead in." 守卫:“哦,那好吧,继续前进。”

Rather than exec'ing, you probably want to establish a socket connection and pass messages back and forth cleanly. 除了执行之外,您可能想要建立套接字连接并干净地来回传递消息。 Or as others have noted, use a library that does the grunt work for you. 或者,正如其他人所指出的,请使用为您服务的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM