Overview
Linux system Admins normally login to the Linux servers either supplying a password or using key-based authentication. sshpass is a tool that allows us to automatically supply passwords to the command prompt so that automated scripts can be run as desired by users. sshpass supplies password to ssh prompt using dedicated tty, fooling ssh to believe that an interactive user is supplying a password.
Some of the common uses of sshpass
1. taking backups to a remote server
2. executing commands on systems at a specified time.
sshpass Installation
1. Centos Based distributions:
Setup the EPEL repository from https://fedoraproject.org/wiki/EPEL and then as root run:
# yum -y install sshpass
2. Ubuntu/Debain based distributions:
As root run:
# apt-get install sshpass
3. Compile & install from the source:
# wget http://sourceforge.net/projects/sshpass/files/latest/download -O sshpass.tar.gz # tar -zxvf sshpass.tar.gz # cd sshpass-1.05/ # ./configure # make # make install # which sshpass /usr/local/bin/sshpass
Getting Help
# sshpass -h 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 -h Show help (this screen) -V Print version information
At the most one of -f, -d, -p or -e should be used
Example 1: supply password with ssh
# sshpass -p 'password' ssh ldap.thegeekdiary.com -l root -o StrictHostKeyChecking=no
Where:
password is the password of your server(ldap.thegeekdiary.com). ‘StrictHostKeyChecking=no‘ is used to control logins to machines whose host key is not known or has changed.
EXAMPLE:2 TO RUN SOME COMMAND ON THE REMOTE SERVER VIZ CHECKING UPTIME AND UNAME
Example 2: To run some command on the remote server
Lets try running 2 commands “uptime” and “uname” on the remote server using the sshpass commnad:
# sshpass -p 'password' ssh ldap.thegeekdiary.com -l root -o StrictHostKeyChecking=no "uptime;uname -a"
Sample Output:
18:49:34 up 21 days, 18:49, 3 users, load average: 0.01, 0.00, 0.00 Linux ldap.thegeekdiary.com 2.6.32-220.el6.x86_64 #1 SMP Tue Dec 6 19:48:22 GMT 2011 x86_64 x86_64 x86_64 GNU/Linux
Example 3: Copying a file using rsync to a server
In our case we are copying a file sshpass.tar.gz to a remote server ldap.thegeekdiary.com
# sshpass -p 'password' rsync -av --progress sshpass.tar.gz root@ldap.thegeekdiary.com:/tmp/
Output of above Command:
sending incremental file list sshpass.tar.gz 98362 100% 62.56MB/s 0:00:00 (xfer#1, to-check=0/1) sent 98472 bytes received 31 bytes 197006.00 bytes/sec total size is 98362 speedup is 1.00 root@server1:/home/thegeekdiary#
Example 4: for loop for copying on to remote servers
Create a file as the following:
# touch /tmp/scr
The file should contain the name of the hosts:
server2.thegeekdiary.com server3.thegeekdiary.com server4.thegeekdiary.com server5.thegeekdiary.com
# for i in `cat /tmp/scr`; do echo " ";echo "###$i####"; sshpass -p 'password' rsync -av --progress sshpass.tar.gz root@$i:/tmp/; done
Output of Above Command:
###server2.thegeekdiary.com#### sending incremental file list sent 54 bytes received 12 bytes 132.00 bytes/sec total size is 98362 speedup is 1490.33 ###server3.thegeekdiary.com#### sending incremental file list sent 54 bytes received 12 bytes 44.00 bytes/sec total size is 98362 speedup is 1490.33 ###server4.thegeekdiary.com#### sending incremental file list sent 54 bytes received 12 bytes 132.00 bytes/sec total size is 98362 speedup is 1490.33 ###server5.thegeekdiary.com#### sending incremental file list sent 54 bytes received 12 bytes 132.00 bytes/sec total size is 98362 speedup is 1490.33