Batch Ping a network

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hooby1
Posts: 17
Joined: 27 Sep 2020 08:28

Batch Ping a network

#1 Post by Hooby1 » 03 Oct 2020 10:57

In cmd I use ping -a <ipaddress> to ping computers on a network but I do each IP individually which is very time consuming.
Is it possible via a batch where the user can input the start and end IP address of the range to check and if the ping has a reply, write that to a text file and if it fails to ping also write that to the file?

aGerman
Expert
Posts: 3973
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch Ping a network

#2 Post by aGerman » 03 Oct 2020 12:25

I bet we already have a couple of similar threads on DosTips ...

Code: Select all

@echo off &setlocal
set "baserange=123.134.145."
set /p "lbound=lower bound: "
set /p "ubound=upper bound: "

>"ip.log" (
  for /l %%i in (%lbound% 1 %ubound%) do (
    <nul set /p "=%baserange%%%i "
    >nul 2>&1 ping %baserange%%%i -n 1 && (echo success) || (echo error)
  )
)
E.g. enter 0 for lower bound and 255 for upper bound.

Steffen

Hooby1
Posts: 17
Joined: 27 Sep 2020 08:28

Re: Batch Ping a network

#3 Post by Hooby1 » 03 Oct 2020 12:57

Steffen

Many thanks for this.

I did a search but after finding and trying some out they were slightly different to what I was after.

I have copied the code and made the change to: set "baserange=123.134.145." to my network IP address.

I then ran the batch file and entered 0 at the first prompt and 255 at the second prompt and the batch appeared to do nothing.

The file was created but nothing is being written to it.

I then stopped it from being run and ran again but this time I entered 0 at the first prompt and 5 at the second prompt so test of the range of IP addresses that I know that are active and on (I am able to ping them with a success rate so I know ping is working) but this code doesn't appear to do anything. I have left it for 5 mins and nothing.

aGerman
Expert
Posts: 3973
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch Ping a network

#4 Post by aGerman » 03 Oct 2020 13:04

All I can tell is that it works at least for me.
Try to (temporarily) remove >nul in order to read the actual output of PING in the log file.

Code: Select all

    2>&1 ping %baserange%%%i -n 1 && (echo success) || (echo error)
Steffen

Hooby1
Posts: 17
Joined: 27 Sep 2020 08:28

Re: Batch Ping a network

#5 Post by Hooby1 » 03 Oct 2020 13:13

Steffen

Thank worked many thanks.

I tried to enter -a to resolve the address to the hostname but it doesn't appear in the output file?

Code: Select all

2>&1 ping %baserange%%%i -a -n  1 && (echo success) || (echo error)
can the hostname be resolved and be shown in the output file?

aGerman
Expert
Posts: 3973
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch Ping a network

#6 Post by aGerman » 03 Oct 2020 13:56

The output of PING is language-dependent. Because I don't know how it looks like if you run it the only idea I have is to redirect the whole first line to the log file.

Code: Select all

@echo off &setlocal EnableDelayedExpansion
set "baserange=123.134.145."
set /p "lbound=lower bound: "
set /p "ubound=upper bound: "

>"ip.log" (
  for /l %%i in (%lbound% 1 %ubound%) do (
    set "firstline="
    for /f "delims=" %%i in ('"ping -n 1 -a %baserange%%%i && (echo success) || (echo error)"') do (
      set "result=%%i"
      if not defined firstline set "firstline=%%i"
    )
    echo !firstline! !result!
  )
)
Don't omit the EnableDelayedExpansion!

Steffen

Hooby1
Posts: 17
Joined: 27 Sep 2020 08:28

Re: Batch Ping a network

#7 Post by Hooby1 » 03 Oct 2020 15:19

Steffen

Many thanks.

Post Reply