偷得浮生 偷得浮生

记录精彩的人生

目录
expect实现批量基于ssh的key部署
/  

expect实现批量基于ssh的key部署

[14:04:26 root@centos8 ~]#cat push_ssh_key.sh
#!/bin/bash
#
#********************************************************************
#Author:zhangzhuo
#QQ: 1191400158
#Date: 2021-01-12
#FileName:push_ssh_key.sh
#URL: https://www.zhangzhuo.ltd
#Description:The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#对方主机的登录密码
PASS=123456
#测试expect软件是否安装没有安装进行安装
rpm -q expect &>/dev/null || yum -y install expect &>/dev/null
#生成自己的ssh私钥
ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa &>/dev/null && echo "ssh key is created"
#给远程主机部署自己的key
while read IP ;do
expect &>/dev/null <<EOF
set timeout 20
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$IP
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$PASS\n" }
}
expect eof
EOF
echo $IP is ready
#下面的hosts.txt文件需要写远程主机的IP
done <hosts.txt

[14:04:31 root@centos8 ~]#cat hosts.txt 
192.168.10.81
192.168.10.71