Port scan a range of hosts with Netcat.

for i in {21..29}; do nc -v -n -z -w 1 192.168.0.$i 443; done
Simple one-liner for scanning a range of hosts, you can also scan a range of ports with Netcat by ex.: nc -v -n -z -w 1 192.168.0.1 21-443 Useful when Nmap is not available:) Range declaration like X..X "for i in {21..29}" is only works with bash 3.0+
Sample Output
(UNKNOWN) [192.168.0.21] 443 (?) : Connection refused
(UNKNOWN) [192.168.0.22] 443 (?) open
(UNKNOWN) [192.168.0.23] 443 (?) : Connection refused
(UNKNOWN) [192.168.0.24] 443 (?) open
....
...

9
By: rez0r
2009-09-25 03:31:29

These Might Interest You

  • It takes over 5 seconds to scan a single port on a single host using nmap time (nmap -p 80 192.168.1.1 &> /dev/null) real 0m5.109s user 0m0.102s sys 0m0.004s It took netcat about 2.5 minutes to scan port 80 on the class C time (for NUM in {1..255} ; do nc -w 1 -z -v 192.168.1.${NUM} 80 ; done &> /dev/null) real 2m28.651s user 0m0.136s sys 0m0.341s Using parallel, I am able to scan port 80 on the entire class C in under 2 seconds time (seq 1 255 | parallel -j255 'nc -w 1 -z -v 192.168.1.{} 80' &> /dev/null) real 0m1.957s user 0m0.457s sys 0m0.994s


    3
    seq 1 255 | parallel -j+0 'nc -w 1 -z -v 192.168.1.{} 80'
    devrick0 · 2011-06-11 14:40:51 0
  • Have netcat listen on port 8000, point browser to http://localhost:8000/ and you see the information sent. netcat terminates as soon as your browser disconnects. I tested this command on my Fedora box but linuxrawkstar pointed out that he needs to use nc -l -p 8000 instead. This depends on the netcat version you use. The additional '-p' is required by GNU netcat that for example is used by Debian but not by the OpenBSD netcat port used by my Fedora system. Show Sample Output


    2
    nc -l 8000
    penpen · 2009-03-25 23:09:38 2
  • Tar's up $DIR locally (w/bzip2) and sends remotely to $HOST:$PORT where netcat listens (using openbsd netcat). Start up receiving side command first, then execute this.


    0
    tar -cjf - $DIR | nc $HOST $PORT
    taintedkernel · 2012-11-13 16:44:26 0
  • This is an example of using 3 hosts, in a netcat relay. first host connects to middle host 1 -> 2 Second hosts redirects to target host 1 -> 2 -> 3 I hope this makes sense.


    2
    nc -vv $MIDDLEHOST 1234; ## nc -vv -l $IamMIDDLEHOST 1234 | nc $Targethost 1234;## nc -l $IamTargetHost 1234 -e /bin/bash;
    Abiden · 2010-01-29 16:45:39 0

What Others Think

For other Bourne family shells try for i in $(seq 21 29); do nc -v -n -z -w 1 192.168.0.$i 443; done instead.
penpen · 455 weeks and 4 days ago
On some distributions the command name is 'netcat', not 'nc', so try both.
hfs · 395 weeks and 5 days ago
one liner for powershell on windows. for ($i=1; $i++){if ($i -lt 255){.\nc.exe -v -n -z -w 1 192.168.0.$i 80;}}
tasdawg · 28 weeks and 5 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: