使用expect批量导入ssh公钥来做ansible免密认证
#!/bin/bash[ ! -f /root/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -p '' &>/dev/nullwhile 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
EOFdone < /root/host_ip.txt


请登录之后再进行评论