sshkey.exp
#!/usr/bin/expect
# 由于是多台服务器,需要在shell脚本中循环调用该脚本
if { $argc != 2 } {
send_user "usage: expect scp-expect.exp file host dir
"
exit
}
#定义变量
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "888888"
#必须使用ssh-copy-id来分发公钥,本机的公钥追到目标服务器的 .ssh/authorized_keys 里
spawn ssh-copy-id -i $file "-p 22 root@$host"
expect {
#询问yes或no,都是用yes
"yes/no" {send "yes
";exp_continue}
"*password" {send "$password
"}
}
expect eof
exit -onexit {
send_user "Kangjie say good bye to you!
"
}
通过shell脚本来调用该文件
for ip in `cat iplist`
do
expect fenfa_sshkey.exp /root/.ssh/id_dsa.pub $ip >/dev/null 2>&1
done
iplist文件中写入服务器的ip地址
分发完成后,通过: ssh 192.168.1.123 /sbin/ifconfig ifconfig eth0
如果没有提示就能显示服务器的ip,说明配置成功