Shell脚本批量传输SSH密钥文件实现免密登录
#!/bin/bash
rm -rf ~/.ssh/{known_hosts,id_rsa*}
ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa #这里写法并不是唯一,下面我会再放一个类似的写法
yum -y install expect tcl
expect << EOF
spawn ssh-copy-id 192.168.163.70 #如果你是批量免密,则需要+for循环实现
expect "(yes/no)?" {send "yes\r"}
expect "password:" {send "docker\r"}
expect "#" {send "exit\r"}
EOF
-------------------------------------批量免密请看这里--------------------------------------------
#!/bin/bash
rm -rf ~/.ssh/{known_hosts,id_rsa*}
ssh-keygen -t rsa -N '' <<EOF #这个写法跟上面是类似的。写法不一样
/root/.ssh/id_rsa
yes
EOF
yum -y install expect tcl
for i in {21..59} #这里根据自己需求而定
do
expect << EOF
spawn ssh-copy-id 192.168.163.$i
expect "(yes/no)?" {send "yes"}
expect "password:" {send "123456"} #客户端密码
expect "#" {send "exit"}
EOF
done
chmod +x 脚本文件 #添加执行权限
脚本跑完之后,会有回馈信息
Number of key(s) added: 1 #添加的密钥数量
Now try logging into the machine, with: “ssh ‘192.168.163.70’” #可以ssh 登录192.168.163.70机器
expect 是一个工具,有需要进一步了解的可以参考这个文档 https://blog.csdn.net/m0_53396354/article/details/125511170