1.  #!/bin/bash 
  2. cat /root/.ssh/id_rsa.pub >> /root.ssh/authorized_keys   将公钥导到auth文件中去 
  3. host1=`cat /root/a`     ##a文件里的内容是你想要scp公钥的目标主机地址  
  4.  
  5. for i in $host1; do 
  6.  
  7. command1="scp /root/.ssh/authorized_keys $i:/root/.ssh/authorized_keys" 
  8.   
  9. expect -c " 
  10.     set timeout 60; 
  11.         spawn $command1; 
  12.         expect { 
  13.                 \"(yes/no)?\" {send \"yes\r\"; exp_continue} 
  14.         \"$i's password:\" {send \"leezqang\r\"; exp_continue}  ##  send 是密码 这种方法有个限制就是所有目标主机密码需要一致
  15.    如果密码不一致 可以定义数组来完成这个功能         } 
  16.         " 
  17.  
  18. done