Software >> OS >> Unix >> Linux >> commands >> sshpass >> What is sshpass and how to use it

What is sshpass?


The sshpass utility is designed to run SSH using the keyboard-interactive password authentication mode, but in a non-interactive way.

SSH uses direct TTY access to ensure that the password is indeed issued by an interactive keyboard user. sshpass runs SSH in a dedicated TTY, fooling SSH into thinking it is getting the password from an interactive user.

If automation is needed when using SSH password authentication, then a sshpass can be used.

How to Install


## For RHEL 7

subscription-manager repos --enable=rhel-7-server-rpms \
                           --enable=rhel-7-server-optional-rpms \
                           --enable=rhel-7-server-extras-rpms \
                           --enable=rhel-ha-for-rhel-7-server-rpms

yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

yum install sshpass

Examples


## Simple example, provide the password to sshpass and let it ssh to your server

sshpass -p MyPassword ssh username@host.example.com


## Same example with strict HostKey checking disabled

sshpass -p MyPassword -o StrictHostKeyChecking=no username@host.example.com


## using a password file to store the password.  The password should be the first line of the file

echo MyPassword > passwordfile
chmod 0400 passwordfile
sshpass -f passwordfile ssh username@host.example.com


## Using a hidden password file and encrypting the password file with GPG
## when invoking the ssh, the .gpg file is decrypted and then piped to sshpass

echo MyPassword > .sshpasswd
gpg -c .sshpasswd
rm .sshpasswd
gpg -d -q .sshpasswd.gpg | sshpass ssh username@host.example.com


## REFERENCES

https://www.redhat.com/sysadmin/ssh-automation-sshpass