#!/bin/bash HOST="$1" PORT="$2" TIMEOUT=1 # seconds if [ -z "$HOST" ] || [ -z "$PORT" ]; then echo "Usage: $0 " exit 1 fi # Check if nc (netcat) is available if ! command -v nc &> /dev/null then echo "Error: netcat (nc) is not installed. Please install it to use this script." exit 1 fi echo "Checking if $HOST:$PORT is open..." # Use netcat (nc) to check if the port is open # -z: zero-I/O mode (scans for listening daemons without sending any data) # -w : timeout for connects if nc -z -w $TIMEOUT "$HOST" "$PORT"; then echo "$HOST:$PORT is OPEN." else echo "$HOST:$PORT is CLOSED or unreachable." fi