bash中使用expect批量分发公钥

#!/bin/bash
#执行此脚本需要机器支持expect命令,可以搭建本地yum源,yum install expect -y安装
USER=wq  #需要配置免密的用户
PASSWD=wq  #用户密码
for host in 1.2.3.{20..23} 1.2.3.{25..27} #机器ip,连续的ip可以用{..},多批次用空格隔开
do
echo $host
   expect -c "
     set timeout 10;
     spawn ssh-copy-id  $USER@$host;
     expect {
       \"*assword*\" {send \"$PASSWD\r\"}
       \"*yes/no*\"    {send \"yes\r\";exp_continue}
      };
     expect eof
   "
done

在bash中,用expect -c "    "把expect语句包起来,将expect -c “   ” 中的双引号加上反斜杠\

发布了15 篇原创文章 · 获赞 5 · 访问量 9057

猜你喜欢

转载自blog.csdn.net/qq_35702095/article/details/83095202