#!/bin/bash
host_file="host_tmp.txt"
host=$1
passwd=$2
username="root"
[ $# -ne 2 ] && echo "Please input parameter host password" && exit
echo "$host $username $passwd " >$host_file
while read host ;do
#ip user passwd
ip=$(echo $host|awk ‘{print $1}‘)
user=$(echo $host|awk ‘{print $2}‘)
passwd=$(echo $host|awk ‘{print $3}‘)
expect <<EOF
spawn ssh-copy-id -i $user@$ip
expect {
"yes/no" {send "yes\n";exp_continue}
"password" {send "$passwd\n"}
}
expect eof
EOF
echo "$host -- $user 完成了免密登录!"
done < $host_file
rm -f $host_file
Shell设置免密登陆