脚本:
vim key.sh
#!/bin/bash
#make key
m -f /root/.ssh/id_dsa
ssh-keygen -t dsa -f /root/.ssh/id_dsa -P "" -q
#fengfagongyao
for ip in 12 11
do
echo ====fenfa key to host 10.0.0.$ip====
sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub -o StrictHostKeyChecking=no root@10.0.0.$ip
echo =============== end==============
echo " "
done
脚本说明:
ssh-keygen -t dsa -f /root/.ssh/id_dsa -P "" -q
创建密钥,-f指定存放位置,-P 密钥加密的密码 -q 减少信息输出
sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub -o StrictHostKeyChecking=no root@10.0.0.$ip
这里需要安装一个软件 yum install sshpass -y 用来提供中户密码
ssh-copy-id 命令来分发密钥 -i 指定密钥本地存放的路径
-o StrictHostKeyChecking=no 在登陆其他服务器是不选择yes/no