some work netstat laziness
Date: April 5, 2011
lets say that i run netstat -tanpu
which gives an output of :
netstat -tanpu (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:5037 0.0.0.0:* LISTEN 4425/adb tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN - tcp 1 0 10.0.1.222:58665 173.236.188.101:80 CLOSE_WAIT 26979/chromium-brow tcp 0 0 10.0.1.222:34104 64.12.30.16:443 ESTABLISHED 27207/pidgin tcp 0 0 10.0.1.222:40856 74.125.226.152:443 ESTABLISHED 26979/chromium-brow tcp 960 0 10.0.1.222:50400 10.0.1.26:139 ESTABLISHED 11575/gvfsd-smb-bro tcp 0 0 10.0.1.222:42265 10.0.1.26:22 ESTABLISHED 20677/ssh tcp 0 0 10.0.1.222:45574 10.0.1.34:22 ESTABLISHED 25819/ssh tcp 0 0 10.0.1.222:51090 74.125.115.125:5222 ESTABLISHED 27207/pidgin tcp 0 0 10.0.1.222:33447 74.125.226.139:80 ESTABLISHED 26979/chromium-brow tcp 0 0 10.0.1.222:51092 74.125.115.125:5222 ESTABLISHED 27207/pidgin tcp 0 0 10.0.1.222:39939 64.12.29.7:443 ESTABLISHED 27207/pidgin tcp 0 0 10.0.1.222:44759 74.125.226.143:80 ESTABLISHED 26979/chromium-brow tcp 0 0 10.0.1.222:54976 10.0.1.34:22 ESTABLISHED 20201/ssh tcp 944 0 10.0.1.222:52363 10.0.1.26:445 ESTABLISHED 11721/gvfsd-smb tcp 0 0 10.0.1.222:51021 74.125.115.125:5222 ESTABLISHED 26979/chromium-brow tcp6 0 0 :::22 :::* LISTEN - tcp6 0 0 ::1:631 :::* LISTEN - udp 0 0 0.0.0.0:68 0.0.0.0:* - udp 0 0 0.0.0.0:5353 0.0.0.0:* - udp 0 0 0.0.0.0:51118 0.0.0.0:* - udp6 0 0 :::44099 :::* - udp6 0 0 :::5353 :::* -
but all i care about is where Local Address is listening on port 22 (4th column), lets make it a tad easier now:
netstat -tanpu | awk '{if ($4 ~ /:22$/){print}}' (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp6 0 0 :::22 :::* LISTEN -
here is the command line : netstat -tanpu | awk '{if ($4 ~ /:22$/){print}}'
some simple awk magic to output only the lines that match the regex (if column 4 matches :22 then print the whole line).
can be easily changed for your own fun and amusement.
lsof -nP -i TCP:22 # listening and established
lsof -nP -i TCP:22 -sTCP:LISTEN # just listening
very awesome