Linux系統shell腳本程式設計——生産實戰案例
在日常的生産環境中,可能會遇到需要批量檢查内網目前線上的主機IP位址有哪些,還可能需要檢查這些線上的主機哪些端口是開放狀态,是以依靠手工來檢查是可以實作,但比較費時費力,是以需要結合shell腳本來實作批量檢查的功能,那麼今天就來做個小小的實驗。
1、開發腳本前準備
一般大家都知道,測試主機是否線上,常用的指令無非就是ping、nmap,是以,首先找一個位址來測試下ping指令的效果
[root@centos6 scripts]# ping 172.16.1.1
PING 172.16.1.1 (172.16.1.1) 56(84) bytes of data.
64 bytes from 172.16.1.1: icmp_seq=1 ttl=255 time=3.43 ms
64 bytes from 172.16.1.1: icmp_seq=2 ttl=255 time=0.699 ms
^C
--- 172.16.1.1 ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 8448ms
rtt min/avg/max/mdev = 0.525/1.053/3.436/0.884 ms
好像單純的這種指令是無法來做批量檢查的,必須要帶一些參數,否則它們一直ping下去
[root@centos6 scripts]# ping -W 2 -c 2 172.16.1.1
64 bytes from 172.16.1.1: icmp_seq=1 ttl=255 time=0.704 ms
64 bytes from 172.16.1.1: icmp_seq=2 ttl=255 time=0.481 ms
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.481/0.592/0.704/0.114 ms
這種方法可以實作,測試發送2個資料包,然後加上逾時時間,自動停止,可以達到效果
[root@centos6 scripts]# echo $?
[root@centos6 scripts]# ping -W 2 -c 2 172.16.1.100
PING 172.16.1.100 (172.16.1.100) 56(84) bytes of data.
--- 172.16.1.100 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 2836ms
1
是以,我們可以通過傳回值來判斷是否線上
2、開發簡單腳本
既然有實作的方法了,那麼接下來就開始開發腳本了
[root@centos6 scripts]# vi checkip.sh
#!/bin/sh
. /etc/init.d/functions
#加載系統函數庫
CMD="ping -W 2 -c 2"
#定義指令變量
IP="172.16.1.2 172.16.1.3 172.16.1.100"
#定義IP變量
for n in $IP
#for循環語句
do
$CMD $n >/dev/null 2>&1
#将指令結果不輸出
if [ $? -eq 0 ];then
#如果傳回值為0就表明線上
action "$n is online" /bin/true
#線上就列印此資訊
else
#否則就表示不線上
action "$IP$n is not online" /bin/false
#不線上就列印此資訊
fi
done
執行下腳本看看結果如何
[root@centos6 scripts]# sh checkip.sh
172.16.1.2 is online [ OK ]
172.16.1.3 is online [ OK ]
172.16.1.100 is not online [FAILED]
此時肯定有小夥伴問了,你這個腳本測試的隻有三個IP,如果内網整個網段IP都手工寫上去,豈不是更費時費力,是以,如果是整個網段,那麼定義IP變量時可以定義成這樣IP="172.16.1." ,因為前三位是相同的,寫for 循環時可以修改成如下
for n in `seq 254`
$CMD $IP$n(将兩段數字拼接成IP地位址)
具體這裡就不再測試了,有興趣的可以自行測試下
3、開發nmap腳本檢查線上IP與線上IP的開放端口情況
首先得了解下nmap的一些參數,它也是非常實用的指令之一,在日常實際生産環境中,經常用來檢查IP、端口、URL位址資訊,具體其中的參數這裡就不做詳細介紹了,後續有時間會分享它的相關參數用法
[root@centos6 scripts]# nmap -sP 172.16.1.1
Starting Nmap 5.51 ( http://nmap.org ) at 2016-12-03 21:09 CST
Nmap scan report for 172.16.1.1
Host is up (0.0091s latency).
MAC Address: 04:BD:70:FB:A9:B7 (Unknown)
Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds
[root@centos6 scripts]# nmap -sP 172.16.1.100
Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn
Nmap done: 1 IP address (0 hosts up) scanned in 0.41 seconds
從上面的結果來看,很容易發現線上與不線上傳回的資訊不同,但是我們需要取得線上的IP位址資訊,那到就隻能取 Nmap scan report for 172.16.1.1 ,因為所有線上的IP傳回的資訊中都會有這一行資訊,是以取相同的資訊。
[root@centos6 scripts]# nmap -sS 172.16.1.1|grep "Nmap scan report for"
[root@centos6 scripts]#
nmap -sS 172.16.1.1|grep "Nmap scan report for"|awk '{print $5}'
172.16.1.1
#取出IP資訊
[root@centos6 scripts]# nmap -sS 172.16.1.1
Starting Nmap 5.51 ( http://nmap.org ) at 2016-12-03 20:56 CST
Host is up (0.041s latency).
Not shown: 994 closed ports
PORT STATE SERVICE
21/tcp open ftp
22/tcp filtered ssh
23/tcp open telnet
80/tcp open http
179/tcp filtered bgp
443/tcp open https
Nmap done: 1 IP address (1 host up) scanned in 8.74 seconds
檢查開啟端口,我們可以通過過濾關鍵字 open 來實作,通過上面的資訊很容易觀察出來
[root@centos6 scripts]# nmap -sS 172.16.1.1|grep "open"
[root@centos6 scripts]# nmap -sS 172.16.1.1|grep "open"|awk '{print $1}'
21/tcp
23/tcp
80/tcp
443/tcp
4、編寫腳本并測試效果
[root@centos6 scripts]# vi checkip_namp01.sh
. /etc/init.d/functions
#加載系統函數庫
FCMD="nmap -sP "
#定義第一個指令變量
IP="172.16.1.1 172.16.1.2 172.16.1.100"
#定義IP變量
TCMD="nmap -sS"
#定義第一個指令變量
UPIP=`$FCMD $IP|grep "Nmap scan report for"|awk '{print $5}'`
#定義擷取線上IP的變量
for ip in ${UPIP}
#for手環語句
action "$ip is on line" /bin/true
#列印資訊
UPPORT=`$TCMD $ip|grep "open"|awk '{print $1}'`
#定義擷取線上IP的開放端口變量
for port in ${UPPORT}
#二層循環檢查端口
do
action "$ip $port is open" /bin/true
#将上面線上IP開放的端口資訊列印輸出
done
UPPORT=`$TCMD $ip|grep "open"|awk '{print $1}'` 定義這個變量時,取的IP位址一定要是上一個循環取出的IP位址,否則會有問題
執行腳本,測試效果如何?
[root@centos6 scripts]# sh checkip_namp01.sh
172.16.1.1 is on line [ OK ]
172.16.1.1 21/tcp is open [ OK ]
172.16.1.1 23/tcp is open [ OK ]
172.16.1.1 80/tcp is open [ OK ]
172.16.1.1 443/tcp is open [ OK ]
172.16.1.2 is on line [ OK ]
172.16.1.2 23/tcp is open [ OK ]
172.16.1.100沒有出現的原因是它不線上
接下來測試下腳本檢查的端口是否正确
[root@centos6 scripts]# telnet 172.16.1.1 443
Trying 172.16.1.1...
Connected to 172.16.1.1.
Escape character is '^]'.
^]
telnet> quit
Connection closed.
[root@centos6 scripts]# telnet 172.16.1.1 21
220 FTP service ready.
[root@centos6 scripts]# telnet 172.16.1.2 23
Trying 172.16.1.2...
Connected to 172.16.1.2.
TL-AP301C login:
從上面的結果來看,腳本檢查的結果是正确,如果需要檢查整個網段隻需要将定義IP變量時定義成“IP="172.16.1.0/24"”即可
腳本寫的可能也不太完美,需要進行改進,歡迎各位大牛多指導,感謝!!!
更多内容敬請關注民工哥個人微信公衆号——友侃有笑
或掃描下方二維碼關注
<a href="http://s2.51cto.com/wyfs02/M01/8B/3E/wKioL1hH-KOhVlY6AABspnf9dSI924.jpg-wh_500x0-wm_3-wmp_4-s_991608999.jpg" target="_blank"></a>
本文轉自 民工哥 51CTO部落格,原文連結:http://blog.51cto.com/mingongge/1880458