主機服務狀態 monitor script 分享

看板MIS (IT資訊人員)作者 (最討厭群組拉人不先問)時間4周前 (2024/09/16 22:15), 編輯推噓1(100)
留言1則, 1人參與, 5天前最新討論串1/1
幾年前有人問,我剛好因為論文寫了一段script , 但最終論文不是這題目 所以就分享出來給有需要的人用,自己也可以改 , 我是在 FreeBSD 12.0 - 14.1 都沒問題, 在其它 unix or unix-like 應該也可以,授權採用 CC BY-SA #!/usr/local/bin/bash chec[Bk_host() { ip=$1 result=`/sbin/ping -W 1 -c 1 $ip | grep 'bytes from '` ping_status=$? nc_result=`nc -z -w 1 $ip 80 > /dev/null 2>&1 ; echo $?` if [ $ping_status -gt 0 ] && [ "$nc_result" == "0" ]; then echo -e "`date +'%Y/%m/%d %H:%M:%S'` - host $ip \033[0;33mPing No echo , Service Active\033[0m" elif [ $ping_status -gt 0 ]; then echo -e "`date +'%Y/%m/%d %H:%M:%S'` - host $ip is \033[0;31mdown\033[0m" else if [ "$nc_result" != "0" ]; then port_result="Service Inactive" else port_result="Service Active" fi echo -e "`date +'%Y/%m/%d %H:%M:%S'` - host $ip is \033[0;32mok\033[0m -`echo $result | cut -d ':' -f 2` $port_resul t" fi } for ip in $(cat /root/ips-home); do check_host $ip & done wait /root/ips-home 192.168.1.1 192.168.1.2 192.168.1.197 192.168.1.198 192.168.1.245 2024/09/16 22:09:05 - host 192.168.1.245 is ok - icmp_seq=0 ttl=64 time=0.098 ms Service Inactive 2024/09/16 22:09:05 - host 192.168.1.197 is ok - icmp_seq=0 ttl=64 time=0.244 ms Service Active 2024/09/16 22:09:05 - host 192.168.1.198 is ok - icmp_seq=0 ttl=64 time=0.234 ms Service Active 2024/09/16 22:09:07 - host 192.168.1.2 Ping No echo , Service Active 2024/09/16 22:09:08 - host 192.168.1.1 is down 需要的人可以按照自已的環境自已再去修改 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.238.52.217 (日本) ※ 文章網址: https://www.ptt.cc/bbs/MIS/M.1726496128.A.13D.html

10/10 10:21, 5天前 , 1F
10/10 10:21, 1F
文章代碼(AID): #1cw3s04z (MIS)
文章代碼(AID): #1cw3s04z (MIS)