腳本功能
實現了批量導入公鑰到對方主機,實現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