Reputation: 1176
When i run netstat i have the following outpout :
$ netstat -lnp --inet
(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 (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:51413 0.0.0.0:* LISTEN 6155/transmission-g
tcp 0 0 127.0.0.1:56424 0.0.0.0:* LISTEN 450/weechat
netstat: no support for `AF INET (sctp)' on this system.
I want to have the list of the ports using only bash (with python it would be easier). I have this :
$ netstat -lnp --inet | cut -d' ' -f45-50 | sed 's/[^0-9]*//g'
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
netstat: no support for `AF INET (sctp)' on this system.
6155
450
But i can't get rid of the text. Any help ?
Upvotes: 1
Views: 2714
Reputation: 364
The lines:
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Are sent to stderr instead of stdout so you can get rid of them by sending stderr to /dev/null instead of letting it be printed in the console.
As in:
$ netstat -lnp --inet 2>/dev/null| cut -d' ' -f45-50 | sed 's/[^0-9]*//g'
BTW, piping through egrep "\w" will get rid of the empty lines.
$ netstat -lnp --inet 2>/dev/null| cut -d' ' -f45-50 | sed 's/[^0-9]*//g' | egrep "\w"
Upvotes: 1
Reputation: 185075
More robust with awk :
netstat -lnp --inet | awk -F'LISTEN *|/' '/^(tcp|udp)/{print $2}' file
6155
450
Upvotes: 4
Reputation: 1006
Try:
netstat -lnp --inet | grep -E 'tcp|udp' | cut -d' ' -f45-50 | sed 's/[^0-9]*//g'
Upvotes: 0