Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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.

share|improve this question

9 Answers 9

I have a command like this:

sshpass -pfoobar ssh -o StrictHostKeyChecking=no user@host command_to_run

and it works.

In my case StrictHostKeyChecking=no is necessary because the commands are run on a ton of hosts which are exchanged regularly (network routers).

Out of curiosity, I tried to run it without the command, just to see what was happening. It turns out, sshpass keeps silent (no output on stderr). But the return-code in that case is 6. At the time of this writing, this was revision 50, and the matching constant in the code is RETURN_HOST_KEY_UNKNOWN, which makes sense.

Maybe that's the same issue in your case?

If that's not the case, have you properly quoted the remote command?

share|improve this answer

sshpass only works if the password prompt ends in assword:.

If your password prompt is different you may have to adjust the sourcecode 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.

share|improve this answer

Did you setup passwordless login?

Password-less logins with OpenSSH

share|improve this answer
I'd rather stick to sshpass, due to the inconvenients of enabling password-less login in an enterprise –  Roman Rdgz Apr 17 '13 at 8:57
Did you try this: sshpass -p 'mypass' ssh –  konrad Apr 17 '13 at 9:13
I did, doesn't matter if i use 'mypass' or mypass. It starts waiting forever –  Roman Rdgz Apr 17 '13 at 9:22
sshpass -V works? –  konrad Apr 17 '13 at 9:38
It prints: "This program is free software, and can be distributed under the terms of the GPL. See the COPYING file for more information". And then ends its execution and does nothing else (I tried adding -V to the beginning of the command I was handling, not just typing sshpass -V) –  Roman Rdgz Apr 17 '13 at 9:41

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.

share|improve this answer

sshpass syntax is

sshpass [-ffilename|-dnum|-ppassword|-e] [options] command arguments

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

and it will execute the command uname -a on the remote server and output the results to the standard out on the local machine.

share|improve this answer

What you need is -f -t -t -t

sshpass -p$PASS ssh -f -t -t -t $USER@$HOST $COMMAND

Additionally you might have to remove "requiretty" from sudo config (/etc/sudoers) on the remote machine.

share|improve this answer

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:

  1. Set directly in your code:

    export SSHPASS='your_pass'
  2. Or ask for it:

    readPassword () {
      echo Ssh Password: 
      read -s SSHPASS
      eval "export SSHPASS='""$SSHPASS""'"
    # read password

Than execute your command;

  1. 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 ...
  2. If your command will be dynamic use like this:

    eval 'sshpass -e ssh user@host << EOF
    mplayer "$DYN_FILE_LOC"
    command2 parameter1 parameter2 parameter3 ...
    command3 parameter1 parameter2 parameter3 ...

hope this helps someone.

share|improve this answer
In your readPassword function, can't you just do export SSHPASS? –  kutschkem Apr 20 at 11:13
@kutschkem, I trust but my idea that i think here; i dont want to write my pass in a static file but instead of writing password again and again for lots of servers(7+), i write once :) You don't have to use readPassword function, you can just write your password, it should work :) Only i want to share my code which is shows an example using sshpass for dynamic commands :D –  veysiertekin Apr 20 at 12:07
No, I just mean that the eval is not necessary after the read, export does not need an assignment. –  kutschkem Apr 20 at 12:35
  1. make a key

    ssh-keygen -t rsa

  2. make a .ssh folder on the remote server it will ask for a password

    ssh $USER@SEVERNAME mkdir -p .ssh

  3. copy the key to the remote server

    cat .ssh/ | ssh $USER@SERVER 'cat >> .ssh/authorized_keys'

Check if it asks for a password, if not it should work.

ssh $USER@SEVERNAME 'command'
share|improve this answer

@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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.