原帖:
http://bbs.linuxtone.org/redirect.php?tid=3325&goto=lastpost
腳本借助了
撫琴煮酒 的
測(cè)試局域網(wǎng)內(nèi)主機(jī)是否alive的小腳本
http://bbs.linuxtone.org/thread-2065-1-1.html
新加入了飛信免費(fèi)發(fā)短信API接口(可以自己給自己發(fā)短信,完全免費(fèi))
把ping不同的ip地址寫到一個(gè)
文件里面,并去判斷這個(gè)文件是否為空來(lái)實(shí)現(xiàn)報(bào)警功能
#!/bin/bash
cat /dev/null >/usr/local/sbin/51edu.txt
for n in {66..75}; do
host=192.168.0.$n
ping -c5 $host &>/dev/null
if [ $? = 0 ]; then
echo "$host is up" >/dev/null
else
echo "$host" >>/usr/local/sbin/51edu.txt
if [[ -s /usr/local/sbin/51edu.txt ]];then
DOWN=`cat /usr/local/sbin/51edu.txt|paste -s -d ','`
curl "http://sms.api.bz/fetion.php?username=1501*******&password=******& amp;sendto=1501*******&message=$DOWN服務(wù)器down機(jī),請(qǐng)盡快處理!"
fi
fi
done
更簡(jiǎn)潔:
#!/bin/bash
#Checks to see if hosts 192.168.100.1-192.168.100.16 are alive
#$?輸出命令退出代碼:0為命令正常執(zhí)行,1-255為有出錯(cuò)
for n in {1..16}; do
host=192.168.100.$n
ping -c2 $host &>/dev/null
if [ $? = 0 ]; then
echo "$host is UP"
else
echo "$host is DOWN"
curl "http://sms.api.bz/fetion.php?username=135****&password=***&sendto=135***&message=$host服務(wù)器down機(jī),請(qǐng)盡快處理!"
fi
done