SHELL腳本-實現(xiàn)服務(wù)器IP和Port連通性的自動監(jiān)控

1.創(chuàng)建目錄"Services-monitor",并在該目錄中創(chuàng)建目錄"FileFromRemoteServices"球涛、文件"main.sh"、文件"downloadFileFromRemoteServices.sh"、文件"sshAndPortTest.sh"褥紫、文件"ServicesList.txt"、文件"crontabStart.sh"瞪慧、文件"crontabQuery.sh"髓考、文件"crontabStop.sh"、文件"userStart.sh"弃酌。


2."main.sh"文件內(nèi)容如下:

#!/bin/bash

#腳本每次運行時氨菇,清空ProgramResult.txt文件和ServicesError.txt文件的內(nèi)容

cat /dev/null > /LinuxScript/Services-monitor/ProgramResult.txt

cat /dev/null > /LinuxScript/Services-monitor/ServicesError.txt

startTime=`date`

echo"" >> /LinuxScript/Services-monitor/ProgramResult.txt

echo "腳本開始運行儡炼,運行時間:$startTime" >> /LinuxScript/Services-monitor/ProgramResult.txt

echo"" >> /LinuxScript/Services-monitor/ProgramResult.txt

#servicesList_line變量用于存儲read命令從文件(servicesList.txt)中讀取出的數(shù)據(jù)(read命令逐行讀取數(shù)據(jù))

while read servicesList_line

do

? ? ? ? #for語句以"空格"為分隔符,逐個讀取行中的參數(shù)查蓉。

? ? ? ? #ip_or_port變量用于存儲IP地址或端口號

? ? ? ? for? ip_or_port in $servicesList_line

? ? ? ? ? ? ? ? then

? ? ? ? ? ? ? ? ? ? ? ? #如果讀取到的是IP地址乌询,賦給變量"ip"。

? ? ? ? ? ? ? ? ? ? ? ? ip=$ip_or_port

? ? ? ? ? ? ? ? ? ? ? ? #通過PING命令測試目標(biāo)主機網(wǎng)絡(luò)連通性豌研,并通過黑洞文件處理命令返回信息妹田。

? ? ? ? ? ? ? ? ? ? ? ? echo "正在測試主機與服務(wù)器$ip的連通性..." >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? ? ? ? ? ? ? ? ? ping -c 2 $ip > /dev/null

? ? ? ? ? ? ? ? ? ? ? ? then

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? echo "服務(wù)器$ip網(wǎng)絡(luò)正常。" >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? ? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? ? ? ? ? fi

? ? ? ? ? ? ? ? elif [[ $ip_or_port =~ ^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$ ]]

? ? ? ? ? ? ? ? then

? ? ? ? ? ? ? ? ? ? ? ? #如果讀取到的是端口號聂沙,賦給數(shù)組"port"秆麸。

? ? ? ? ? ? ? ? ? ? ? ? let portNum++

? ? ? ? ? ? ? ? ? ? ? ? port[$portNum]=$ip_or_port

? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? ? ? ? ? cat /dev/null > /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? ? ? ? ? ? ? ? ? echo "ServicesList.txt文件中存在錯誤的IP地址或端口號,錯誤參數(shù)為\"$ip_or_port\"及汉。" >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? ? ? ? ? ? ? ? ? echo "錯誤沮趣,程序已退出" >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? ? ? ? ? ? ? ? ? exit

? ? ? ? ? ? ? ? fi

? ? ? ? done

? ? ? ? echo "正在測試服務(wù)器$ip的端口連通性..." >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? #調(diào)用sshAndPortTest腳本,登錄目標(biāo)服務(wù)器檢測服務(wù)器端口狀態(tài)坷随,并生成狀態(tài)文件(于目標(biāo)服務(wù)器)房铭。該腳本執(zhí)行時間:4s-7s。

? ? ? ? expect /LinuxScript/Services-monitor/sshAndPortTest.sh $ip ${port[*]}

? ? ? ? #調(diào)用downloadFileFromRemoteServices腳本温眉,將目標(biāo)服務(wù)器生成的狀態(tài)文件下載到本地缸匪,本地存儲路徑為"/LinuxScript/Services-monitor/FileFromRemoteServices"。

? ? ? ? expect /LinuxScript/Services-monitor/downloadFileFromRemoteServices.sh $ip

