#!/bin/bash
[ ! -f /root/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -p '' &>/dev/null
while read line;do
ip=`echo $line | cut -d " " -f1`
user_name=`echo $line | cut -d " " -f2`
pass_word=`echo $line | cut -d " " -f3`
/usr/bin/expect << EOF
spawn ssh-copy-id $user_name@$ip
expect {
"yes/no" { send "yes\n";exp_continue}
"password" { send "$pass_word\n"}
}
expect eof
EOF
done < /root/host_ip.txt
该脚本只可对未接受该客户端的公钥的服务器第一次使用,如果重复使用,需要去服务端删除/root/.ssh/authorized.key中关于客户端的公钥条目,否则报错!!!