Batch script with nested loops to ping range of IP's - loops

Working 1 loop code:
for /l %i in (1,1,254) do #ping 131.212.30.%i -n 1 -w 100 | find "Reply"
Not running code where I try to use a counter so every time the ping gets a reply we add 1 to online:
SET online=0 for /L %i in (1,1,254) do for /L %j in (1,1,255) do #ping 131.212.%i.%j -n 1 -w 100 | find "Reply" SET /A online=online+1
Thanks a lot.

Reply from 146.57.239.18: Destination host unreachable
The destination is not reachable, so your local host (146.57.239.18) replies with "Destination host unreachable").
146.57.239.18 is not the pinged host, but your localhost.
It's better to search for TTL= instead of Reply:
...
ping 131.212.%%i.%%j -n 1 -w 100 | find "TTL="
...
Also your set /a online=%online%+1 doesn't work. You would need delayed expansion. The set /a online +=1 syntax works better:
...
ping 131.212.%%i.%%j -n 1 -w 100 | find "TTL=" && SET /A online +=1 || set /a offline +=1
...
As a result, the whole code would look like:
SET online=0
for /L %%i in (1,1,254) do for /L %%j in (1,1,255) do ping 131.212.%%i.%%j -n 1 -w 100 | find "TTL=" && SET /A online +=1
echo %online% hosts are online.
EDIT a much quicker solution (working parallel):
#echo off
SET online=0
for /L %%i in (1,1,254) do (
start /min "pinging" cmd /c "(#for /L %%j in (1,1,255) do #ping 146.254.%%i.%%j -n 1 -w 100 | find "TTL=") >ping%%i.txt"
)
:loop
timeout /t 1 >nul
tasklist /v | find "pinging" && goto :loop
pause
for /f %%i in ('type ping*.txt 2^>nul^|find /c "TTL="') do echo %%i hosts are online
del ping*.txt

You have the syntax slightly wrong. The following should work (split over a couple of lines for readability:
for /l %%i in (1,1,254) do (
for /l %%j in (1,1,254) do (
ping 131.212.%%i.%%j -n 1 -w 100 | find "Reply"
)
)
Keep in mind that this is a lot of IPs to ping.

Related

Batch file filtering out ping results

I have a script to continuously ping a set of machines (stored in SiteName.txt) and once a machine comes online, a counter increments, and the machine is removed from the list so it doesn't ping again (I just need to know if a machine has been online, not if it's on right now).
The issue I have is I've noticed there are a few phantom pings coming up with the IP address of the site they were built at (and that IP hasn't already been taken over by another machine) so I get false positives.
My current code is:
#echo off & setlocal EnableDelayedExpansion
TITLE %~n0
set /a counteron=0
for /F %%a in (%~n0.txt) do set "NVC=!NVC! %%a"
:ping
ping -n 61 127.0.0.1>nul
for %%i in (%NVC%) do (
ping %%i -n 1 | find "TTL=" >nul
if !errorlevel! GEQ 1 (
echo %%i is offline.
) else (
set /a counteron+=1
echo %%i is online
set "NVC=!NVC: %%i=!"
)
)
echo.
cls
echo. %counteron% machines are online.
if defined NVC goto :ping
cls
echo All machines in %~n0 are online.
pause
Is it possible to do the same thing, but if the IP's first 3 octets match a specific set (10.79.208.xxx), then it still comes up as offline?
Thanks in advance
Perhaps
ping %%i -n 1 | find "TTL=" |findstr /b /l /v /c:"Reply from 10.79.208." >nul
which should set errorlevel to zero only if both TTL= is found AND a line beginning (/b) with the /l literal string /c: "Reply from 10.79.208." is /v not found
which I believe is your quest...
Perhaps this slight alteration to your script is what you're after.
#echo off & setlocal enabledelayedexpansion
set nvc=192.168.1.1
for %%i in (%NVC%) do (
ping %%i -n 1 | find "TTL=" >nul
if !errorlevel! GEQ 1 (
echo %%i is offline.
) else (
for /f "tokens=1,2,3 delims=." %%j in ("%%i") do (
set octets=%%j.%%k.%%l
if "!octets!"=="10.79.208" (
echo %%i is false positive
) else (
set /a counteron+=1
echo %%i is online
set "NVC=!NVC: %%i=!"
)
)
)
)

Writing a batch file to detect ping anomalies