? ? ? ? if [ -e /LinuxScript/Services-monitor/FileFromRemoteServices/PortStatuBuffer.txt ]? ? #判斷PortStatuBuffer文件是否存在

? ? ? ? #如果PortStatuBuffer文件存在类溢,將"port"數(shù)組中的端口號與文件中的內(nèi)容進行匹配凌蔬。

? ? ? ? then

? ? ? ? ? ? ? ? for portCheck in ${port[*]}

? ? ? ? ? ? ? ? do

? ? ? ? ? ? ? ? ? ? ? ? cat /LinuxScript/Services-monitor/FileFromRemoteServices/PortStatuBuffer.txt | awk '{print $4}' | grep -w "0\.0\.0\.0\:$portCheck$\|$ip\:$portCheck$\|\:\:\:$portCheck$" >> /dev/null

? ? ? ? ? ? ? ? ? ? ? ? if [ $? -eq 0 ]

? ? ? ? ? ? ? ? ? ? ? ? then

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? echo "服務(wù)器$ip端口$portCheck正常。" >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? ? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? echo "服務(wù)器$ip端口$portCheck異常闯冷。異常原因:$portCheck端口未開放或端口意外關(guān)閉砂心。" >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? echo "服務(wù)器$ip端口$portCheck異常。異常原因:$portCheck端口未開放或端口意外關(guān)閉蛇耀,未在該服務(wù)器生成的PortStatuBuffer文件中檢測到端口$portCheck" >> /LinuxScript/Services-monitor/ServicesError.txt

? ? ? ? ? ? ? ? ? ? ? ? fi

? ? ? ? ? ? ? ? done

? ? ? ? else

? ? ? ? ? ? ? ? echo "服務(wù)器$ip端口檢測異常辩诞。異常原因:服務(wù)器$ip網(wǎng)絡(luò)異常或密碼錯誤纺涤,也可能是該服務(wù)器不存在译暂。" >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? ? ? ? ? echo "服務(wù)器$ip端口檢測異常。異常原因:服務(wù)器$ip網(wǎng)絡(luò)異沉么叮或密碼錯誤外永,也可能是該服務(wù)器不存在。無法下載該服務(wù)器生成的PortStatuBuffer文件拧咳。" >> /LinuxScript/Services-monitor/ServicesError.txt

? ? ? ? fi

? ? ? ? #刪除port數(shù)組象迎,避免數(shù)組中殘留的數(shù)據(jù)影響下一次循環(huán)的結(jié)果。

? ? ? ? unset port

? ? ? ? #刪除本次服務(wù)器檢測生成的狀態(tài)文件呛踊,避免對下一次檢測產(chǎn)生影響砾淌。

? ? ? ? rm -f /LinuxScript/Services-monitor/FileFromRemoteServices/PortStatuBuffer.txt

done < /LinuxScript/Services-monitor/ServicesList.txt

endTime=`date`

echo "腳本結(jié)束運行,結(jié)束時間:$endTime" >> /LinuxScript/Services-monitor/ProgramResult.txt

#判斷ServicesError文件是否為空谭网。如果ServicesError文件為空汪厨,說明指定的遠端主機均具備連通性,遠端主機指定的端口也均已開放愉择,在屏幕上輸出指定信息劫乱;如果ServicesError文件不為空,說明存在遠端主機不具備連通性或遠端主機指定端口未開放/意外關(guān)閉的情況锥涕,在屏幕輸出文件中的信息衷戈。

if [ -s /LinuxScript/Services-monitor/ServicesError.txt ]

then

? ? ? ? cat /dev/null > /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? echo"" >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? echo "腳本開始時間:$startTime" >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? echo "腳本結(jié)束時間:$endTime" >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? cat /LinuxScript/Services-monitor/ServicesError.txt >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? echo "按ctrl+c退出" >> /LinuxScript/Services-monitor/ProgramResult.txt

else

? ? ? ? cat /dev/null > /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? echo"" >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? echo "腳本開始時間:$startTime" >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? echo "腳本結(jié)束時間:$endTime" >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? echo "所有服務(wù)器、服務(wù)器端口正常" >> /LinuxScript/Services-monitor/ProgramResult.txt

? ? ? ? echo "按ctrl+c退出" >> /LinuxScript/Services-monitor/ProgramResult.txt

