Bash

Check If Port Is Open

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Several ways to probe a remote port: nc (most portable), bash's /dev/tcp pseudo-device (zero deps), or curl. Pick by what's available.
Bash
Raw
# Method 1: nc (netcat) — universal
nc -z -w 3 host.example.com 443 && echo "open" || echo "closed"

# Method 2: pure bash via /dev/tcp (no deps — but bash-only, not POSIX sh)
if timeout 3 bash -c "echo > /dev/tcp/host.example.com/443"; then
    echo "open"
fi

# Method 3: curl for an HTTPS endpoint
curl -fsSL --connect-timeout 3 https://host.example.com >/dev/null && echo "HTTPS OK"

# Reusable helper
port_open() {
    timeout 3 bash -c "echo > /dev/tcp/$1/$2" 2>/dev/null
}

port_open google.com 443 && echo "Google HTTPS reachable"
port_open localhost 5432 || echo "Postgres NOT running"
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.