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
";exp_continue}"password:" { send "Ak46
"}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