无密钥登录的自动脚本实现:
vim auto_ssh.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#!/usr/bin/expect
set
timeout 10
set
username [lindex $argv 0]
set
password [lindex $argv 1]
set
hostname
[lindex $argv 2]
spawn
ssh
-copy-
id
-i
/root/
.
ssh
/id_rsa
.pub $username@$
hostname
expect {
#first connect, no public key in ~/.ssh/known_hosts
"Are you sure you want to continue connecting (yes/no)?"
{
send
"yes\r"
expect
"password:"
send
"$password\r"
}
#already has public key in ~/.ssh/known_hosts
"password:"
{
send
"$password\r"
}
"Now try logging into the machine"
{
#it has authorized, do nothing!
}
}
expect eof
|
chmod 777 auto_ssh.sh
然后执行下述命令即可。
./auto_ssh.sh root 123456 192.168.10.162
试验结果:
162机器上传看:
成功!
# cat ip.txt
192.168.191.166
192.168.191.167
192.168.191.168
192.168.191.169
# cat ip-pass.txt
192.168.191.166 password1
192.168.191.167 password2
192.168.191.168 password3
192.168.191.169 password4
删除历史访问秘钥信息,以免有变:
for i in `cat ip.txt`;do sed -i '/'$i'/d' ~/.ssh/known_hosts;done
批量添加互信服务器:
while read IP PASS;do ./auto_ssh.sh root $PASS $IP;done < ip-pass.txt
本文转自leonardos51CTO博客,原文链接: http://blog.51cto.com/leomars/1928994,如需转载请自行联系原作者