Created
April 24, 2020 23:54
-
-
Save pacmac/4b926914afc422b851a1fbc007c78f05 to your computer and use it in GitHub Desktop.
Ping a Host & Save Status to Log File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
STAMP=`date "+%y%m%d-%H:%M:%S"` | |
FILE="/var/log/ping.log" | |
ISUP="DOWN" | |
_update () { | |
if [ ! -f "$FILE" ]; then | |
echo "$STAMP $ISUP" > $FILE | |
fi | |
LAST=$( tail -n 1 $FILE ) | |
if [[ $LAST == *"$ISUP"* ]]; then | |
echo "No Change: $ISUP" | |
else | |
echo "Changed: $ISUP"; | |
echo "$STAMP $ISUP" >> $FILE | |
fi | |
} | |
_ping () { | |
ping -c1 $1 > /dev/null | |
if [ $? -eq 0 ] | |
then | |
echo ok | |
ISUP="UP" | |
else | |
echo "fail" | |
ISUP="DOWN" | |
fi | |
} | |
_ping $1; | |
_update; | |
cat $FILE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment