Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

@ECHO OFF
SETLOCAL
FOR /f "delims=" %%a IN (q22639584.txt) DO SET "line=%%a"&CALL :process

GOTO :EOF

:process
SET "line=%line:<=%"
CALL :select %line%
ECHO %A%+%B%+%C%
GOTO :eof

:select
SET A=%1
:selectl
shift
SET "B=%1"
SET "C=%2"
SET "line=%3"
IF DEFINED line GOTO selectl
IF %c:~0,1%==[ (SET "c=%c:~1,-1%") else (SET "B=")
GOTO :eof

I used a file named q22639584.txt containing your data for my testing.

The lines are reported with + characters between the fields.

It would have been good if you'd shown us what you wanted as a result. I'm just assuming that

1++10.3.2.1
2+FA0-0.100M.rc00-rhv.aplus.dk+62.61.128.142
3+hor2.ae15-cr1.danskkabeltv.dk+62.61.137.86
4+TE2-2.10G.rc02-hor.aplus.dk+62.61.138.44
5+speedtest01-hor.aplus.dk+62.61.131.22

is what you want.

@ECHO OFF &SETLOCAL disableDelayedExpansion
SET "Dest=speedtest.danskkabeltv.dk"
SET /a LineStart=1
SET /a LineEnd=30
FOR /F "delims=" %%X IN (FILE) DO CALL:GetTraceLine "%%~X"
GOTO:EOF

:GetTraceLine
SETLOCAL
SET "Line=%~1"
SET "Line=%Line: ms =%"
FOR /F "tokens=1,5,6 delims= " %%A IN ("%Line%") DO IF %%A GEQ %LineStart% IF %%A LEQ %LineEnd% (
    call :GetPing %%A %%B %%C
)
EXIT /b

:GetPing
SETLOCAL
ECHO(%~1 %~2 %~3
EXIT /b

output is:

1 10.3.2.1
2 FA0-0.100M.rc00-rhv.aplus.dk [62.61.128.142]
3 hor2.ae15-cr1.danskkabeltv.dk [62.61.137.86]
4 TE2-2.10G.rc02-hor.aplus.dk [62.61.138.44]
5 speedtest01-hor.aplus.dk [62.61.131.22]

- one liner:

for /f %a in ('tracert -4 -d stackoverflow.com ^|sed -r "/\sms\s/!d; s/.*\s(.+)/\1/"') do @echo %a

This gives the kind of information asked for in the updated question but not quite the desired formatting.

It relies on English language for the ping results. Tested in Windows 8.1

@echo off
setlocal enabledelayedexpansion
set "file=%temp%\tr999.tmp"
call :tr speedtest01-hor.aplus.dk
pause
goto :EOF
:tr
echo performing tracert to %1
for /f "skip=2 tokens=1,5 delims=ms " %%a in ('tracert -4 -d %1 ^|findstr "[0-9]"') do (
 if /i not "%%b" == "Reque" (
   set Packetloss=
   set average=*
   set host=
   echo checking hop %%a to %%b
   ping -a -n 4 %%b > "%file%"
   for /f "tokens=2" %%f in ('findstr "Pinging" ^< "%file%"') do set host=%%f
   for /f "tokens=4 delims==" %%f in ('findstr "Lost" ^< "%file%"') do set packetloss=%%f
   for /f "tokens=4 delims==" %%f in ('findstr "Average" ^< "%file%"') do set average=%%f
   >> "%file%.txt" echo Hop: %%a - Packetloss: !packetloss:~0,-1! - Average: !average! - IP: %%b - Host: !host! 
 ) else (
   >> "%file%.txt" echo Hop: %%a - *** timed out ***
 )
)
echo done
echo.
type "%file%.txt"
copy "%file%.txt" ".\tracert results.txt" >nul
del "%file%" "%file%.txt"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top