在此之前遇到两个问题,一个是如何对shell同时进行两个变量的循环

http://bbs.chinaunix.net/thread-4096418-1-1.html

另一个是如何在shell中调用expect内容

http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4096482&page=1#pid23955323


在QQ群和论坛大神的协助下顺利解决这俩问题

最终结果为

#!/bin/sh
while read ip password
        do
                expect <<EOF
                set timeout 3
                #spawn ssh root@$ip
                spawn scp /root/.ssh/id_rsa.pub root@$ip:/root/.ssh/authorized_keys
                expect {
                        "*yes/no" {send "yes\r";exp_continue}
                        "*password:" {send "$password\r"}
                }
                expect eof
EOF
        done < ip.txt



另一个方法为QQ群里的大神提供

#!/bin/bash
#2013-7-17
#version 1.0
autologin(){
expect -c "
set timeout -1;
#a=cat 1.txt
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $1
expect {
        "*yes/no" {send "yes"\r;exp_continue}
        "*assword*" {send $2\r}
}
interact;"
}
for ((i=1;i<=`cat 1.txt|wc -l`;i++))
do
  line=`sed -n "${i}p" 1.txt`
  IP=`echo $line|awk '{print $1}'`
  PASS=`echo $line|awk '{print $2}'`
  autologin $IP $PASS
done


运维参考资料

http://item.taobao.com/item.htm?spm=a1z10.5.w4002-1279326270.15.0tzUHO&id=18619178113