Running commands over ssh with Java

2019-01-14 08:44发布

问题:

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!

回答1:

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.



回答2:

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

  • Trilead
  • JTA

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.



回答3:

plug: see this example of executing a command over ssh using sshj



回答4:

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



回答5:

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



回答6:

I've used the trilead ssh client with a lot of success. I have also tried jsch (another java ssh client) and tunneling to the native ssh client under linux. Trilead was by far the best.



回答7:

Take a look at the very recently released SSHD, which is based on the Apache MINA project.



回答8:

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



回答9:

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



标签: java ssh openssh