简体   繁体   中英

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

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

and Im executing commands on the box by Printwriter as follows --

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

Any pointers on the above would be of great help.

Regards p1nG

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

You can use SSH-2 for java , which seems to be actively maintained.

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.

You can setup password-less SSH authentication. Here is a step by step guide for setting up the same. 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?" 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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