Want to run a script that plays a sound everytime your network connection drops out or comes back online? Use this script to help you with testing your home or office network connection
Today I needed to install a new network cable in one of my rooms, and unfortunately my current switch is not only on the other side of my apartment, but it had run out of ports. Annoyingly I hadn't labelled the cables, so my only option was to unplug the cables one by one and see which one was the one I needed to replace.
My only option was to unplug each cable, go back to the other end of my apartment, and then check if the network connection was still working. This was a very time-consuming process, and I thought there must be a better way to do this. So I created this simple bash script to play a sound everytime the network connection drops out or comes back online, I plugged in some speaker into my Mac and ran the script. This way I could hear when the network connection was back online without having to go back and forth across my entire apartment.
Please note - this script is setup to work on a Mac, but you can easily modify it to work on a Linux or Windows machine.
Copy the code below and save it as something like network-test.sh
on your Mac.
#!/bin/bash # Host to ping HOST="google.com" # Replace with the IP or hostname you want to ping # Path to the success sound file (sound when connection is alive) SUCCESS_SOUND="/System/Library/Sounds/Glass.aiff" # Path to the error sound file (sound when connection is dropped) ERROR_SOUND="/System/Library/Sounds/Basso.aiff" # Variable to track connection status CONNECTION_ALIVE=0 while true; do # Ping the host and check if successful if ping -c 1 $HOST > /dev/null 2>&1; then # If previously disconnected, play reconnect sound and update status if [ $CONNECTION_ALIVE -eq 0 ]; then echo "Connection restored ✅ ✅ ✅ ✅" afplay $SUCCESS_SOUND CONNECTION_ALIVE=1 else # Keep playing the success sound every 1 second afplay $SUCCESS_SOUND fi else # If disconnected, keep playing the error sound every second echo "Connection lost ❌ ❌ ❌ ❌" afplay $ERROR_SOUND CONNECTION_ALIVE=0 fi sleep 1 # Wait 1 second before the next check done
Open a terminal and navigate to the folder where you saved the script. Run the following command to make the script executable:
chmod +x network-test.sh
To run the script, simply type the following command in the terminal:
./network-test.sh
You should now hear a sound every time your network connection drops out or comes back online.
This script is a simple way to test your network connection without having to constantly check your network connection by hand. You can easily modify the script to work on a Linux or Windows machine by changing the sound file paths.