1. exit #退出循環(huán),同時(shí)也會(huì)退出腳本
[root@shell /scripts/shell-day31]# cat exit.sh
#!/bin/bash
for i in {1..3}
do
? ? echo "123"
? ? exit
? ? echo "456"
done
echo "腳本執(zhí)行結(jié)束"
[root@shell /scripts/shell-day31]# sh exit.sh
123
2. break #跳出當(dāng)前循環(huán),繼續(xù)執(zhí)行循環(huán)以外的命令
[root@shell /scripts/shell-day31]# cat break.sh
#!/bin/bash
for i in {1..3}
do
? ? echo "123"
? ? break
? ? echo "456"
done
echo "腳本執(zhí)行結(jié)束"
[root@shell /scripts/shell-day31]# sh break.sh
123
腳本執(zhí)行結(jié)束
3. continue #跳出本次循環(huán),不繼續(xù)執(zhí)行循環(huán)的剩余代碼。繼續(xù)執(zhí)行下一次的循環(huán)。
[root@shell /scripts/shell-day31]# cat continue.sh
#!/bin/bash
for i in {1..3}
do
? ? echo "123"
? ? continue
? ? echo "456"
done
echo "腳本執(zhí)行結(jié)束"
[root@shell /scripts/shell-day31]# sh continue.sh
123
123
123
腳本執(zhí)行結(jié)束
4. 先掃描10.0.0.0/24網(wǎng)段內(nèi)的主機(jī),存活的主機(jī)則下發(fā)公鑰。
1. 先判斷是否存在密鑰,沒(méi)有則進(jìn)行創(chuàng)建密鑰對(duì),有則跳過(guò)
2. 批量探測(cè)主機(jī)是否存活,存活則判斷遠(yuǎn)程端口是否開(kāi)放
3. 主機(jī)存活且端口開(kāi)放的主機(jī)則分發(fā)公鑰
4. 判斷是否發(fā)送成功
如何免交互式的進(jìn)行創(chuàng)建密鑰對(duì)
-f filename #指定私鑰文件保存的路徑
-N new_passphrase #指定一個(gè)新的密碼
ssh-keygen? -t rsa -f /root/.ssh/id_rsa -N ""
如何免交互式的分發(fā)公鑰
-o StrictHostKeyChecking=no #忽略回復(fù)yes的交互(避免第一次交互出現(xiàn) 公鑰檢查)
sshpass -p123456 #指定密碼為123456,忽略交互
yum install -y sshpass
sshpass -p1? ssh-copy-id -i /root/.ssh/id_rsa.pub? -o "StrictHostKeyChecking=no"? root@10.0.0.7
[root@shell /scripts/shell-day31]# cat fenfa.sh
#!/bin/bash
#1.引用函數(shù)庫(kù)
[ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函數(shù)庫(kù)文件不存在!"
#2.判斷是否存在秘鑰對(duì)
if [ -f /root/.ssh/id_rsa.pub ] && [ -f /root/.ssh/id_rsa ];then
? ? action "密鑰對(duì)文件存在!" /bin/true
else
? ? rm -rf /root/.ssh &>/dev/null
? ? ssh-keygen? -t rsa -f /root/.ssh/id_rsa -N "" &>/dev/null
? ? if [ $? -eq 0 ];then
? ? ? ? action "已經(jīng)創(chuàng)建新的密鑰對(duì)!"? /bin/true
? ? else
? ? ? ? action "新的密鑰對(duì)創(chuàng)建失敗!"? /bin/false
? ? ? ? exit
? ? fi
fi
#3.批量探測(cè)主機(jī)是否存活
Ip_log=/tmp/ip.log
>$Ip_log
i=1
while [ $i -le 254 ]
do
? ? {
? ? ? ? IP=10.0.0.$i
? ? ? ? ping -c1 -W1 $IP &>/dev/null
? ? ? ? if [ $? -eq 0 ];then
? ? ? ? ? ? action "${IP}主機(jī)是存活的..........." /bin/true
? ? ? ? ? ? echo "$IP" >>$Ip_log
? ? ? ? fi
? ? }&
? ? let i++
? ? sleep 0.1
done
wait
while read line
do
? ? State=$(nmap -p22 $line | awk '/^22/{print $2}')
? ? if [ $State == "open" ];then
? ? ? ? action "主機(jī)地址${line}遠(yuǎn)程端口是開(kāi)放的........" /bin/true
? ? ? ? sshpass -p1? ssh-copy-id -p22 -i /root/.ssh/id_rsa.pub? -o "StrictHostKeyChecking=no"? root@$line &>/dev/null
? ? ? ? if [ $? -eq 0 ];then
? ? ? ? ? ? action "主機(jī)地址${line}公鑰發(fā)送成功........" /bin/true
? ? ? ? else
? ? ? ? ? ? action "主機(jī)地址${line}公鑰發(fā)送失敗........" /bin/false
? ? ? ? fi
? ? else
? ? ? ? action "主機(jī)地址${line}遠(yuǎn)程端口是關(guān)閉的........" /bin/false
? ? fi
done < $Ip_log