一:copy公钥脚本
[root@ansible tmp]# cat copy-pubkey.sh
#!/bin/bash
PassWord=$1
rpm -qa |grep sshpass 2>&1 > /dev/null
if [ $? -ne 0 ];then
yum -y install sshpass
fi
if [ ! -f ~/.ssh/id_rsa ];then
ssh-keygen -P "" -f ~/.ssh/id_rsa
fi
for ip in {192.128.232.13,192.128.232.15}
do
echo "== copy $ip pub-key to $ip is starting =="
sshpass -p${PassWord} ssh-copy-id -i /root/.ssh/id_rsa.pub root@$ip "-o StrictHostKeyChecking=no " &> /dev/null
if [ $? -eq 0 ];then
echo "host $ip is successed"
fi
echo "== copy pub-key to $ip is end =="
done
#执行脚本,后面要加位置参数为密码
[root@ansible tmp]# ./copy-pubkey.sh 123456
二:检查批量主机是否可以正常免秘钥操作
[root@ansible tmp]# cat check-sship.sh
#!/bin/bash
CMD=$1
for ip in {192.128.232.13,192.128.232.15}
do
echo "=== host $ip check ==="
ssh $ip $CMD
if [ $? -eq 0 ];then
echo "ssh login successed"
fi
done
#执行脚本,后面需要加位置参数,是在远程主机上执行的操作
[root@ansible tmp]# ./check-sship.sh hostname
三:优化脚本,如果主机的password都不一样,就需要使用下面的脚本
[root@master03 ~]# cat ssh-copy.sh
#!/bin/bash
rpm -qa |grep sshpass 2>&1 > /dev/null
if [ $? -ne 0 ];then
yum -y install sshpass
fi
if [ ! -f ~/.ssh/id_rsa ];then
ssh-keygen -P "" -f ~/.ssh/id_rsa
fi
for ip in {192.128.232.13,192.128.232.15}
do
echo "== copy $ip pub-key to $ip is starting =="
echo -n "Enter $ip password: "
read PassWord
sshpass -p${PassWord} ssh-copy-id -i /root/.ssh/id_rsa.pub root@$ip "-o StrictHostKeyChecking=no " &> /dev/null
if [ $? -eq 0 ];then
echo "host $ip is successed"
fi
echo "== copy pub-key to $ip is end =="
done
1.执行操作,拷贝公钥到定义的远程主机,实现免秘钥
[root@master03 ~]# chmod +x ssh-copy.sh
[root@master03 ~]# ./ssh-copy.sh
== copy 192.128.232.13 pub-key to 192.128.232.13 is starting ==
Enter 192.128.232.13 password: 123456 # 这里就要输入13主机的password
host 192.128.232.13 is successed
== copy pub-key to 192.128.232.13 is end ==
== copy 192.128.232.15 pub-key to 192.128.232.15 is starting ==
Enter 192.128.232.15 password: 123456 # 这里就要输入15主机的password
host 192.128.232.15 is successed
== copy pub-key to 192.128.232.15 is end ==
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)