SSH Password with sshpass: Install Tutorial Examples

SSH Password with sshpass Example


SSH PasswordThe 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 ps or history command output or log files.

Overview

Install sshpass

Install sshpass from source to ensure you get the latest version.

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 -f flag.
sshpass -f PASSWORD.txt ssh -o StrictHostKeyChecking=no [email protected] 'whoami;pwd' >> sshpass.log

Optionally clear your local history file.
history -c

See also:
Bash One-liners: Linux, If, Else, For loop, Sed, Awk
Expect SSH Script for a Cisco Device: Tutorial Example
Golang SSH Client: Crypto & Goexpect Examples