Useful Bash scripts for network checks
In short: A few short Bash scripts can replace repetitive manual checks: pinging a list of devices, testing service ports, confirming DNS answers and checking web endpoints. Keep targets in a file, log results with a timestamp, and only test networks you are responsible for.
The pattern behind every check
Network checks look different on the surface, but they share one shape. You keep a list of targets in a text file, loop over it, run a test against each one, write down the result and finish with a summary. Keeping targets in a file means a colleague can add a new switch without editing code.
Only run these checks against networks and devices you manage or have written permission to test. Even a simple ping sweep or port check can trigger alerts, and scanning other people's systems may break the law.
Reachability sweep with a CSV report
Create hosts.txt with one IP address or hostname per line, then run this script.
#!/usr/bin/env bash
set -uo pipefail
TARGETS="${1:-hosts.txt}"
OUT="reach_$(date +%Y%m%d_%H%M).csv"
fail=0
echo "timestamp,host,status" > "$OUT"
while read -r host; do
[[ -z "$host" || "$host" == \#* ]] && continue
if ping -c 2 -W 2 "$host" > /dev/null 2>&1; then
status="up"
else
status="down"; fail=$((fail + 1))
fi
echo "$(date '+%F %T'),$host,$status" >> "$OUT"
done < "$TARGETS"
echo "Done: $fail host(s) down. Results in $OUT"
exit $(( fail > 0 ? 1 : 0 ))
Notice that set -e is left out here on purpose: a failing ping is expected, so the script handles it in the if instead of stopping. Blank lines and lines starting with # are skipped so you can comment the host file.
Ports, DNS and web checks
The commands below are the building blocks. Drop them into the same loop pattern.
# Is TCP 22 open on a switch? (-z: just test, -w: timeout seconds)
nc -z -w 3 192.168.10.2 22 && echo "ssh open"
# Pure Bash TCP test when nc is not installed
timeout 3 bash -c '</dev/tcp/192.168.10.2/443' && echo "443 open"
# Does the name resolve to the expected address?
expected="192.168.10.50"
actual=$(dig +short intranet.example.com A | head -n1)
[[ "$actual" == "$expected" ]] || echo "DNS mismatch: got '$actual'"
# HTTP status code of a health endpoint
code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 https://intranet.example.com/health)
[[ "$code" == "200" ]] || echo "web check failed: $code"
Making checks useful at work
- Keep one target file per site or role, for example
core.txtandcctv.txt. - Use short timeouts so one dead host does not stall the run.
- Write a dated CSV so you can compare today with last week in a spreadsheet.
- Return a non-zero exit code when something fails so a scheduler or monitoring tool can alert.
- Schedule the script with cron and keep it in Git so changes are tracked.
A common mistake is to treat a failed ping as proof that a device is down. Many firewalls, servers and cloud hosts block ICMP (Internet Control Message Protocol) by design, so a host can be perfectly healthy while ignoring ping. Where that is the case, test the service the users actually need, such as TCP 443 on a web server or TCP 22 on a switch management address. The reverse also happens: a device can answer ping while its main service has crashed, which is why an HTTP status check is more meaningful than reachability for applications. Match each check to the question you are really asking.
Tip: these scripts complement, not replace, a proper monitoring system. They are perfect for change windows: run the sweep before and after a change and compare the two CSV files.
Practice
Why does the sweep script avoid set -e?
How can you test a TCP port without nc?
timeout 3 bash -c "</dev/tcp/host/443", and check the exit code.What is a good use of before-and-after CSV files?
Key takeaways
- Keep targets in a file and loop over them.
- Use ping, nc, dig and curl with short timeouts.
- Write timestamped CSV results for later comparison.
- Exit non-zero on failure so schedulers can alert.
- Only check networks you are authorised to test.
Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.