批量添加ssh认证公钥的脚本,有利于大批量管理服务器哦。
如果你的服务器的ip是有一定范围的,比如是 192.168.0.1--192.168.0.100之间,而且他们的root用户密码相同,都为redhat,那么你可以用下面的脚本快速的部署信任关系。
#!/bin/bash
# auto copy publick key to ssh server,but you must create the rsa public key first!
# auth: jarson@gmail.com
# date: 20100408
# include a expect script
# see the log file /tmp/test.log
cat>/tmp/auto.tcl<<eof
#!/usr/bin/expect -f
set timeout -1
#set user [lrange $argv 1 1]
#set password [lrange $argv 2 2]
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 "redhat\r"
expect eof
eof
chmod 755 /tmp/auto.tcl
rm -f /root/.ssh/known_hosts
for i in {1..100}
do
if
ping -c2 192.168.0.$i &>/dev/null
then
/tmp/auto.tcl 192.168.0.$i &>/dev/null
echo "********192.168.0.$i**************">>/tmp/test.log
ssh 192.168.0.$i ifconfig eth0|grep 'inet addr:'|cut -f2 -d:|cut -f1 -d' ' >>/tmp/test.log
else
echo "the server 192.168.0.$i is't exsit">>/tmp/down.log
fi
done
建立后,执行这个脚本就可以了
如果是你的服务器ip,root密码无规律咋办呢?这个时候可以这么做,建立一个文件 server.txt 内如格式如下
ip地址:root密码
比如
202.19.1.3:qwz234
201.19.1.2: de123s
...
这个时候可以这么来做
#!/bin/bash
# auto copy publick key to ssh server,but you must create the rsa public key first!
# auth: jarson@gmail.com
# date: 20100408
# include a expect expect script
# see the log file /tmp/test.log
cat>/tmp/auto.tcl<<eof
#!/usr/bin/expect -f
set timeout -1
#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 eof
eof
chmod 755 /tmp/auto.tcl
rm -f /root/.ssh/known_hosts
for i in `cat /server.txt`
do
IP=`echo $i|cut -f1 -d:`
PASS=`echo $i|cut -f2 -d:`
if
ping -c2 $IP &>/dev/null
then
/tmp/auto.tcl $IP $PASS &>/dev/null
echo "********$IP**************">>/tmp/test.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/down.log
fi
done
- jarson's blog
- Login or register to post comments

最新回应
4 days 2 hours 之前
1 year 6 days 之前
1 year 13 weeks 之前
1 year 13 weeks 之前
1 year 20 weeks 之前
1 year 22 weeks 之前
1 year 25 weeks 之前
1 year 27 weeks 之前
1 year 27 weeks 之前
1 year 27 weeks 之前