脚本功能
实现了批量导入公钥到对方主机,实现ssh免密登录。修改了/etc/ssh/ssh_config中的StrictHostKeyChecking 不检查对方公钥,第一次连接的时候,便不会问你是否保存公钥
#!/bin/bash
#
#********************************************************************
#Author: liuhao
#QQ: 1921160095
#Date: 2019-11-06
#FileName: ip.sh
#Description: The test script
#Copyright (C): 2019 All rights reserved
#********************************************************************
read -p "请输入起始ip: " beginip
read -p "请输入结束ip: " endip
read -p "请输入密码: " mima
begarray=(${beginip//./ })
endarray=(${endip//./ })
if [ "${begarray[0]}" = "${endarray[0]}" -a "${begarray[1]}" = "${endarray[1]}" -a "${begarray[2]}" = "${endarray[2]}" ]; then
[ -f /root/.ssh/id_rsa ] || ssh-keygen -P "" -f /root/.ssh/id_rsa &> /dev/null
rpm -q sshpass &> /dev/null || yum install sshpass -y &> /dev/null
sed -i '/Checking ask/c StrictHostKeyChecking no' /etc/ssh/ssh_config
begin=${begarray[3]}
end=${endarray[3]}
while [ "${begarray[3]}" -le "${endarray[3]}" ];do
if ping -c1 -w1 ${endarray[0]}.${endarray[1]}.${endarray[2]}.${begarray[3]} &> /dev/null ; then
{
if sshpass -p $mima ssh-copy-id -i /root/.ssh/id_rsa.pub ${endarray[0]}.${endarray[1]}.${endarray[2]}.${begarray[3]} &> /dev/null ;then
echo ${endarray[0]}.${endarray[1]}.${endarray[2]}.${begarray[3]} success >> success.txt
else
echo ${endarray[0]}.${endarray[1]}.${endarray[2]}.${begarray[3]} error >> error.txt
fi
echo ${begarray[3]}
}&
else
echo ${endarray[0]}.${endarray[1]}.${endarray[2]}.${begarray[3]} host is unreachable >> error.txt
fi
let begarray[3]++
done
wait
else
echo 仅支持变动最后一位,范围1~254
fi