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 connecti
You can run commands using edtFTPj/PRO, as well as performing file transfers via SFTP. It's Java.
Why not use a Java ssh client? This one is BSD-licensed, and there are more clients listed here.
Most security minded programs don't use stdin/stdout for capturing passwords, they capture the TTY or some equivalent method.
Rather than using an external ssh program, why not use a Java ssh library:
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.
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.
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