#!/usr/bin/expect
# filename: distribute_key.exp
if [ $argc != 2 ]{
send_user "usage: expect expect.exp file host
"
exit
}
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "123456"
spawn ssh-copy-id -i $file "-p 22 root@$host"
expect {
"yes/no" {send "yes
";exp_continue}}
"*password" {send "$password
"}
}
expect eof
#!/bin/bash
# filname:distribute_key.sh
for n in 128 129 130
do
expect distribute_key.exp ~/.ssh/id_dsa.pub 192.168.33.$n
done
测试;
ssh-keygen -t dsa -P "" -f ~/.ssh/id_dsa >/dev/null 2>&1
sh distribute_key.sh