2018年5月5日

Linux ICMP 監控Script

每秒發送1個ICMP封包,加上時間標記並記錄下來
[root@Localhost ~]# vi ping_check.sh
#!/bin/bash

DATE=$(date +"%d.%m.%Y %T")
SHORT_DATE=$(date +"%d.%m.%Y")

echo
echo "Pinging host " $@
echo

HOST=$@


ping $HOST | while read PONG
do
        grep -q ttl <<< "$PONG"

        if [ $? -eq 0 ]; then

                echo "`date`: $PONG"
                echo "`date`: $PONG" &>> ping_check_$SHORT_DATE.log

        else

                echo "`date`: ping failed, $HOST host is DOWN!" &>> ping_check_$SHORT_DATE.log
                echo "$PONG" &>> ping_check_$SHORT_DATE.log
        fi
done