在 Windows CLI 上使用时间戳进行 Ping

问题描述 投票:0回答:19

在 Windows 命令提示符

cmd
上,我使用
ping -t to 10.21.11.81

Reply from 10.21.11.81: bytes=32 time=3889ms TTL=238
Reply from 10.21.11.81: bytes=32 time=3738ms TTL=238
Reply from 10.21.11.81: bytes=32 time=3379ms TTL=238

是否有可能获得这样的输出?

10:13:29.421875 Reply from 10.21.11.81: bytes=32 time=3889ms TTL=238
10:13:29.468750 Reply from 10.21.11.81: bytes=32 time=3738ms TTL=238
10:13:29.468751 Reply from 10.21.11.81: bytes=32 time=3379ms TTL=238

请注意,我想仅使用 CMD 提供的命令来实现此目的

windows cmd timestamp ping
19个回答
142
投票

WindowsPowershell:

选项1

ping.exe -t COMPUTERNAME|Foreach{"{0} - {1}" -f (Get-Date),$_}

选项2

Test-Connection -Count 9999 -ComputerName COMPUTERNAME | Format-Table @{Name='TimeStamp';Expression={Get-Date}},Address,ProtocolAddress,ResponseTime

110
投票
@echo off
    ping -t localhost|find /v ""|cmd /q /v:on /c "for /l %%a in (0) do (set "data="&set /p "data="&if defined data echo(!time! !data!)" 

note:在批处理文件中使用的代码。要从命令行使用,请将

%%a
替换为
%a

启动 ping,强制正确的行缓冲输出 (

find /v
),并启动启用延迟扩展的
cmd
进程,该进程将执行无限循环读取管道数据,这些数据将回显到以当前时间为前缀的控制台。

2015-01-08编辑: 在更快/更新的机器/操作系统版本中,以前的代码存在同步问题,使得

set /p
读取一行,而
ping
命令仍在写入,结果是行剪切。

@echo off
    ping -t localhost|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!time! !data!)&ping -n 2 localhost>nul"

在子 shell 的开头包含两个额外的

pause
命令(只能使用一个,但由于
pause
消耗一个输入字符,一对 CRLF 被破坏并读取带有 LF 的行)以等待输入数据,并且包含一个
ping -n 2 localhost
来等待内部循环中每次读取的时间。结果是行为更稳定,CPU 使用率更低。

注意:内部

ping
可以替换为
pause
,但是每个读取行的第一个字符会被
pause
消耗,并且不会被
set /p

检索

75
投票

您可以在 Bash 中执行此操作(例如 Linux 或 WSL):

ping 10.0.0.1 | while read line; do echo `date` - $line; done

虽然它没有给出你通常在最后点击 ^C 时得到的统计数据。


32
投票

批处理脚本:

@echo off

set /p host=host Address: 
set logfile=Log_%host%.log

echo Target Host = %host% >%logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1 ') do (echo %%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
    timeout 1 >NUL 
    GOTO Ping)

此脚本将询问要 ping 的主机。 Ping 输出输出到屏幕和日志文件。 日志文件输出示例:

Target Host = www.nu.nl
Pinging nu-nl.gslb.sanomaservices.nl [62.69.166.210] with 32 bytes of data: 
24-Aug-2015 13:17:42 Reply from 62.69.166.210: bytes=32 time=1ms TTL=250
24-Aug-2015 13:17:43 Reply from 62.69.166.210: bytes=32 time=1ms TTL=250
24-Aug-2015 13:17:44 Reply from 62.69.166.210: bytes=32 time=1ms TTL=250

日志文件名为 LOG_[主机名].log 并写入与脚本相同的文件夹。


25
投票

这可能对某人有帮助:[需要在 Windows PowerShell 中运行]

ping.exe -t 10.227.23.241 |Foreach{"{0} - {1}" -f (Get-Date),$_} >> Ping_IP.txt

-- 检查当前目录或用户主路径下是否有 Ping_IP.txt 文件。

上面的命令会在如下文件中提供输出;