fi


3."sshAndPortTest.sh"文件內(nèi)容如下:

#!/bin/expect

#執(zhí)行該腳本需要傳入兩個參數(shù):IP地址和端口號

#定義ipAddress變量层坠,用于接收外層腳本傳遞的IP地址殖妇。

set ipAddress [lindex $argv 0]

#定義ipAddress變量,用于記錄外層腳本傳遞參數(shù)的個數(shù)破花。

set portNum [lindex $argc]

#定義變量i谦趣,用作循環(huán)參數(shù)

set i 1

#開啟一個交互進程,用于實現(xiàn)ssh登錄遠端主機座每。

spawn ssh "root@$ipAddress"

#為timeout變量賦值前鹅,重新指定expect命令匹配字符串的超時時間。

#將超時時間設(shè)為4s峭梳,避免在ssh返回重驗證消息前舰绘,expect命令就已經(jīng)匹配超時。

set timeout 4

#匹配遠端主機的ssh驗證消息,并發(fā)送相應(yīng)指令抓于,從而實現(xiàn)ssh自動驗證登錄味混。注:如果匹配超時,不發(fā)送任何指令者蠕。

expect {

"connecting" { send "yes\r";exp_continue }

"password:" { send "密碼\r";exp_continue }

"again" { send "密碼\r"; }

timeout { }

}

#休眠2s,避免之后的命令在登錄到遠端主機前就已經(jīng)被執(zhí)行掐松。

sleep 2

#在目標(biāo)主機創(chuàng)建PortStatuBuffer.txt文件踱侣,用于臨時存儲netstat命令執(zhí)行的結(jié)果。

send "mkdir -p /root/Services-monitor_client\r"

send "touch /root/Services-monitor_client/PortStatuBuffer.txt\r"

send "cat /dev/null > /root/Services-monitor_client/PortStatuBuffer.txt\r"

#對外層腳本傳遞的端口號進行端口狀態(tài)的查詢大磺,并將查詢結(jié)果放入PortStatuBuffer.txt文件抡句。

while { $i < $portNum } {

? ? ? ? send "netstat -anl | grep -w \"0\.0\.0\.0\:\*\\\|\:\:\:\*\" | grep -w \"0\.0\.0\.0\:[lindex $argv $i]\\\|$ipAddress\:[lindex $argv $i]\\\|\:\:\:[lindex $argv $i]\" >> /root/Services-monitor_client/PortStatuBuffer.tx

t\r"

? ? ? ? incr i

}

#休眠1s,避免命令還未執(zhí)行完杠愧,腳本就已經(jīng)退出

sleep 1

exit

#命令解釋:

#休眠2s待榔,避免之后的命令在連接到遠端主機前就已經(jīng)被執(zhí)行。

#sleep 2

#在遠端主機執(zhí)行命令。

#檢測遠端主機端口是否對外開放锐锣,將netstat查詢到的記錄寫入PortStatuBuffer文件(文件目錄為/Services-monitor_client/PortStatuBuffer.txt)腌闯。

#send "mkdir -p /root/Services-monitor_client\r"? ? 注:通過"-p"參數(shù)可以實現(xiàn)文件夾的遞歸創(chuàng)建,且即便目標(biāo)文件夾存在雕憔,也不會報錯姿骏。

#send "touch /root/services-monitor_client/PortStatuBuffer.txt\r"? ? 注:如果文件不存在,創(chuàng)建該文件斤彼;如果文件存在分瘦,刷新該文件的mtime,且不會清空文件中已有的內(nèi)容琉苇。


4."downloadFileFromRemoteServices.sh"文件內(nèi)容如下:

#!/bin/expect

#執(zhí)行該腳本需要傳入一個參數(shù):IP地址

#定義ipAddress變量嘲玫,用于接收外層腳本傳遞的IP地址。

set ipAddress [lindex $argv 0]

#開啟一個交互進程并扇,用于實現(xiàn)scp下載遠端主機文件去团,并將其存放至本地文件夾/root/Services-monitor/FileFromRemoteServices。

spawn scp "$ipAddress:/root/Services-monitor_client/PortStatuBuffer.txt" "/LinuxScript/Services-monitor/FileFromRemoteServices"

