Skip to content

Instantly share code, notes, and snippets.

@jcanfield
Created July 28, 2025 00:46
Show Gist options
  • Save jcanfield/3dc48f2bcaf2a103d397a651528983af to your computer and use it in GitHub Desktop.
Save jcanfield/3dc48f2bcaf2a103d397a651528983af to your computer and use it in GitHub Desktop.
Check Ports bash script using ss and netstat
#!/bin/bash
# NOTES: If ss and netstat return null, the script will say so
# Function to check a specific port using ss
chk-port() {
if [ -z "$1" ]; then
echo "Usage: chk-port <port-number>"
return 1
fi
echo "Checking port with ss:"
output=$(sudo ss -tulpn | grep ":$1")
if [ -z "$output" ]; then
echo "Ports not found with ss on port $1."
else
echo "$output"
fi
}
# Function to check a specific port using netstat
chk-port-more() {
if [ -z "$1" ]; then
echo "Usage: chk-port-more <port-number>"
return 1
fi
echo "Checking port with netstat:"
output=$(sudo netstat -tulpn | grep ":$1")
if [ -z "$output" ]; then
echo "Ports not found with netstat on port $1."
else
echo "$output"
fi
}
# Check if a port number was provided
if [ -z "$1" ]; then
echo "Usage: $0 <port-number>"
exit 1
fi
# Call the functions with the provided port number
chk-port "$1"
chk-port-more "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment