Monitor Network Connectivity with a Bash Script

Monitoring network connectivity is essential for both individual users and businesses. A reliable connection can affect everything from simple browsing to extensive business operations. In this article, you will learn how to create a lightweight Bash script to monitor network connectivity, check server uptime, and log connection statuses. This is an ideal solution for troubleshooting internet issues or ensuring remote services remain online.

Prerequisites

  • A basic understanding of Bash scripting
  • Knowledge of ping, curl, and other networking commands
  • A Linux or MacOS environment with Bash installed

DID YOU KNOW?

Monitoring your connection can help identify issues before they affect your productivity. A script can automate this process at regular intervals.

The Script

This simple Bash script will check the connectivity of a specified server and log the result. The script uses the ping command to test connectivity and records the status with a timestamp in a log file.

#!/bin/bash
SERVER="8.8.8.8"
LOGFILE="connectivity.log"

while true; do
    if ping -c 1 $SERVER > /dev/null; then
        echo "$(date) - $SERVER is UP" >> $LOGFILE
    else
        echo "$(date) - $SERVER is DOWN" >> $LOGFILE
    fi
    sleep 60
done

Step-by-Step Explanation

NOTE!

Make sure to give the script executable permissions by running chmod +x script_name.sh after saving it.

Let’s break down the script:

  1. Define Variables: Here, you set the SERVER variable to the IP address of the server you want to monitor, and the LOGFILE variable where logs will be saved.
  2. Continuous Monitoring: The script runs an infinite loop that performs the connectivity check every 60 seconds.
  3. Ping and Log Status: The ping command checks the connection, and based on the result, it appends a log entry with the current date and status.
  4. Add any additional features, such as sending email notifications if the server goes down.

How to Run the Script

To execute the script, follow these steps:

  1. Open your terminal.
  2. Navigate to the directory where you saved the script.
  3. Run the script using ./script_name.sh.

Conclusion

By creating and running this simple Bash script, you can easily monitor network connectivity and ensure that your critical services are online. This can significantly improve your ability to troubleshoot internet issues proactively.

FAQ

  1. What is the purpose of the ping command in the script?

    The ping command is used to check the connectivity status of a specified server by sending ICMP echo requests.

  2. How can I check the log file?

    You can view the log file using a text editor or the cat command, like this: cat connectivity.log.

  3. Can the script monitor multiple servers?

    Yes, you can modify the script to loop through an array of servers instead of a single one.

  4. What if I want to stop the script?

    You can stop the script by pressing Ctrl + C in the terminal where it is running.

  5. Can I run the script in the background?

    Yes, you can run it in the background by appending & at the end of the command: ./script_name.sh &.

Troubleshooting

If you encounter issues while running your script, consider the following common problems:

  • Permission Denied: If you see a permission error, make sure to run chmod +x script_name.sh to make your script executable.
  • Ping Command Not Found: Ensure that the iputils-ping package is installed on your system.
  • Log File Not Found: If the log file is not being created, check the directory where you are running the script for any errors in path or permissions.