Friday, May 27, 2011

Ping a range of IPs

C:\>for /l %i in (1,1,254) do @ping 192.168.1.%i -n 1 -w 100 | find "Reply"

This will ping all addresses from 192.168.1.1 to 192.168.1.254 one time each, wait 100ms for a reply (more than enough time on a local network) and show only the addresses that replied.

Syntax for FOR /L is (start,step,end) if you want to change the range.

Also, note that the Windows FIND is case sensitive, so make sure you capitalize "Reply," or else use the /i switch or just "eply."

30 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thanks Florjan! I love this one!
    Greetings, Matt

    ReplyDelete
  3. Excellent one liner :D i'd like to point out that in a language specific non-english enviroment the "Reply" will yield no result.

    Changing it to "TTL" will give a result in any language.

    ReplyDelete
  4. Hi, how would you adjust this to only show IPs that do not respond?

    ReplyDelete
  5. for /l %i in (1,1,254) do @ping 192.168.1.%i -n 1 -w 100 | find "timed out"

    ReplyDelete
    Replies
    1. for /l %i in (1,1,254) do @ping 192.168.1.%i -n 1 -w 100 | find "timed out"

      this request does not show which addresses timed out any ideas?

      Delete
    2. need to change wait time from - w 100 to something like 5000 depends on how quickly you get your timeout messages. Might also need to use "unreachable"

      Delete
  6. Thank you for this post, helped a lot

    ReplyDelete
  7. for /l %i in (1,1,254) do @ping 192.168.1.%i -n 1 -w 100 | find "Reply" good job en.seo-hk.org

    ReplyDelete
  8. for /l %i in (1,1,254) do @ping 192.168.1.%i -n 1 -w 100 | find "Reply" good job en.seo-hk.org

    ReplyDelete
  9. can you use parameters like -a in the @ ping? I tried it but it didn't seem to work.

    ReplyDelete
  10. And to get an output file of it all?

    ReplyDelete
  11. Just add >>pinglist.txt or whatever filename to the end. (You need two >'s, otherwise each ping will overwrite the file)

    ReplyDelete
  12. I should apply myself more to the command line. So elegant and powerful. Thanks!

    ReplyDelete
  13. what is difference between @ping and ping ...
    and what is /L after for

    ReplyDelete
    Replies
    1. The @ sign just makes the command not appear. Without it, you would see each ping command written to the screen.

      The "for /l" is a loop and operates on (start, step, end). Without the /l it would expect file and directory names.

      Delete
  14. best dos command of all times

    ReplyDelete
  15. Does anyone know whether it's possible to run a single line command for a subnet larger than a /24? Altering the step change isn't an option as there's no IP intelligence there so it just starts pinging .256, .257, .258 etc. as opposed to incrementing the third octet and starting afresh from whatever your starting point is.

    ReplyDelete
    Replies
    1. You would have to do a nested loop and expand slightly:

      for /l %i in (0,1,7) do @for /l %j in (0,1,255) do @ping 192.168.%i.%j -n 1 -w 100 | find "Reply"

      This will ping 192.168.0.0 through 7.255. Obviously, it will actually try to ping the ID and the broadcast, but there's no way around that.

      Delete
    2. Thanks Joe :)

      It was actually a mass rDNS lookup I was looking to perform so achieved this as follows (it was the nested loop I needed though so thanks!):

      FOR /L %i IN (124,1,127) DO @for /l %j in (0,1,255) do nslookup xxx.xxx.%i.%j x.x.x.x | FINDSTR /r "Name Address">>c:\rdns.txt

      I could probably have done with omitting entries that didn't resolve from the results but it did the job :)

      Delete
  16. Thank you Joe :-)

    I was wondering; is there a way to request host names of responding IP's at the same time?

    ReplyDelete
    Replies
    1. Sure thing! Just add "-a" to the ping command. It'll slow things down a bit, and it's not always reliable, but that's all we can do.

      Delete
  17. is is possible to ping multiple host name by above com line.

    E.g edit all host name by xx.txt then ping them

    ReplyDelete
  18. Is it possible to change you computers IP address, Netmask and Gateway from the command prompt ?

    ReplyDelete