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

1.在使用之前,先安装epel源,yum install expect -y
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 "Ak46\n"}
        timeout {puts "Connect timeout!";return}
}
expect eof
exit -onexit {
        send_user "Job has finished!"
}

注:set的作用是设置变量,spawn设置执行命令时,可以引用变量;变量的第一个参数为0
3.执行以下命令开始分发

for ip in `cat /root/ip.txt`;do expect /root/fenfa_sshkey.exp $ip ;done

 

posted @ 2018-12-15 21:15  西门运维  阅读(459)  评论(0编辑  收藏  举报