您的位置:首页 > 其它

Expect--自动批量分发公钥脚本

2016-08-01 10:28 323 查看
1.在使用之前,先安装epel源,yum install expect
2.写分发脚本,后缀为exp
#!/usr/bin/expect
set host_ip [lindex $argv 0]
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $host_ip
expect {
-timeout 60
"(yes/no)?" { send "yes\n";exp_continue}
"password:" { send "123456\n"}
timeout {puts "Connect timeout!";return}
}
expect eof
exit -onexit {
send_user "Job has finished!"
}
注:set的作用是设置变量,spawn设置执行命令时,可以引用变量;变量的第一个参数为0

3.设置变量列表,并调用自动分发脚本;后缀为sh
#!/bin/bash
iplist=(
192.168.0.241
192.168.0.242
192.168.0.243
)
for ip in ${iplist[*]};do
expect /root/ssh164.exp $ip
done
4.开始分发
#sh ssh164.sh
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  expect
相关文章推荐