#將超時時間設(shè)為4s拜马,避免在ssh返回重驗證消息前渗勘,expect命令就已經(jīng)匹配超時。

set timeout 4

#匹配遠端主機的ssh驗證消息俩莽,并發(fā)送相應(yīng)指令旺坠,從而實現(xiàn)scp自動驗證。注:如果匹配超時扮超,不發(fā)送任何指令取刃。

expect {

"password:" { send "密碼\r";exp_continue }

"again" { send "密碼\r"; }

timeout { }

}

#休眠1s,避免文件還沒下載腳本就已經(jīng)退出出刷。

sleep 1

exit

#注:

#如果遠端服務(wù)器找不到對應(yīng)的文件璧疗,腳本不會報錯,且不會在本地主機產(chǎn)生文件馁龟。

#如果遠端服務(wù)器連接不上(不存在或密碼不正確)崩侠,腳本不會報錯,且不會在本地主機產(chǎn)生文件坷檩。


5."crontabStart.sh"文件內(nèi)容如下:

#!/bin/bash

echo "0 */1 * * * /bin/bash /LinuxScript/Services-monitor/main.sh" > /var/spool/cron/root


6."crontabQuery.sh"文件內(nèi)容如下:

#!/bin/bash

crontab -l


7."crontabStop.sh"文件內(nèi)容如下:

#!/bin/bash

crontab -r


8."userStart.sh"文件內(nèi)容如下:

#!/bin/bash

clear

tail -f ProgramResult.txt

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末却音,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子矢炼,更是在濱河造成了極大的恐慌系瓢,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,464評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件句灌,死亡現(xiàn)場離奇詭異夷陋,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評論 3 399
  • 文/潘曉璐 我一進店門骗绕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來藐窄,“玉大人,你說我怎么就攤上這事爹谭〖闲埃” “怎么了?”我有些...
    開封第一講書人閱讀 169,078評論 0 362
  • 文/不壞的土叔 我叫張陵诺凡,是天一觀的道長。 經(jīng)常有香客問我践惑,道長腹泌,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,979評論 1 299
  • 正文 為了忘掉前任尔觉,我火速辦了婚禮凉袱,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘侦铜。我一直安慰自己专甩,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,001評論 6 398
  • 文/花漫 我一把揭開白布钉稍。 她就那樣靜靜地躺著涤躲,像睡著了一般。 火紅的嫁衣襯著肌膚如雪贡未。 梳的紋絲不亂的頭發(fā)上种樱,一...
    開封第一講書人閱讀 52,584評論 1 312
  • 那天,我揣著相機與錄音俊卤,去河邊找鬼嫩挤。 笑死,一個胖子當(dāng)著我的面吹牛消恍,可吹牛的內(nèi)容都是我干的岂昭。 我是一名探鬼主播,決...
    沈念sama閱讀 41,085評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼狠怨,長吁一口氣:“原來是場噩夢啊……” “哼约啊!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起取董,我...
    開封第一講書人閱讀 40,023評論 0 277
  • 序言:老撾萬榮一對情侶失蹤棍苹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后茵汰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體枢里,經(jīng)...
    沈念sama閱讀 46,555評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,626評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了栏豺。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片彬碱。...
    茶點故事閱讀 40,769評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖奥洼,靈堂內(nèi)的尸體忽然破棺而出巷疼,到底是詐尸還是另有隱情,我是刑警寧澤灵奖,帶...
    沈念sama閱讀 36,439評論 5 351
  • 正文 年R本政府宣布嚼沿,位于F島的核電站,受9級特大地震影響瓷患,放射性物質(zhì)發(fā)生泄漏骡尽。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,115評論 3 335
  • 文/蒙蒙 一擅编、第九天 我趴在偏房一處隱蔽的房頂上張望攀细。 院中可真熱鬧,春花似錦爱态、人聲如沸谭贪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽俭识。三九已至,卻和暖如春吆豹,著一層夾襖步出監(jiān)牢的瞬間鱼的,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評論 1 274
  • 我被黑心中介騙來泰國打工痘煤, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留凑阶,地道東北人。 一個月前我還...
    沈念sama閱讀 49,191評論 3 378
  • 正文 我出身青樓衷快,卻偏偏與公主長得像宙橱,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子蘸拔,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,781評論 2 361

推薦閱讀更多精彩內(nèi)容