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
Related
So i have remote machine with simple .bat file:
C:\Program Files\Wireshark\tshark.exe -i 1 -w c:\file.pcap
This command start Tshark process and start sniffing
Now my problem is that i start this .bat file in several machines and in each machine the correct interface is different so with the commend Tshaek -D i can get all the interfaces with the index numbers but from here i need to know which interface is the one the connected to the internet.
And of course i need to do that inside my.bat file and this need to be my interface number (in my example the interface number is hard code and equal to 1)
Any suggestions ?
This is very hacky, but should do.
#echo off
for /f "tokens=2 delims=:" %%i in ('ipconfig ^|findstr "IPv4"') do (
ping -n 1 -S%%i google.com |findstr /i reply
if not errorlevel 1 for /f %%a in ('ipconfig ^| findstr /I /N "%%i"') do set cnt=%%a
)
set /a cnt-=5
for /f "tokens=1* delims=:" %%i in ('ipconfig ^|more +%cnt%') do set adap=%%i & goto runts
:runts
set adap=%adap:*adapter =%
for /f "usebackq delims=." %%i in (`""C:\Program Files\Wireshark\tshark.exe" -D | findstr /I "%adap%""`) do (
"C:\Program Files\Wireshark\tshark.exe" -i %%i -w c:\file.pcap
)
What we do is, run ipconfig and get all IPv4 addresses and use each as source to ping google.com. Using findstr we then match reply and then errorlevel 0 will determine whether the adapter was the active one.
Now we simply go up 5 lines to get the description of the adapter, then we findstr that adapter using tshark -D and simply run the full tshark command.
Note, this will be completely different if you use IPv6.
I'm stuck on this one, I've managed to create a batch file that imports list of IPs & hostnames but when I test for success or failure, I'd like to output how long it took. I don't know how to capture this and include it in the output. I'm trying to hide the default ping.exe output. Here's my code
#echo off
for /F "tokens=1,2 delims= " %%A in (computers.txt) do (
rem ping -n 1 %%A | findstr /i "TTL=" | find "Reply" > nul
if %errorlevel% == 0 (
echo %%B %%A successful ping in ?? Seconds
echo %%B %%A successful ping [%date%, %time%] >> log.txt
)
)
Pause
I've figured it out:
#echo off
setlocal enableDelayedExpansion
for /F "tokens=1,2 delims= " %%A in (computers.txt) do (
for /f "tokens=7 delims== " %%G in ('ping -n 1 %%A') do (
ping -n 1 %%A >nul
if errorlevel 1 (
echo %%B %%A unsuccessful ping
echo %%B %%A unsuccessful ping [!date!, !time!] >> log.txt
) else if "%%~G" NEQ "of" if "%%~G" NEQ "0" (
set ms=%%~G
set ms=!ms:ms=!
for /f %%Z in ('powershell !ms!/1000') do set second=%%~Z
echo %%B %%A successful ping in !second! seconds
echo %%B %%A successful ping [!date!, !time!] >> log.txt
)
)
)
Pause
Using FOR /F, it is possible to process command output.
As you may see, I have 2 ping -n 1 %%A. It is because FOR /F doesn't capture the ERRORLEVEL generated by the processing command. The first command gets the output, the second gets ERRORLEVEL.
if "%%~G" NEQ "of" if "%%~G" NEQ "0" filters out the unneeded results.
The powershell command converts milliseconds to second(batch file does not support decimal numbers)
I/O example
I don't have any other computers on my network, therefore I tested it with URLs.
Input -> Output
www.google.com A A www.google.com successful ping in 0.002 seconds
www.yahoo.com B B www.yahoo.com successful ping in 0.032 seconds
a.a.a C C a.a.a unsuccessful ping
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.
I'm trying to compare an empty string to the results from a ping using FIND. I want to capture only failed ping requests.
Here's what I've tried so far:
ping -n 1 %choice% | FIND "Request" >> %request%
FOR /F "delims=" %%a in ('ping -n 1 %choice% | FIND "Request") Do #set request =%a
Once I get this to set correctly, I plan to compare request to an empty string.
Test this:
ping -n 1 %choice% | FIND "TTL=">nul && (echo pass) || (echo fail)
pause
You need to protect the pipe, and have matched quotes in the for command, and use %%:
FOR /F "delims=" %%a in ('ping -n 1 %choice% ^| FIND "Request" ') Do #set request =%%a
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.