Bash connection monitoring with ping and tracert
My mom was complaining of her internet going out tonight, so I wrote a quick bash script to monitor her pings.
The script infinitely pings the target and logs the successes (or failures) into mom.log. No, you CAN’T change that log name. Okay, okay.. just kidding you can.
When the script detects a failure, it starts a tracert which it dumps to the same file. The result should be a list of successes and failures with tracerts for each failure. Take note this won’t work at all if pings are suppressed on the host you’re trying to monitor.
#!/bin/bash ip=220.127.116.11 while [ 1 ]; do ping -c 1 $ip 1> /dev/null result=$? if [ $result == "1" ]; then echo FAIL on `date` >> ~/mom.log echo FAIL on `date`. Doing tracert! tracert $ip >> ~/mom.log fi if [ $result == "0" ]; then echo SUCCEED on `date` >> ~/mom.log echo SUCCEED on `date` fi sleep .4 done
Save that code into something.sh and then
chmod +x it.
You’ll want to run this in a screen so its not dependent on your keeping your shell window open at all times. You can do that by running this command before you start the script:
screen -S pingtest
control-a then the letter d to detach the screen so it resumes running in the background.
Later, when you want to stop the script, run this:
screen -r pingtest
Sometimes simple things work well enough to make a note of!