Skip to content

Instantly share code, notes, and snippets.

@pacmac
Created April 24, 2020 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pacmac/4b926914afc422b851a1fbc007c78f05 to your computer and use it in GitHub Desktop.
Save pacmac/4b926914afc422b851a1fbc007c78f05 to your computer and use it in GitHub Desktop.
Ping a Host & Save Status to Log File
#!/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