BASH
Checking Multiple Network Port Availability on Localhost
A bash script to check the availability of a list of specified network ports on the local machine using `nc` (netcat), useful for diagnosing service issues.
#!/bin/bash
# Script to check if a list of ports are open on localhost
# Array of ports to check
declare -a PORTS=(80 443 3000 5000 8080 3306 6379 27017)
HOST="127.0.0.1" # Localhost
echo "Checking network port availability on $HOST..."
echo "-------------------------------------"
for PORT in "${PORTS[@]}"; do
echo -n "Checking port $PORT... "
# Use netcat (nc) to check if the port is open
# -z: zero-I/O mode (just scan for listening daemons)
# -w 1: timeout after 1 second
if nc -z -w 1 "$HOST" "$PORT" &>/dev/null; then
echo "OPEN"
else
echo "CLOSED or FILTERED"
fi
done
echo "-------------------------------------"
echo "Port check complete."
How it works: This script helps web developers diagnose common issues by checking if specific network ports are open and listening on the local machine. It iterates through a predefined list of ports (e.g., for web servers, databases, cache services) and uses `nc` (netcat) with the `-z` (zero-I/O mode) and `-w` (timeout) flags to quickly determine if a connection can be established. This provides a quick diagnostic tool for ensuring essential services are running as expected.