I need to use sshpass to launch a remote command through SSH from a Java code.
If I manually type in a console:
ssh -p 22 user@ipaddress mplayer '/media/data/myFavouriteSong.mp3'works perfectly, but asks for password. So I tried running sshpass:
sshpass -p mypass ssh -p 22 user@ipaddress mplayer '/media/data/myFavouriteSong.mp3'
sshpass -p mypass ssh -l user@ipaddress mplayer '/media/data/myFavouriteSong.mp3'
sshpass -p mypass ssh -t user@ipaddress mplayer '/media/data/myFavouriteSong.mp3'
sshpass -p mypass ssh user@ipaddress echo 'OK'and none of them work.
710 Answers
This may be caused by the host-key checks done by ssh. It looks like sshpass keeps silent on invalid host keys (no output on neither stderr nor stdout) and exists with status-code 6. At the time of this writing, this was revision 50, and the matching constant in the code is RETURN_HOST_KEY_UNKNOWN, which hints to that error.
Your error-code may differ and looking at the code linked above may give you some insight.
If your issue is an invalid host-key you should think twice about overriding the error with a CLI option. Your machine could be compromised or you may be subject to a MITM attack! If you are 100% certain that this is not the case and if you have no means to keep the verified host-keys up-to date, you can use a command like this:
sshpass -pfoobar ssh -o StrictHostKeyChecking=no user@host command_to_run 11 sshpass command works only if the password prompt ends in assword:.
If your password prompt is different you may have to adjust the source code of sshpass to recognize the prompt you have.
I have seen password prompts like Password for user@host: on some systems, maybe this is your problem.
I think you want something like:
sshpass -p yourpassword ssh user@ipaddress somecommandFor instance, this works for me:
sshpass -p mypassword ssh username@10.0.0.9 touch foo 1 sshpass syntax is
sshpass [-ffilename|-dnum|-ppassword|-e] [options] command argumentsNote that there is no space between the -p and the password.
Also I've noticed that you have to connect with ssh at least once manually to obtain the RSA key of the machine you are connecting to, to go into the ~/.ssh/known_hosts file before sshpass will allow you to connect.
so after obtaining the entry in the known_hosts file I can run a command such as
sshpass -ffilename_with_password_in_it ssh user@server uname -aand it will execute the command uname -a on the remote server and output the results to the standard out on the local machine.
There is lots of way to do that, here is my suggested solution:
First of all, storing your password in a variable is give you more flexible commands. sshpass has an option for that:
-e: this option allows to read password from $SSHPASS environment variable
You have two way to set this variable:
Set directly in your code:
export SSHPASS='your_pass'Or ask for it:
readPassword () { echo Ssh Password: read -s SSHPASS eval "export SSHPASS='""$SSHPASS""'" } # read password readPassword
Than execute your command;
If your command is static, run it directly:
sshpass -e ssh user@host << EOF mplayer '/media/data/myFavouriteSong.mp3' command2 parameter1 parameter2 parameter3 ... command3 parameter1 parameter2 parameter3 ... ... EOFIf your command will be dynamic use like this:
DYN_FILE_LOC='/media/data/myFavouriteSong.mp3' eval 'sshpass -e ssh user@host << EOF mplayer "$DYN_FILE_LOC" command2 parameter1 parameter2 parameter3 ... command3 parameter1 parameter2 parameter3 ... ... EOF'
hope this helps someone.
4make a key
ssh-keygen -t rsamake a .ssh folder on the remote server it will ask for a password
ssh $USER@SEVERNAME mkdir -p .sshcopy the key to the remote server
cat .ssh/id_rsa.pub | ssh $USER@SERVER 'cat >> .ssh/authorized_keys'
Check if it asks for a password, if not it should work.
ssh $USER@SEVERNAME 'command' sshpass uses pseudo terminals and the man page includes apologies for breaking occasionally. You can also try fd0ssh. It works if you do not need to send stdin to a process on the remote machine. That works if you just issue a command and capture the result.
What you need is -f -t -t -t
sshpass -p$PASS ssh -f -t -t -t $USER@$HOST $COMMANDAdditionally you might have to remove "requiretty" from sudo config (/etc/sudoers) on the remote machine.
@exhuma had an example that lead to it working for me.
The password needs to be in form of -pPassword (without the space)
also quotes may be necessary: sshpass -p'Password&0' user@hostname
The simplest usage is to set the SSHPASS variable:
read -p "SSH password:" SSHPASS
export SSHPASSThen you can run e.g.:
sshpass -e ssh $user@$host echo TestIf something is going wrong, the -v flag will give you more information.