批处理:从tracert检索IP /主机

时间:2014-03-25 15:46:36

标签: batch-file traceroute

我试图创建一个运行tracert的脚本,然后在每一跳上执行ping操作。目前,由于变量全部取决于主机是否可达,因此tracert使我感到困扰。

tracert执行跳数,3次ping然后发布地址或主机名。如果主机名无法访问,则可能会给出*代替。

例如:

  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]

上面的例子中,如果延迟,则所有行都因1 ms OR *而不同。

如果写了一些代码:

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

A = Hop count
B = Hostname (Or IP address if no hostname)
C = IP address (If hostname is present)

这只是其中的一部分..但由于我依赖A B&amp; C是那些IP地址和主机名,如果在ms列中得到*左右,我会得到错误的结果。

我不知道如何搜索IP地址,如果这是一种方法来执行for / f反转,以获取最后一列?

谢谢:)

修改

嘿伙计们,谢谢你的帮助。我尝试过你们使用过的一些方法,但我似乎无法将结果做好。

根据要求,整个代码&amp;我希望如何得到结果:

@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

似乎&#34;皮肤&#34;第一行,当我打电话给GetTraceLine时,我仍然得到&#34; ms ms&#34;在线。

后面的结果是:

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

我希望它更有意义:)

再次感谢所有的帮助!

4 个答案:

答案 0 :(得分:4)

for /f "tokens=5 delims=ms " %a in ('tracert -4 -d speedtest01-hor.aplus.dk ^|find " ms "') do @echo %a

在批处理文件中使用双倍所有百分号。

答案 1 :(得分:0)

@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

我使用了一个名为q22639584.txt的文件,其中包含我的测试数据。

报告的行在字段之间带有+个字符。

如果您向我们展示了您想要的结果,那将是件好事。我只是假设

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

是你想要的。

答案 2 :(得分:0)

@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

输出是:

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]

- 一个班轮:

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

答案 3 :(得分:0)

这提供了在更新的问题中要求的信息类型,但不是所需的格式。

它依赖于英语语言的ping结果。在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"