批量部署ssh私钥

#!/bin/bash 
# auto copy publick key to ssh server,but you must create the rsa public key first! 
# include a  expect expect script 
# see the log file  /tmp/test.log 
#批量部署ssh私钥,密码不相同!  
if
  rpm -q expect
then
  echo "good" >/dev/null
else
  echo "you must install the expect package"
fi  

cat> /tmp/auto.tcl << end
#!/usr/bin/expect -f  
set timeout 10 
#set user [lrange $argv 2 2] 
set password [lrange \$argv 1 1]   
set ipaddr   [lrange \$argv 0 0]  
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@\$ipaddr  
expect  "Are you sure you want to continue connecting (yes/no)?" 
send "yes\r" 
expect  "*?assword:"  
send "\$password\r"
expect end
end

chmod 755 /tmp/auto.tcl  
rm -f /root/.ssh/known_hosts 
for i in `cat /server.txt`    #server.txt格式为: IP:password                                                      
do 
  IP=`echo $i|cut -f1 -d:` 
  echo "$IP"
  PASS=`echo $i|cut -f2 -d:` 
  echo "$PASS"
  if 
    ping -c2 $IP &>/dev/null 
     then 
     /tmp/auto.tcl $IP $PASS
     # ssh-agent bash
    # ssh-add .ssh/id_rsa
     echo "********$IP**************">>/tmp/up2.log 
     ssh $IP ifconfig eth0|grep 'inet addr:'|cut -f2 -d:|cut -f1 -d' ' >>/tmp/test.log 
  else 
     echo "the server $IP is down">>/tmp/down2.log
  fi 
done

你可能感兴趣的:(批量部署ssh私钥)