time { i=0; while [ $(( i < 65535 )) -eq 1 ] ; do nc -zw2 localhost $((++i)) && echo port $i opened ; done; }

list all opened ports on host

in loop, until the last port (65535), list all opened ports on host. in the sample I used localhost, but you can replace with any host to test.
Sample Output
port 22 opened
port 80 opened
port 631 opened
port 3306 opened
port 3690 opened
port 6665 opened
port 6666 opened
port 6667 opened
port 6668 opened
port 6669 opened
port 7048 opened
port 8079 opened
port 8280 opened
port 8885 opened
port 8888 opened
port 9127 opened
port 23791 opened
port 23943 opened
port 33556 opened
port 34482 opened
port 39447 opened
port 48030 opened
port 51687 opened

real    4m33.201s
user    1m16.657s
sys     2m42.194s

1
2009-12-09 17:33:47

4 Alternatives + Submit Alt

What Others Think

To test a port quickly, we saw : nc -w2 -z host.tld && echo open To test all the ports, the good tool is nmap. But that's a good start to re-code a nmap fork from sctratch ;) Last point, I edited your snippet shorter : time { while (( c < 65535 )); do nc -w2 -z localhost $((++c)) && echo port $c opened ; done; }
sputnick · 440 weeks and 5 days ago
Here's another way to write that loop. time { for ((i=1;i<65535;i++));do nc -zw2 localhost $i && echo port $i opened;done;}
eightmillion · 440 weeks and 5 days ago
Shorter and shorter ;) time { for i in {1..65534}; do nc -zw2 localhost $i && echo "$i open"; done; }
sputnick · 440 weeks and 4 days ago
That's faster too.
eightmillion · 440 weeks and 4 days ago

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: