SSH Password with sshpass Example
sshpass tool is a good choice for non-interactive SSH password login with Linux or Unix based machines. It can be used as a one-liner or rolled in a bash shell script.
You should avoid using it in a production environment because its possible that the password can be viewed from the
history command output or log files.
Install sshpass from source to ensure you get the latest version.
$ curl -O -L http://downloads.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.gz
$ tar xvzf sshpass-1.06.tar.gz
$ cd sshpass-1.06
$ sudo make install
How to use sshpass
This example demonstrates using
sshpass on one line with multiple commands. Note the optional port number and disabled host key check.
2>> will display the output and append it to file.
sshpass -p PASSWORD ssh -p 22 -o StrictHostKeyChecking=no [email protected] 'whoami;pwd' 2>> sshpass.log
Additionally, the password can be stored in a file.
echo 'passw0rd' > PASSWORD.txt
Then the password in the file can be referenced using the
sshpass -f PASSWORD.txt ssh -o StrictHostKeyChecking=no [email protected] 'whoami;pwd' >> sshpass.log
Optionally clear your local history file.