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