sshpass is used to non interactively log in to remote SSH server and execute commands. It can use a password from a file, environment variable, or from a command-line argument.
To install sshpass on RHEL/CentOS, run
yum install sshpass
On Debian/Ubuntu
apt install sshpass
Example
boby@sok-01:~$ sshpass ssh -o StrictHostKeyChecking=no -p 3333 [email protected] "uptime" 02:16:14 up 470 days, 10:41, 0 users, load average: 0.07, 0.04, 0.04 boby@sok-01:~$
Take MySQL backup on a remote server
sshpass ssh -o StrictHostKeyChecking=no -p 3333 [email protected] "mysqldump serverok_wp > /root/serverok_wp.sql"
Command-line options for sshpass are
root@sok-01:~# sshpass Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters -f filename Take password to use from file -d number Use number as file descriptor for getting password -p password Provide password as argument (security unwise) -e Password is passed as env-var "SSHPASS" With no parameters - password will be taken from stdin -P prompt Which string should sshpass search for to detect a password prompt -v Be verbose about what you're doing -h Show help (this screen) -V Print version information At most one of -f, -d, -p or -e should be used root@sok-01:~#
To login to password using a password in the command line, use
sshpass -p PASSWORD_HERE ssh USER@SERVER_IP
To use a password from a file, use
echo 'PASSWORD_HERE' > pw_file chmod 0400 pw_file sshpass -f pw_file ssh USER@SERVER_IP
Using password-less authentication using an RSA key is much more secure. But sshpass can be used in places where you can’t use RSA key.
See SSH