make pretty the netstat output for listening services

sudo netstat -plntu --inet | sort -t: -k2,2n | sort --stable -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n | sort -s -t" " -k1,1
bit of a contrived example and playing to my OCD but nice for quick scripted output of listening ports which is sorted by port, ip address and protocol.
Sample Output
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      26691/sshd
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4437/httpd
tcp        0      0 0.0.0.0:443                 0.0.0.0:*                   LISTEN      4437/httpd
tcp        0      0 0.0.0.0:1270                0.0.0.0:*                   LISTEN      3901/scxcimserver
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      4323/mysqld
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      4351/nrpe
tcp        0      0 0.0.0.0:13722               0.0.0.0:*                   LISTEN      3953/xinetd
tcp        0      0 0.0.0.0:13782               0.0.0.0:*                   LISTEN      3953/xinetd
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      4408/master
tcp        0      0 127.0.0.1:199               0.0.0.0:*                   LISTEN      3920/snmpd
tcp        0      0 127.0.0.1:6010              0.0.0.0:*                   LISTEN      20142/sshd
tcp        0      0 127.0.0.1:6011              0.0.0.0:*                   LISTEN      915/sshd
tcp        0      0 127.0.0.1:6012              0.0.0.0:*                   LISTEN      18068/sshd
tcp        0      0 127.0.0.1:6013              0.0.0.0:*                   LISTEN      6182/sshd
tcp        0      0 127.0.0.1:6014              0.0.0.0:*                   LISTEN      6182/sshd
tcp        0      0 127.0.0.1:6015              0.0.0.0:*                   LISTEN      23596/sshd
tcp        0      0 127.0.0.1:6016              0.0.0.0:*                   LISTEN      24031/sshd
tcp        0      0 127.0.0.1:6017              0.0.0.0:*                   LISTEN      26005/sshd
tcp        0      0 127.0.0.1:6018              0.0.0.0:*                   LISTEN      26480/sshd
tcp        0      0 127.0.0.1:6019              0.0.0.0:*                   LISTEN      20051/sshd
tcp        0      0 127.0.0.1:6021              0.0.0.0:*                   LISTEN      20452/sshd
tcp        0      0 127.0.0.1:6026              0.0.0.0:*                   LISTEN      7309/sshd
tcp        0      0 127.0.0.1:6027              0.0.0.0:*                   LISTEN      7465/sshd
tcp        0      0 127.0.0.1:6028              0.0.0.0:*                   LISTEN      7628/sshd
tcp        0      0 127.0.0.1:6029              0.0.0.0:*                   LISTEN      7880/sshd
tcp        0      0 127.0.0.1:6030              0.0.0.0:*                   LISTEN      8412/sshd
tcp        0      0 127.0.0.1:6031              0.0.0.0:*                   LISTEN      8658/sshd
tcp        0      0 127.0.0.1:6032              0.0.0.0:*                   LISTEN      8839/sshd
tcp        0      0 127.0.0.1:6033              0.0.0.0:*                   LISTEN      9082/sshd
tcp        0      0 127.0.0.1:6034              0.0.0.0:*                   LISTEN      9260/sshd
tcp        0      0 127.0.0.1:6035              0.0.0.0:*                   LISTEN      9642/sshd
tcp        0      0 127.0.0.1:6037              0.0.0.0:*                   LISTEN      6182/sshd
udp        0      0 0.0.0.0:69                  0.0.0.0:*                               3953/xinetd
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               3399/ntpd
udp        0      0 0.0.0.0:161                 0.0.0.0:*                               3920/snmpd
udp        0      0 127.0.0.1:123               0.0.0.0:*                               3399/ntpd
udp        0      0 10.129.6.22:123             0.0.0.0:*                               3399/ntpd
udp        0      0 10.129.6.66:123             0.0.0.0:*                               3399/ntpd

0
By: thechile
2013-08-13 09:21:26

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: