fenfa_sshkey.exp
#!/usr/bin/expect
if { $argc != 2 } {
send_user "usage: expect fenfa_sshkey.exp file host\n"
exit
}
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "123456"
spawn ssh-copy-id -i $file "-p 22 bier888@$host"
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
}
expect eof
exit -onexit {
send_user "say good bye to you!\n"
}
用法:
[bier888@WebServer ~]$ expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub 192.168.1.139
直接发送到对方机器的家目录下面
把执行的命令放到一个脚本里面循环批量执行
fenfa_sshkey.sh
#!/usr/bin/expect
. /etc/init.d/functions
for ip in `cat iplist`
do
expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub $ip
if [ $? -eq 0 ]
then
action "scp $file to remotedir is OK" /bin/true
else
action "scp $file to remotedir is FAIL" /bin/false
fi
done
执行
sh fenfa_sshkey.sh
这样子就不需要yes和no的提示了和不需要密码