sshpass
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
1 |
yum install sshpass |
On Debian/Ubuntu
1 |
apt install sshpass |
Example
1 2 3 |
boby@sok-01:~$ sshpass ssh -o StrictHostKeyChecking=no -p 3333 root@ok.serverok.in "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
1 |
sshpass ssh -o StrictHostKeyChecking=no -p 3333 root@ok.serverok.in "mysqldump serverok_wp > /root/serverok_wp.sql" |
Command-line options for sshpass are
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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
1 |
sshpass -p PASSWORD_HERE ssh USER@SERVER_IP |
To use a password from a file, use
1 2 3 |
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