您好,登錄后才能下訂單哦!
小編給大家分享一下Linux系統(tǒng)shell腳本編程的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
1、開發(fā)腳本前準(zhǔn)備
一般大家都知道,測試主機(jī)是否在線,常用的命令無非就是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
好像單純的這種命令是無法來做批量檢查的,必須要帶一些參數(shù),否則它們一直ping下去
[root@centos6 scripts]# ping -W 2 -c 2 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=0.704 ms
64 bytes from 172.16.1.1: icmp_seq=2 ttl=255 time=0.481 ms
--- 172.16.1.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.481/0.592/0.704/0.114 ms
這種方法可以實現(xiàn),測試發(fā)送2個數(shù)據(jù)包,然后加上超時時間,自動停止,可以達(dá)到效果
[root@centos6 scripts]# echo $?
0
[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.
^C
--- 172.16.1.100 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 2836ms
[root@centos6 scripts]# echo $?
1
因此,我們可以通過返回值來判斷是否在線
2、開發(fā)簡單腳本
既然有實現(xiàn)的方法了,那么接下來就開始開發(fā)腳本了
[root@centos6 scripts]# vi checkip.sh
#!/bin/sh
. /etc/init.d/functions
#加載系統(tǒng)函數(shù)庫
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循環(huán)語句
do
$CMD $IP$n >/dev/null 2>&1
#將命令結(jié)果不輸出
if [ $? -eq 0 ];then
#如果返回值為0就表明在線
action "$IP$n is online" /bin/true
#在線就打印此信息
else
#否則就表示不在線
action "$IP$n is not online" /bin/false
#不在線就打印此信息
fi
done
執(zhí)行下腳本看看結(jié)果如何
[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,如果內(nèi)網(wǎng)整個網(wǎng)段IP都手工寫上去,豈不是更費時費力,因此,如果是整個網(wǎng)段,那么定義IP變量時可以定義成這樣IP="172.16.1." ,因為前三位是相同的,寫for 循環(huán)時可以修改成如下
for n in `seq 254`
do
$CMD $IP$n(將兩段數(shù)字拼接成IP地地址)
done
具體這里就不再測試了,有興趣的可以自行測試下
3、開發(fā)nmap腳本檢查在線IP與在線IP的開放端口情況
首先得了解下nmap的一些參數(shù),它也是非常實用的命令之一,在日常實際生產(chǎn)環(huán)境中,經(jīng)常用來檢查IP、端口、URL地址信息,具體其中的參數(shù)這里就不做詳細(xì)介紹了,后續(xù)有時間會分享它的相關(guān)參數(shù)用法
[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
Starting Nmap 5.51 ( http://nmap.org ) at 2016-12-03 21:09 CST
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
從上面的結(jié)果來看,很容易發(fā)現(xiàn)在線與不在線返回的信息不同,但是我們需要取得在線的IP地址信息,那到就只能取 Nmap scan report for 172.16.1.1 ,因為所有在線的IP返回的信息中都會有這一行信息,所以取相同的信息。
[root@centos6 scripts]# nmap -sS 172.16.1.1|grep "Nmap scan report for"
Nmap scan report for 172.16.1.1
[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
Nmap scan report for 172.16.1.1
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
MAC Address: 04:BD:70:FB:A9:B7 (Unknown)
Nmap done: 1 IP address (1 host up) scanned in 8.74 seconds
檢查開啟端口,我們可以通過過濾關(guān)鍵字 open 來實現(xiàn),通過上面的信息很容易觀察出來
[root@centos6 scripts]# nmap -sS 172.16.1.1|grep "open"
21/tcp open ftp
23/tcp open telnet
80/tcp open http
443/tcp open https
[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
#!/bin/sh
. /etc/init.d/functions
#加載系統(tǒng)函數(shù)庫
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手環(huán)語句
do
action "$ip is on line" /bin/true
#打印信息
UPPORT=`$TCMD $ip|grep "open"|awk '{print $1}'`
#定義獲取在線IP的開放端口變量
for port in ${UPPORT}
#二層循環(huán)檢查端口
do
action "$ip $port is open" /bin/true
#將上面在線IP開放的端口信息打印輸出
done
done
注:UPPORT=`$TCMD $ip|grep "open"|awk '{print $1}'` 定義這個變量時,取的IP地址一定要是上一個循環(huán)取出的IP地址,否則會有問題
執(zhí)行腳本,測試效果如何?
[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沒有出現(xiàn)的原因是它不在線
接下來測試下腳本檢查的端口是否正確
[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
Trying 172.16.1.1...
Connected to 172.16.1.1.
Escape character is '^]'.
220 FTP service ready.
^]
telnet> quit
Connection closed.
[root@centos6 scripts]# telnet 172.16.1.2 23
Trying 172.16.1.2...
Connected to 172.16.1.2.
Escape character is '^]'.
TL-AP301C login:
telnet> quit
Connection closed.
從上面的結(jié)果來看,腳本檢查的結(jié)果是正確,如果需要檢查整個網(wǎng)段只需要將定義IP變量時定義成“IP="172.16.1.0/24"”即可
看完了這篇文章,相信你對“Linux系統(tǒng)shell腳本編程的示例分析”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。