yodog
yodog

Reputation: 6232

display open ports grouped by process

given a netstat output, how can i display the selected open ports grouped by process?

what i got so far:

:~# netstat -tnlp | awk '/25|80|443|465|636|993/ {proc=split($7,pr,"/"); port=split($4,po,":"); print pr[2], po[port]}' 

haproxy 636
haproxy 993
haproxy 993
haproxy 465
haproxy 465
exim4 25
apache2 80
exim4 25
apache2 443

desired output (in one line):

apache2 (80 443), exim4 (25), haproxy (465 636 993)

please note:

the main goal is to have this single line displayed to the user on ssh logon, using motd (i got this part covered)

Upvotes: 0

Views: 52

Answers (1)

bbotte
bbotte

Reputation: 180

netstat -tnlp|awk '/25|80|443|465|636|993/ {proc=split($7,pr,"/"); port=split($4,po,":"); print pr[2], po[port]}'|sort|uniq|awk '{a[$1]=a[$1](" "$2" "$3)}END{for (i in a) printf "%s (%s),",i,a[i]}'

try this, Later addition

sort|uniq|awk '{a[$1]=a[$1](" "$2" "$3)}END{for (i in a) printf "%s (%s),",i,a[i]}'

Upvotes: 1

Related Questions