Wednesday, June 10, 2009

[Shell script] Remote Services watchdog

以下的script是針對偵測到對方的service沒有正常運作時則發mail給所指定的使用者,請參考:

#!/bin/bash

SRVLIST='192.168.0.100:80
192.168.0.100:53'
MAILACCOUNT="root@server1.example.com"

for SRV in `echo $SRVLIST`
do
 IP=`echo $SRV | cut -d: -f1`
 PORT=`echo $SRV | cut -d: -f2`
 nc -w 1 $IP $PORT > /dev/null 2>&1 || echo "Port $PORT on Server $IP is failed" | mail -s "Error: Service failed" $MAILACCOUNT
done

No comments: