Skip to content

Instantly share code, notes, and snippets.

@aerostitch
Last active June 10, 2022 12:43
Show Gist options
  • Save aerostitch/50de732f88ca7aeb9bb8 to your computer and use it in GitHub Desktop.
Save aerostitch/50de732f88ca7aeb9bb8 to your computer and use it in GitHub Desktop.
Sorting the connections to a linux server by its status and bu combination of source/destination IP and port
# By status
netstat -an | awk '$6 ~ /^[A-Z_]+[1-2]*$/{a[$6]++} END {for (i in a) print a[i], "\t", i}' | sort -n
# By source IP:port
netstat -an | awk '$4 ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+$/{a[$4]++} END {for (i in a) print a[i], "\t", i}' | sort -n
# By destination IP:port
netstat -an | awk '$5 ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+$/{a[$5]++} END {for (i in a) print a[i], "\t", i}' | sort -n
# Just by destination IP
netstat -an | awk '$5 ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+$/{a[gensub(/^(.+):.+$/, "\\1", "g", $5)]++} END {for (i in a) print a[i], "\t", i}' | sort -n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment