Ping Test Script

2018-04-21

I recently was experiencing significant packet loss on my network over the past week. I need to be able to track this information as I work from home and needed to prove to both my employer and my ISP that this was a problem. So I set out to create an automated solution that allowed me to have a historical record of each time I experienced packet loss. This is what the process looks like.

First off, I needed to have the results run at a regular interval so I could make sure that I didn’t miss any momentary dropouts. To accomplish this, I setup a very basic bash script called “pingtest”:

#!/bin/bash

date > ping.txt
ping -qc 100 -i .2 8.8.8.8 >> ping.txt

(If you’re following along, make sure it’s executable: $ chmod +x pingtest) This produces an output like this:

Fri Apr 13 11:45:01 MST 2018
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.

--- 8.8.8.8 ping statistics ---
100 packets transmitted, 100 received, 0% packet loss, time 19863ms
rtt min/avg/max/mdev = 20.324/22.223/28.589/1.572 ms

I then run this every 5 minutes by running $ crontab -e and adding the following line:

*/5 * * * * ~/pingtest

Again, if you’re following along, this next step gets a bit involved, so I’m not going into a ton of detail because I already have this web server running. But in short, I created a symlink from the file in my home directory to a page on the web server ($ sudo ln -s ~/ping.txt /var/www/html/ping.txt). This allows me ㅡ or technically anyone else ㅡ to access the ping test results from anywhere.

This is where things got interesting. I’m not an Android developer at all and I needed the ability to be notified of any packet loss. This is where one of my favorite apps, MacroDroid, comes in. This app is sort of like Tasker Light, but only in terms of UI. It’s an incredibly powerful toolset that allows you to create simple scripted actions based on triggers. The final script I created looks like this:

Ping Test Macro

If you already have the app, you can download the completed script here, although you will obviously need to make changes to things like server names.

Step-by-step, this is what the script is doing:

Other Articles