简体   繁体   中英

Java exec or runtime not working with ssh

I'm trying to ssh into ubuntu using exec, but for some reason when I execute from the code I get the error port 22: Connection refused

In the code I use concat to put the strings together, but I know they're put together properly because I print them out and if I copy and paste them into the command line then it will ssh properly. My code tries:

p1= Runtime.getRuntime().exec(run1);
p1.waitfor();

where

run1 = "ssh -o StrictHostKeyChecking=no -v -i key " + "ubuntu@"+ DNS + " sudo mke2fs -F -j "+device;

Any ideas?

You are initiating the connection, so for it to be refused it means that the machine you are attempting to ssh into is denying you ssh access.

Log into that machine by whatever means you have and verify that the ssh server is running. If it is, then verify that the firewall is not blocking port 22; because, sometimes the ssh server is running but the firewall won't allow network access to the ssh server due to a port blocking rule.

--- Edited after question in comments ---

Is there a difference between ssh in the command line and using exec? Because I can 
connect to the server through the command line, which I assume is still using port 22. 
So if I can ssh that way does that mean port 22 is working?

There are a few possibilities. Java comes with a Security Manager which only serves to deny programs access to machine resources. This is why it is possible to safely run applet code, which is downloaded from remote servers, as the Security Manager denies permission to access the hard drive or make connections to other machines. In the applet sandbox, it does allow connections back to the originating web server (to download more code and images).

However, the lack of a security exception directs the suspicion away from the Security Manager. The fact that the message uses the words "Connection refused" is a strong indicator that the SSH server you are connecting to won't accept a connection from you.

Perhaps by operating on the command line, the ssh command is using a different environment or configuration. I would see if the command is aliased, of if the ssh connection makes some assumption about key pairs. If nothing seems to be out of line with the command, I would check that your program is connection with ssh version 2 (version 1 is not allowed by many due to a security hole).

Then I would also hunt around for possible differences in name resolution. You might be resolving the hostname in the command line differently than you are resolving it from the Java program. This could mean that the Java program is attempting connection to a different machine, one which doesn't have a secure shell server running.

Either way, it seems that you'll have to do a bit of debugging to isolate if it is a true coding problem or an environmental issue.

If you are getting Connection refused, the SSHD is not running or you are being blocked by Firewall (or similiar).

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