9/14/2018 8:58:48 AM - Pinging 10.227.23.241 with 32 bytes of data:
9/14/2018 8:58:48 AM - Reply from 10.227.23.241: bytes=32 time=29ms TTL=117
9/14/2018 8:58:49 AM - Reply from 10.227.23.241: bytes=32 time=29ms TTL=117
9/14/2018 8:58:50 AM - Reply from 10.227.23.241: bytes=32 time=28ms TTL=117
9/14/2018 8:58:51 AM - Reply from 10.227.23.241: bytes=32 time=27ms TTL=117
9/14/2018 8:58:52 AM - Reply from 10.227.23.241: bytes=32 time=28ms TTL=117
9/14/2018 8:58:53 AM - Reply from 10.227.23.241: bytes=32 time=27ms TTL=117
9/14/2018 8:58:54 AM - Reply from 10.227.23.241: bytes=32 time=28ms TTL=117

祝你好运!!!


7
投票

这可能适合更高版本的 Windows 版本:

for /l %i in (1,0,2) do @echo|cmd /v:on /c set /p=!time! & ping -n 1 10.21.11.81 | findstr "Reply timed" && timeout /t 2 > nul:

6
投票

在 Windows 上

您可以使用其他答案之一。

在 Unix/Linux 上

while :;do ping -n -w1 -W1 -c1 10.21.11.81| grep -E "rtt|100%"| sed -e "s/^/`date` /g"; sleep 1; done

或者作为 ~/.bashrc 的函数

pingt
:

pingt() {
  while :;do ping -n -w1 -W1 -c1 $1| grep -E "rtt|100%"| sed -e "s/^/`date` /g"; sleep 1; done
}

来源:https://stackoverflow.com/a/26666549/1069083


4
投票

我认为我的代码是每个人都需要的:

ping -w 5000 -t -l 4000 -4 localhost|cmd /q /v /c "(pause&pause)>nul &for /l %a in () do (for /f "delims=*" %a in ('powershell get-date -format "{ddd dd-MMM-yyyy HH:mm:ss}"') do (set datax=%a) && set /p "data=" && echo([!datax!] - !data!)&ping -n 2 localhost>nul"

显示:

[Fri 09-Feb-2018 11:55:03] - Pinging localhost [127.0.0.1] with 4000 bytes of data:
[Fri 09-Feb-2018 11:55:05] - Reply from 127.0.0.1: bytes=4000 time<1ms TTL=128
[Fri 09-Feb-2018 11:55:08] - Reply from 127.0.0.1: bytes=4000 time<1ms TTL=128
[Fri 09-Feb-2018 11:55:11] - Reply from 127.0.0.1: bytes=4000 time<1ms TTL=128
[Fri 09-Feb-2018 11:55:13] - Reply from 127.0.0.1: bytes=4000 time<1ms TTL=128

注意:要在命令行中使用的代码,并且您必须在操作系统上预安装powershell。


2
投票

试试这个:

使用以下内容创建批处理文件:

echo off

cd\

:start

echo %time% >> c:\somedirectory\pinghostname.txt

ping pinghostname >> c:\somedirectory\pinghostname.txt

goto start

您可以根据需要向 ping 命令添加自己的选项。这不会将时间戳与 ping 放在同一行,但它仍然可以为您提供所需的信息。

更好的方法是使用 fping,到这里 http://www.kwakkelflap.com/fping.html 下载它。


2
投票

使用

ping -D 8.8.8.8

来自手册页

-D     Print timestamp (unix time + microseconds as in gettimeofday) before each line

输出

[1593014142.306704] 64 bytes from 8.8.8.8: icmp_seq=2 ttl=120 time=13.7 ms
[1593014143.307690] 64 bytes from 8.8.8.8: icmp_seq=3 ttl=120 time=13.8 ms
[1593014144.310229] 64 bytes from 8.8.8.8: icmp_seq=4 ttl=120 time=14.3 ms
[1593014145.311144] 64 bytes from 8.8.8.8: icmp_seq=5 ttl=120 time=14.2 ms
[1593014146.312641] 64 bytes from 8.8.8.8: icmp_seq=6 ttl=120 time=14.8 ms

