shell腳本如下,執行腳本後,會将ping檢測結果輸出到一個ping_monitor.log日志檔案中,腳本接收一個參數,即要檢測的ip位址
#!/bin/sh
MONITOR_LOG=/home/gavin2/scripts/log/ping_monitor.log
ping_monitor()
{
ping -c 3 $1 > /dev/null
if [ $? -eq 0 ];then
echo "`date +"%Y/%m/%d %H:%M:%S"` $1 ping success" >>${MONITOR_LOG} 2>&1
else
echo "`date +"%Y/%m/%d %H:%M:%S"`[error] $1 ping failed" >>${MONITOR_LOG} 2>&1
fi
}
ping_monitor 192.168.1.136