網管技術
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
#! /bin/bash
# 變數要先宣告後才能使用 shopt -s -o nounset
HostList=${1:?'請提供主機IP列表檔!'}
[ ! -f $HostList ] && echo '主機IP列表檔不存在喔! 請檢查一下先!' && exit 1
# 日期時間 Date=$(date +'%Y%m%d%H%M') Date_for_man=$(date +'%Y-%m-%d %H 時 %M 分')
# ping 的次數 pno=4
# 偵測結果存檔 padir="/var/www/pa" pahtml="$padir/index.html" pahtml_now="$padir/pa-$Date.html"
# # 函式區 #
html_head() { [ ! -e $padir ] && mkdir -p $padir cat <<HEAD > $pahtml_now <html> <head> <title>ping alive 偵測結果</title> <meta HTTP-EQUIV="Refresh" CONTENT="900"> <meta HTTP-EQUIV="Pragma" CONTENT="no-cache"> <meta HTTP-EQUIV="Cache-Control" content="no-cache"> <meta HTTP-EQUIV="Expires" CONTENT="Mon, 01 Jul 2000 06:00:13 GMT"> <meta http-equiv="Content-Type" content="text/html; charset=Big5"> </head> <body bgcolor="white"> <div align=center><font size=6><b>*** 我的主機監控 ***</b></font></div> <div align=center>偵測時間: $Date_for_man</div> <p> <table width="60%" align=center border=3> <tr><td nowrap>主機名稱</td><td>IP</td><td nowrap>最小反應時間</td><td nowrap>最大反應時間</td><td nowrap>平均反應時間</td></tr> HEAD
}
html_tr() { if [ "$1" = "PingError" ]; then cat <<TR >> $pahtml_now <tr><td>$host</td><td>$ip</td><td colspan=3><font color=red><b>無法連通!!!</b></font></td></tr> TR
else cat <<TR >> $pahtml_now <tr><td>$host</td><td>$ip</td><td>$rt_min ms</td><td>$rt_max ms</td><td>$rt_avg ms</td></tr> TR
fi }
html_end() { cat <<END >> $pahtml_now </table> </body> </html> END
ln -sf $pahtml_now $pahtml }
# # 主程式區 #
# 建立偵測結果網頁的檔頭 html_head
# 處理迴圈 #----------------------------------------------------------------------- while read host ip do rt_min= rt_avg= rt_max= while read r do if [[ $r == rtt* ]]; then rt_min=$(echo $r | awk '{print $4}' | awk -F/ '{print $1}') rt_avg=$(echo $r | awk '{print $4}' | awk -F/ '{print $2}') rt_max=$(echo $r | awk '{print $4}' | awk -F/ '{print $3}') html_tr $rt_min $rt_avg $rt_max fi done < <(ping -c $pno $ip) if [ -z $rt_min ]; then html_tr PingError fi done < <(cat $HostList) #-----------------------------------------------------------------------
# 建立偵測結果網頁的檔尾 html_end
|
主機IP列表檔格式
主機名稱 IP
例如:
1 |
localhost 127.0.0.1 |