In my school, the internet is not available(every night after 23:0 the school will kill the internet, to put us in bed >..<), then the ping will never stop, though I have use
#!/bin/bash
INTERNET_STATUS="UNKNOWN"
TIMESTAMP=`date +%s`
while [ 1 ]
do
ping -c 1 -W 0.7 8.8.4.4 > /dev/null 2>&1
if [ $? -eq 0 ] ; then
if [ "$INTERNET_STATUS" != "UP" ]; then
echo "UP `date +%Y-%m-%dT%H:%M:%S%Z` $((`date +%s`-$TIMESTAMP))";
INTERNET_STATUS="UP"
fi
else
if [ "$INTERNET_STATUS" = "UP" ]; then
echo "DOWN `date +%Y-%m-%dT%H:%M:%S%Z` $((`date +%s`-$TIMESTAMP))";
INTERNET_STATUS="DOWN"
fi
fi
sleep 1
done;
the output will produce smth like:
bash-3.2$ ./internet_check.sh
UP 2016-05-10T23:23:06BST 4
DOWN 2016-05-10T23:23:25BST 19
UP 2016-05-10T23:23:32BST 7
the number in the end of a line shows duration of previous state, i.e. 19 up, 7 secs down