Skip to content

Instantly share code, notes, and snippets.

@dietercastel
Created May 20, 2016 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dietercastel/016e90ef21bc2328c4446c03d4b072a1 to your computer and use it in GitHub Desktop.
Save dietercastel/016e90ef21bc2328c4446c03d4b072a1 to your computer and use it in GitHub Desktop.
Finds pids that are listening to ports (atm tcp6 on all interfaces only). Ideas for extension include: udp/tcp/udp6, support different interfaces, generate improved output (nice grep-able overview), ...
#Finds pids that are listening to ports (atm tcp6 on all interfaces only)
netstat | grep LISTEN | grep tcp6 | awk '{print $4 }' | awk -F':' '{print $4}' > /tmp/ports.txt
#convert ports to hex
cat /tmp/ports.txt | while read myline; do printf '%x\\n' $myline ; done > /tmp/hexports.txt
#get inodes from hex ports
grep -i -f /tmp/hexports.txt /proc/net/tcp6 | awk '{print $10}' > /tmp/inodes.txt
#get pids tied to inodes
lsof | grep -f /tmp/inodes.txt | awk '{print $2}' > /tmp/pids.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment