Skip to content

Instantly share code, notes, and snippets.

@Saruspete
Created February 9, 2021 15:47
Show Gist options
  • Save Saruspete/ca511162a235c7e87264e48f9ad49768 to your computer and use it in GitHub Desktop.
Save Saruspete/ca511162a235c7e87264e48f9ad49768 to your computer and use it in GitHub Desktop.
Check that no process is listening on ephemeral port range
#!/usr/bin/env bash
# Get min/max port range from sysctl
prange="$(sysctl net.ipv4.ip_local_port_range| awk '{print $3,$4}')"
pmin="${prange% *}"
pmax="${prange#* }"
# TODO: ignore ports in sysctl net.ipv4.ip_local_reserved_ports
ss --listen --numeric --tcp --process | awk -v pmin=$pmin -v pmax=$pmax '
$1=="LISTEN"{
split($4,a,":")
port=a[length(a)]
if (port >= pmin && port <= pmax) {
print
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment