Have you ever needed to verify if your remote servers have outbound internet access? Whether you're managing a cluster of web servers or a set of edge devices, ensuring they can "talk to the world" is a fundamental task.
In this post, I'll walk you through a lightweight Network Connectivity Monitoring Tool I built using Bash. It's simple, portable, and comes with a Docker-based test environment to get you started safely.
The Problem
Manually SSH-ing into dozens of servers to run a ping command is tedious and error-prone. I needed a way to:
- Check connectivity from multiple servers at once.
- Support non-standard SSH ports.
- Log results for historical tracking.
- Get a quick summary of which hosts are "Reachable" vs "Unreachable."
The Solution: A Bash-Powered Monitor
The core of this tool is a Bash script that uses SSH to execute a ping command on remote targets. Here's a look at the key features and how it works.
1. Robust Scripting with set -euo pipefail
To make the script reliable, I used strict error handling:
-
-e: Exit immediately if a command fails. -
-u: Treat unset variables as errors. -
-o pipefail: Ensure that if any part of a pipeline fails, the whole pipeline fails.
2. Intelligent Connectivity Checks
The script doesn't just check if the server is "up"; it distinguishes between an SSH failure (can't connect to the box) and a Network failure (connected to the box, but the box can't reach the internet).
# A snippet of the core logic
if ssh -o StrictHostKeyChecking=no -o BatchMode=yes -p "${port}" "${ssh_target}" \
"ping -c 3 8.8.8.8 > /dev/null 2>&1"; then
log_result "$host" "REACHABLE" "$TIMESTAMP"
echo " ✓ $host - REACHABLE"
else
log_result "$host" "UNREACHABLE" "$TIMESTAMP"
echo " ✗ $host - UNREACHABLE (network issue)"
fi
3. Automated Logging & Summaries
Every run generates a timestamped log in connectivity_log.txt, making it easy to spot patterns over time. At the end of each run, you get a clean summary:
========================================
Summary
========================================
Total hosts checked: 3
Reachable: 2
Unreachable: 1
========================================
Testing Safely with Docker
One of the best parts of this project is the included Test Lab. Using docker-compose, you can spin up three Ubuntu-based SSH servers locally to test the script without risking your production environment.
I've included a setup.sh script that:
- Starts the containers.
- Installs
ping(not present in minimal Ubuntu images). - Configures SSH keys for passwordless login.
- Validates the environment.
How to Try It Out
- Clone the project.
- Generate an SSH key if you haven't already:
ssh-keygen -t rsa. -
Launch the test lab:
./setup.sh -
Run the monitor:
./monitor_connectivity.sh
Lessons Learned
While building this, I ran into a few classic Bash "gotchas":
-
Loops vs
set -e: Using awhile readloop withset -ecan cause the script to exit prematurely when it hits the end of a file. Switching to aforloop withgrepsolved this. -
Docker Ephemerality: Since containers reset on restart, I automated the SSH key injection into the
setup.shscript to ensure a smooth developer experience.
Conclusion
Sometimes, the simplest tools are the most effective. This Bash script provides a no-nonsense way to monitor network health across your infrastructure.
Check out the full source code here! github
What are your favorite "mini-tools" for server management? Let me know in the comments!
Top comments (0)