为Ping记录加一个时间戳


最近公司网络不稳定,间歇性断流。为了检查网络状况。我们需要用ping来了解网络状况(丢包等等一些问题)。
ping的输出默认是不带时间戳的,所以很难定位故障时间。
下面一个脚本很好的解决了此问题。
代码另存为ping.vbs
用法为:cscript ping.vbs 192.168.1.2 -t > ping.txt
如果ping的输出是英文
那么脚本里的
re.Pattern=”^请求|^来自”
替换为:
re.Pattern=”^Reply|^Request”

Dim args, flag, unsuccOut
args=""
otherout=""
flag=0

If WScript.Arguments.count = 0 Then
WScript.Echo "Usage: cscript tping.vbs [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]"
WScript.Echo "                         [-s count] [[-j host-list] | [-k host-list]]"
WScript.Echo "                         [-r count] [-w timeout] destination-list"
wscript.quit
End if

For i=0 to WScript.Arguments.count - 1
args=args & " " & WScript.Arguments(i)
Next

Set shell = WScript.CreateObject("WScript.Shell")
Set re=New RegExp
re.Pattern="^请求|^来自"
Set myping=shell.Exec("ping" & args)

while Not myping.StdOut.AtEndOfStream
   strLine=myping.StdOut.ReadLine()
   r=re.Test(strLine)
   If r Then
WScript.Echo date & " "& time & chr(9) & strLine
flag=1
   Else
unsuccOut=unsuccOut & strLine
   End if
Wend

if flag = 0 then
WScript.Echo unsuccOut
end if

附上一个linux下的方法:

ping baidu.com -c 10 | awk '{ print $0"\t" strftime("%H:%M:%S-%D",systime()) } '

发表回复

This site uses Akismet to reduce spam. Learn how your comment data is processed.