Free tech learning for everyone, from basics to pro

ScriptingProgramming and Application SecurityBeginner

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.

A network check scriptRead targetshosts.txtTestping, port,DNSRecordCSV or logDecideany failures?Reportexit code,alert
Figure 1. Most network check scripts follow the same pattern: read a target list, test each one, record results, then report.

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

Quick checks and their toolsReachabilitypingTCP portnc -z, bash /dev/tcpDNSdig +shortWeb servicecurl -sIPathtraceroute, mtrTLSopenssl s_client
Figure 2. Each type of check maps to a standard Linux tool you can call from a script.

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

  1. Keep one target file per site or role, for example core.txt and cctv.txt.
  2. Use short timeouts so one dead host does not stall the run.
  3. Write a dated CSV so you can compare today with last week in a spreadsheet.
  4. Return a non-zero exit code when something fails so a scheduler or monitoring tool can alert.
  5. 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?
Because failed pings are expected results, not errors. With set -e the script would stop at the first unreachable host.
How can you test a TCP port without nc?
Use the Bash /dev/tcp feature inside timeout, for example timeout 3 bash -c "</dev/tcp/host/443", and check the exit code.
What is a good use of before-and-after CSV files?
During a change window: comparing them shows exactly which devices or services changed state because of the change.

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.
Tracks your learning on this device only. See your progress on the learning paths.

About the author

Mohamed Fayas

Founder of FM TecHub. CCNA certified, with a BTEC HND in Network Engineering, and BEng (Hons) in Computer Networking and Cloud Security. He writes practical, plain-English guides on networking, security and cloud.

More about the author

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.

Stay up to date by email

New notes, tools and security news from FM TecHub, explained simply. Free.

We only use your email to send these updates. See our privacy policy.