- 1.首先,需要检查expect是否安装:rpm -qa|grep expect 2.然后,在操作机上创建公钥:ssh-keygen 一路回车即可 3.创建好之后到/home/barfoo/.ssh/下就可以看到id开头的2个文件,其中id_rsa.pub就是公钥文件, 4.需要做的就是将这个文件传送给其他机器:ssh-copy-id -i /home/barfoo/.ssh/id_rsa.pub barfoo@ip 5.最后ssh root@ip就实现无密码登录了 6.以下是脚本,根据自己实际情况做修
#2017-05-18
#批量ssh认证建立
for p in $(cat /home/barfoo/ip.txt) #注意ip.txt文件的绝对路径
do
ip=$(echo "$p"|cut -f1 -d":") #取ip.txt文件中的ip地址
password=$(echo "$p"|cut -f2 -d":") #取ip.txt文件中的密码
#expect自动交互开始
expect -c "
spawn ssh-copy-id -i /home/barfoo/.ssh/id_rsa.pub barfoo@$ip
expect {
"*yes/no*" {send "yes
"; exp_continue}
"*password*" {send "$password
"; exp_continue}
"*Password*" {send "$password
";}
}
"
done
1. ip.txt文件里面ip和密码写法
172.16.4.27:barfoo1
172.16.4.28:barfoo2
1. 也可以这么写
for i in `seq 101 150`
do
/usr/bin/expect << EOF
spawn ssh-copy-id bfadmin@192.168.100.$i
expect {
"yes/no" { send "yes
"; exp_continue; }
"*password" { send "P@ssw0rd
" }
}
expect "~$ "
send "exit
"
expect eof
EOF
done
下载PSSH安装包
wget http://www.theether.org/pssh/pssh-1.4.3.tar.gz #下载PSSH安装包
tar zxvf pssh-1.4.3.tar.gz
cd pssh-1.4.3
wget 'http://peak.telecommunity.com/dist/ez_setup.py'
python ez_setup.py
python setup.py install
安装好后执行
pssh -P -h ip.txt 'uptime