Skip to main content

How to ping all devices on a network from the Windows command line interface (CLI) or Window For Loops

By July 2, 2014September 9th, 2020Blog, Hot Technology Topics, Microsoft

Many times I am working on a network where I need to either flush out an ARP table or just see if any hosts respond.  So from a Windows system we go to the command prompt and use the for command to setup a basic for loop (like back in the programming days).  If you want to see all the options for the “for” command use the:

                for /?

Our example is going to the sequence of numbers option “/L”.  We are going to need to define 5 things to get our loop to work.

1) Variable:  We need to define a variable that will be assigned the values as we loop through them.

2) Start: What number is the sequence is starting at.  We will be using 1.

3) Step: What is going to be added to the sequence with each iteration.

4) End: What is the last sequence in the iteration.

5) Command: What command to run, and all its parameters to use

So to put it in context it would look like:

FOR /L %variable IN (start,step,end) DO command [command-parameters]

In my example I am using:

1) Variable:         i

2) Start:                                1

3) Step:                                1

4) End:                  254

5) Command:                     ping 192.168.1 -n 1

Put all together it is:

for /L %i IN (1,1,254) DO ping 192.168.1.%i -n 1

It will walk the whole local network block and ping each host one time. When it runs Below is the output.

Pinging 192.168.1.1 with 32 bytes of data:

Reply from 192.168.1.1: bytes=32 time=287ms TTL=255

Ping statistics for 192.168.1.1:

Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),

Approximate round trip times in milli-seconds:

Minimum = 287ms, Maximum = 287ms, Average = 287ms

Pinging 192.168.1.2 with 32 bytes of data:

Request timed out.

Ping statistics for 192.168.1.2:

Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),

Pinging 192.168.1.3 with 32 bytes of data:

Request timed out.

Ping statistics for 192.168.1.3:

Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),

.

.

.

Pinging 192.168.1.252 with 32 bytes of data:

Reply from 192.168.1.252: bytes=32 time=244ms TTL=63

Ping statistics for 192.168.1.252:

Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),

Approximate round trip times in milli-seconds:

Minimum = 244ms, Maximum = 244ms, Average = 244ms

Pinging 192.168.1.253 with 32 bytes of data:

Request timed out.

Ping statistics for 192.168.1.253:

Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),

Pinging 192.168.1.254 with 32 bytes of data:

Request timed out.

Ping statistics for 192.168.1.254:

Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),

Jason Howe, PEI

Leave a Reply