Page 1 of 1
Batch Ping a network
Posted: 03 Oct 2020 10:57
by Hooby1
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?
Re: Batch Ping a network
Posted: 03 Oct 2020 12:25
by aGerman
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
Re: Batch Ping a network
Posted: 03 Oct 2020 12:57
by Hooby1
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.
Re: Batch Ping a network
Posted: 03 Oct 2020 13:04
by aGerman
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
Re: Batch Ping a network
Posted: 03 Oct 2020 13:13
by Hooby1
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?
Re: Batch Ping a network
Posted: 03 Oct 2020 13:56
by aGerman
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
Re: Batch Ping a network
Posted: 03 Oct 2020 15:19
by Hooby1
Steffen
Many thanks.