#!/binbash ! rpm -qa |grep expect &> /dev/null && yum -y install expect #判断是否已下载expect 若没有则下载该服务
if [ ! -e /root/.ssh/id_rsa.pub ];then #判断公钥是否已存在 没有则生成 /usr/bin/expect <<EOF set timeout 10 spawn ssh-keygen expect "id_rsa" send " " expect "passphrase" send " " expect "agian" send " " expect eof EOF fi while read file;do #遍历文件(ip 密码) ip=`echo "$file" |awk '{print $1}'` #截取出ip passwd=`echo "$file" | awk '{print $2}'` #截取出密码 /usr/bin/expect <<EOF set timeout 5 spawn ssh-copy-id -i $ip expect { "yes" { send "yes "; exp_continue } "$ip" { send "$passwd " } } expect eof EOF done < /root/host_name #保存ip 密码的文件路径 可自定义
cat /root/host_name
192.168.198.150 xxxxxxx 192.168.198.151 xxxxxxx 192.168.198.152 xxxxxxx 192.168.198.153 xxxxxxx ......