程式扎記: [Linux 文章收集] sshpass Command : Non-interactive Password Authentication with SSH

標籤

2015年4月7日 星期二

[Linux 文章收集] sshpass Command : Non-interactive Password Authentication with SSH

Source From Here 
Preface 
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: 
# 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

Example List 
Example:1 Supply Password with SSH 
-p password: Provide password as argument (security unwise)
# sshpass -p 'password' ssh ldap.nextstep4it.com -l root -o StrictHostKeyChecking=no

The -l argument is used to decide user_name to login; the -o argument is used for options in /etc/ssh/ssh_config. Here "-o 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"


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/


Example:4 For loop for copying on to remote servers 
// Create a file as the following
# vi /tmp/scr
  1. server2.nextstep4it.com  
  2. server3.nextstep4it.com  
  3. server4.nextstep4it.com  
  4. 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


Supplement 
禁用SSH 遠程主機的公鑰檢查

沒有留言:

張貼留言

網誌存檔