记录ping结果

时间:2015-01-14 08:43:35

标签: windows logging cmd ping

我正在跟踪多个电路。我通过从机器ping到远程IP地址来完成此任务。我最近在此站点的帮助下添加了日志记录功能,但日志增长太快,并且正在减慢从文本文件中提取信息的数据库的速度。是否可以仅记录某些值的值。从那些超时的值开始。 提前致谢

以下是日志文件中生成的一小部分内容。我只想在会议上说Timed Out时保留。

Thu 01/15/2015 - 8:16:10.27 - 74.125.230.244 - rtt [284]

Thu 01/15/2015 - 8:16:12.60 - 74.125.230.244 - rtt [154]

Thu 01/15/2015 - 8:16:14.91 - 74.125.230.244 - rtt [154]

Thu 01/15/2015 - 8:16:21.65 - 74.125.230.244 - rtt [Timed Out]

Thu 01/15/2015 - 8:16:23.74 - 74.125.230.244 - rtt [Timed Out]

Thu 01/15/2015 - 8:16:29.36 - 74.125.230.244 - rtt [Timed Out]

Thu 01/15/2015 - 8:16:31.86 - 74.125.230.244 - rtt [Timed Out]

Thu 01/15/2015 - 8:16:35.36 - 74.125.230.244 - rtt [Timed Out]

Thu 01/15/2015 - 8:16:40.36 - 74.125.230.244 - rtt [Timed Out]

Thu 01/15/2015 - 8:16:45.36 - 74.125.230.244 - rtt [Timed Out]

Thu 01/15/2015 - 8:16:48.36 - 74.125.230.244 - rtt [Timed Out]

Thu 01/15/2015 - 8:16:54.35 - 74.125.230.244 - rtt [Timed Out]

Thu 01/15/2015 - 8:16:56.80 - 74.125.230.244 - rtt [287]

Thu 01/15/2015 - 8:16:59.18 - 74.125.230.244 - rtt [249]

@echo off
TITLE = Circuit 1
mode 40,20
setlocal enableextensions enabledelayedexpansion

rem Get address from command line
set "address=74.125.230.244"
if not defined address set "address=127.0.0.1"

rem Configure levels and colors 
rem The format is initialValue:color in value descending format
set "levels=9000:4F 178:E0 146:2F 0:E0"

rem infinite loop
for /l %%i in () do (
    rem retrieve information from ping command
    set "rtt=Timed Out"
    set "ttl=?"
    for /f "tokens=3,4 delims==^<" %%a in (
        'ping -n 1 "%address%" ^| find "TTL="'
    ) do for /f "tokens=1 delims=m" %%c in ("%%a") do (
        set /a "rtt=%%c"
        set "ttl=%%b"
    )

    rem retrieve color
    set "color="
    for %%z in (%levels%) do for /f "tokens=1,2 delims=:" %%a in ("%%z") do (
        if not defined color if !rtt! geq %%a set "color=%%b"
    )

    rem show information
    if defined color color !color!
    echo(!time! - %address% - rtt[!rtt!]

    rem save to log
    for /f "tokens=1-4 delims=.:-/ " %%a in ("!date!") do (
        >> "%%b-%%c-%%d_%%a_circuit1.txt" echo(!date! - !time! - %address% - rtt[!rtt!]
    )

    rem wait and repeat the process
    ping -n 3 localhost >nul 2>nul 
)

1 个答案:

答案 0 :(得分:0)

理论上,您需要做的就是将执行for的{​​{1}}连接到日志:

echo