1
投票

我还需要它来监控数据库镜像超时问题的网络问题。我使用的命令代码如下:

ping -t Google.com|cmd /q /v /c "(pause&pause)>nul & for /l %a in () do (set /p "data=" && echo(!date! !time! !data!)&ping -n 2 Google.com>nul" >C:\pingtest.txt

您只需将 Google.com 修改为您的服务器名称即可。它非常适合我。并记住完成后停止此操作。 pingtest.txt 文件将每分钟增加 4.5 KB(大约)。

感谢 raymond.cc。 https://www.raymond.cc/blog/timestamp-ping-with-hrping/


1
投票

试试这个:

ping -c2 -s16 sntdn | awk '{print NR " | " strftime("%Y-%m-%d_%H:%M:%S") " | " $0  }'

看看是否适合你


1
投票
ping -t wwww.google.com|cmd /q /v /c "(pause&pause)>nul & for /l %a in () do (set /p "data=" && echo(!date! !time! !data!)&ping -n 2 wwww.google.com>nul"

0
投票

另一种powershell方法(我只想失败)

$ping = new-object System.Net.NetworkInformation.Ping
$target="192.168.0.1"
Write-Host "$(Get-Date -format 's') Start ping to $target"
while($true){
    $reply = $ping.send($target)
    if ($reply.status -eq "Success"){
        # ignore success    
        Start-Sleep -Seconds 1
    }
    else{
        Write-Host "$(Get-Date -format 's') Destination unreachable" $target

    }
}

0
投票

MC ND 针对 Windows 的答案的增强。
我需要一个在 WinPE 中运行的脚本,因此我执行了以下操作:

@echo off
SET TARGET=192.168.1.1
IF "%~1" NEQ "" SET TARGET=%~1

ping -t %TARGET%|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!time! !data!)&ping -n 2 localhost >nul"

这可以硬编码到特定的 IP 地址(在我的示例中为

192.168.1.1
)或采用传递的参数。 正如 MC ND 的回答所示,大约每 1 秒重复一次 ping。


0
投票

简单😎:

@echo off

set hostName=www.stackoverflow.com
set logfile=C:\Users\Dell\Desktop\PING_LOG\NetworkLog\Log_%hostName%.text
echo Network Loging Running %hostName%...
echo Ping Log %hostName% >>%logfile%

:Ping
for /f "tokens=* skip=2" %%A in ('ping %hostName% -n 1 ') do (
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
    timeout 1 >NUL
    GOTO Ping)

0
投票

您可以在

ping -n 2 localhost
之前添加字符 R,而不是在循环末尾添加额外的
!data!
,因为唯一的可能性是回复或请求。第一个字符是从
pause>nul
消耗的。因此,不要使用以下表达式:

ping localhost -t -l 4|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p data=&echo(!date! !time! !data!)&ping -n 2 localhost>nul"

你可以使用这个表达:

ping localhost -t -l 4|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p data=&echo(!date! !time! R!data!)&pause>nul"

产生相同的输出,例如:

22:34:49.49 Reply from 172.217.4.46: bytes=4 time=14ms TTL=116
22:34:50.49 Reply from 172.217.4.46: bytes=4 time=14ms TTL=116
22:34:55.47 Request timed out.
22:34:56.49 Reply from 172.217.4.46: bytes=4 time=14ms TTL=116
22:34:57.49 Reply from 172.217.4.46: bytes=4 time=14ms TTL=116

0
投票

这是 Windows CMD 的一个衬垫,它写入带有时间戳的文件(在 ping 上具有无限循环)

FOR /L %N IN () DO date /t >>ping.txt && time /t >>ping.txt && ping google.com -n 4 >>ping.txt

这是一个仅 ping 4 次且带有时间戳的版本:

date /t >>ping.txt && time /t >>ping.txt && ping google.com -n 4 >>ping.txt

0
投票
ping.exe -t 1.1.1.1|Foreach{"{0} - {1}" -f (Get-Date),$_}
© www.soinside.com 2019 - 2024. All rights reserved.