sshpass Command : Non-interactive Password Authentication with SSH

0
6903

Overview :

Linux system Admins  normally login to the linux  servers either supplying a password, or using keybased authentication. sshpass is a tool which allows us to automatically supply password to the command prompt so that automated scripts can be run as desired by users. sshpass supplies password to ssh prompt using adedicated tty , fooling ssh to believe that a interactive user is supplying 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 run

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 most one of -f, -d, -p or -e should be used

Example:1 Supply Password with SSH
# sshpass -p 'password' ssh ldap.nextstep4it.com -l root -o StrictHostKeyChecking=no

Where :

password' is the password of your server(ldap.nextstep4it.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
 # sshpass -p 'password' ssh ldap.nextstep4it.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.nextstep4it.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.nextstep4it.com

 # sshpass -p 'password' rsync -av --progress sshpass.tar.gz root@ldap.nextstep4it.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/nextstep4it#

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.nextstep4it.com
server3.nextstep4it.com
server4.nextstep4it.com
server5.nextstep4it.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.nextstep4it.com####
sending incremental file list
sent 54 bytes received 12 bytes 132.00 bytes/sec
total size is 98362 speedup is 1490.33

###server3.nextstep4it.com####
sending incremental file list
sent 54 bytes received 12 bytes 44.00 bytes/sec
total size is 98362 speedup is 1490.33

###server4.nextstep4it.com####
sending incremental file list
sent 54 bytes received 12 bytes 132.00 bytes/sec
total size is 98362 speedup is 1490.33

###server5.nextstep4it.com####
sending incremental file list
sent 54 bytes received 12 bytes 132.00 bytes/sec
total size is 98362 speedup is 1490.33

LEAVE A REPLY