I'm trying to create a batch file where it would detect ping anomalies. I want it to ping to an IP infinitely (-t) until I close it where it would write down whenever ms > 100 ms and the time stamp as well. I'm thinking somewhere along the lines of sub string variables but I don't know how to wrap my head around it.
:Loop
time /t >> textfile.txt
ping -n 1 127.0.0.1 | findstr /c:"Minimum" >> textfile.txt
timeout /t 5
Goto Loop
Or perhaps this suits your needs
ping /t > textfile.txt
or
:loop
wmic /append:"textfile.txt" path win32_pingstatus where "address='127.0.0.1' and responsetime > 100" get responsetime,timestamprecord
goto loop
I've been looking for an answer to this same question for while. bgalea's answer gave me the pieces I needed to write my own. Here's what I came up with:
:: usage: badpings.bat [ip adress | hostname] [ping time threshhold]
#echo off
if "%1"=="" (
set pingdest=yahoo.com
) else (
set pingdest=%1
)
if "%2"=="" (
set /a limit=100
) else (
set /a limit=%2
)
echo Pinging %pingdest%.
echo Logging replies over %limit%ms.
echo Press Ctrl+C to end.
:Loop
for /f "usebackq tokens=1-6" %%a in (`ping -n 1 %pingdest% ^| findstr "Request Reply request"`) do (
set var=%%a %%b %%c %%d %%e %%f
set pingtimestr=%%e
)
if "%pingtimestr%"=="find" (
echo Ping request could not find host %pingdest%. Please check the name and try again.
goto End
)
if "%pingtimestr%"=="host" (
set /a pingtime=%limit%+1
)
if "%pingtimestr:~0,4%"=="time" (
set /a pingtime=%pingtimestr:~5,-2%
)
if %pingtime% GTR %limit% (
echo [%time%] %var%>>badpings.log
echo [%time%] %var%)
timeout /t 1 /nobreak >nul
Goto Loop
:End
It works on Windows 10. I haven't tested it on other OS versions.

Separate IP from text in ping output?

I'd like to be able to run a single batch file that would find the IP address of every device connected to my router, and then find their hostnames from it. What I have so far is below:
#echo off
set /a n=0
:repeat
set /a n+=1
echo 192.168.1.%n%
ping -n 1 -w 500 192.168.1.%n% | FIND /i "Reply">>devices.txt
if %n% lss 254 goto repeat
type devices.txt
goto :hostname
This finds all the possible addresses and writes them to a text file, devices.txt.
However, I now end up with the following:
Reply from 192.168.1.82: bytes=32 time=5ms TTL=255
The next part of the batch file goes:
:hostname
ping -a %ip% -l 1 -n 1 >> hostnames.txt
This pings the IPs found in devices.txt and returns the result in hostnames.txt - or at least, I'd like it to.
Somehow I need to separate the IP address from the other text when writing it to devices.txt, and then assign it as a variable so it can be used by the next part.
Is there a simple function I can use to do either of these things?
Try this:
#echo off
setlocal
set "p=ping -n 1 -w 500 192.168.1"
for /l %%a in (1,1,254) do (
for /f "Tokens=3 delims=: " %%b in (
'%p%.%%a^|Find /i "TTL="'
) do (
echo Pinging %%b
echo %%b>>devices.txt
for /f "tokens=2" %%c in (
'ping -a %%b -l 1 -n 1^|Find /i "pinging"') do (
echo %%c >>hostnames.txt
)
)
)
We start by creating a For /L loop that goes from 1 to 254 in increments of 1. Then we run a for loop on a ping command that takes the third token on the return line and puts it into a variable, %%b in this case. We then echo out pinging IP address contained in %%b and write it to devices.txt. Then, we run another ping to extract the second token which is the hostname of the computer we're pinging and write it out to hostnames.txt.

I need batch to create as much vars as he loops

Example:
:loop
set n=0
set m=254
set /a n+=1
ping -n 1 -w 500 xxx.xxx.xxx.%n% | find /i "reply" > file
=====BELOW is what I need=====
set a=0
set /a a+=1
set %a%= < file
====ABOVE is what I need=====
if %n% lss %m% goto loop
So specifically I need a batch script that can make number of variables as much as he loops. I searched a lot for answer and even tried few ideas on my own... but I can't figure this out... I guess lack of batch knowledge since I am ubuntu user and not Win. Thanks in advance. Regards
Use FOR /L instead of SET /A with GOTO
Use FOR /F to process results of command instead of temp file and SET /P
Use carefully constructed variable names to emulate an array (potentially sparse array in this case)
for /l %%N in (1 1 255) do (
for /f "delims=" %%A in (
'ping -n 1 -w 500 xxx.xxx.xxx.%%N^|find /i "reply"'
) do set "addr[%%N]=%%A"
)
#echo off
setlocal enableextensions disabledelayedexpansion
for /l %%n in (1 1 254) do (
for /f "tokens=*" %%r in (
'ping -n 1 -w 500 xxx.xxx.xxx.%%n ^| find "reply" '
) do set "a[%%n]=%%r"
)
set a[
endlocal
This uses for /f to run a command (ping | find) and assing the output of the command (1 line of the ping response, filtered by find) to a variable with a incrementing number in its name.
set a[ is used to show the resulting data on console
#echo off
if exist Pingresult.txt del Pingresult.txt
for /l %%n in (1 1 254) do ping -n 1 -w 500 xxx.xxx.xx.%%n | find /i "reply" >>PingResult.txt

batch file - getting ip from ping command

i have this command.
ping -n 1 piratelufi.com | find /i "reply from"
i want to get the ip inside the ping response. i thought of using %var:~0,10% but i have no idea how to redirect the output to echo.
i even tried to use pipe
ping -n 1 piratelufi.com | find /i "reply from" | echo %1
but the %1 variable does not represent the output of the first two commands. i even tried to use &1 but i failed. What is the 'variable' needed to echo out the output from the first two commands?
Check out the FOR command it's really cool:
FOR /f "tokens=1,3 delims=: " %%A IN ('ping -n 1 piratelufi.com') DO IF %%A==Reply ECHO IP IS %%B
Try the following:
for /f "tokens=2 delims=[]" %f in ('ping -4 -n 1 piratelufi.com ^|find /i "pinging"') do echo IP=%f

Resources