Im trying to make a script which runs a tracert, and then performs a ping on every hop. Currently, the tracert troubles me, due to the fact that the variables changes all depending if the host is reachable or not.
A tracert performs a hop count, 3 pings and then post the address or hostname. If the hostname is unreachable, it might give out a * instead.
For example:
1 <1 ms <1 ms <1 ms 10.3.2.1
2 * <1 ms <1 ms FA0-0.100M.rc00-rhv.aplus.dk [62.61.128.142]
3 * 1 ms 1 ms hor2.ae15-cr1.danskkabeltv.dk [62.61.137.86]
4 * 1 ms 1 ms TE2-2.10G.rc02-hor.aplus.dk [62.61.138.44]
5 1 ms <1 ms <1 ms speedtest01-hor.aplus.dk [62.61.131.22]
The above example, all the lines differ due to the 1 ms OR * if its delayed.
If written some code:
SET Dest=speedtest.danskkabeltv.dk
SET LineStart=1
SET LineEnd=30
FOR /F "tokens=1,8,9 delims= " %%A IN ('Tracert %Dest%') DO IF %%A GEQ %LineStart% IF %%A LEQ %LineEnd% (
@((timeout /t 2 /nobreak)>nul)
call :GetPing %%A %%B %%C )
pause
And:
A = Hop count
B = Hostname (Or IP address if no hostname)
C = IP address (If hostname is present)
This is only some of it.. But due to the fact that i rely on A B & C being those IP addresses and hostnames, i get the wrong results if i get * or so in the ms column.
I dont know how to search for the IP address, of if theres a way to do a for /f reversed, to get the last columns first?
Thanks :)
EDIT:
Hey guys, thanks for all the help. I've tried with some of the methods you guys used, but i can't really seem to get my result right.
As requested, the entire code & how i would like the results:
@ECHO OFF &SETLOCAL disableDelayedExpansion
SET "Dest=speedtest.danskkabeltv.dk"
SET /a LineStart=1
SET /a LineEnd=30
FOR /F "delims=" %%X IN ('Tracert -w 300 %Dest%') DO (
CALL:GetTraceLine %%~X
)
pause
GOTO:EOF
:GetTraceLine (
SETLOCAL
if "%1"=="Tracing" (
EXIT /b
)
SET "Line=%~1"
SET "Line=%Line: ms =%"
FOR /F "tokens=1,5,6 delims= " %%A IN ("%Line%") DO IF %1 GEQ %LineStart% IF %1 LEQ %LineEnd% (
SET /a Hop=%%A
SET /a HOST=%%B
SET /a IP=%%C
if "%HOP%"=="Tracing" (
EXIT /b
)
if "%HOST%"=="Request" (
echo Hop: %HOP% Packetloss: --- Average: --- Host: Timeout
EXIT /b
)
if "%IP%"=="" (
call :GetPing %%A %HOST%
EXIT /b
)
ELSE (
FOR /F "delims=[]" %%A IN ('%IP%') DO (
set IP=%%A
)
call :GetPing %%A %IP% %HOST%
EXIT /b
)
)
:GetPing (
SetLocal
@ECHO OFF
SET AVG=---
SET LOSS=---
SET TARGET=%2
SET TARGET2=%3
SET HOP=%1
SET PingCMD=ping.exe -w 300 -n 30 %TARGET%
FOR /f "tokens=3 delims=," %%A IN ('%PingCMD%') DO (
call :GetLoss %%A
call :GetAvg %%A
)
echo Hop: %HOP% Packetloss: %loss% Average: %avg% Host: %TARGET% %TARGET2%
)
EXIT /b
:GetLoss
IF "%1"=="Lost" SET loss=%2 %3%% %4
EXIT /b
:GetAvg
IF "%1"=="Average" SET avg=%2 %3
EXIT /b
It doesnt seem to "skin" the first line, when i call GetTraceLine, im still getting "ms ms" in the line.
The result im after is this:
Hop: 1 Packetloss: 0 (0% loss) Average: 0ms IP: 192.168.1.1
Hop: 2 Packetloss: 0 (0% loss) Average: 0ms IP: 10.59.17.1
Hop: 3 Packetloss: 0 (0% loss) Average: 2ms IP: 172.17.2.137
Hop: 4 Packetloss: 0 (0% loss) Average: 6ms IP: 172.17.4.36
Hop: 5 Packetloss: 0 (0% loss) Average: 4ms IP: 172.17.4.10
Hop: 6 Packetloss: 0 (0% loss) Average: 2ms IP: 172.17.4.3
Hop: 7 Packetloss: 0 (0% loss) Average: 3ms IP: 80.72.159.241 Host: lt-0-0-0.mx-1a.ip.cirque.dk
Hop: 8 Packetloss: 0 (0% loss) Average: 2ms IP: 194.255.185.193 Host: 0xc2ffb9c1.linknet.dk.telia.net
Hop: 9 Packetloss: 0 (0% loss) Average: 2ms IP: 194.255.133.97 Host: 0xc2ff8561.linknet.dk.telia.net
Hop: 10 Packetloss: 0 (0% loss) Average: 5ms IP: 194.255.133.98 Host: 0xc2ff8562.linknet.dk.telia.net
Hop: 11 Packetloss: 0 (0% loss) Average: 3ms IP: 87.72.143.234
Hop: 12 Packetloss: --- Average: --- IP: Timeout
Hop: 13 Packetloss: --- Average: --- IP: Timeout
Hop: 14 Packetloss: 0 (0% loss) Average: 3ms IP: 62.61.131.22 Host: speedtest01-hor.aplus.dk
I hope it makes more sense then :)
Again, thanks for all the help!
for /f "tokens=5 delims=ms " %a in ('tracert -4 -d speedtest01-hor.aplus.dk ^|find " ms "') do @echo %a
For usage inside batch file double all percent signs.