1. 程式人生 > >Tools:實現ping操作帶時間戳【windows+linux】

Tools:實現ping操作帶時間戳【windows+linux】

【windows下】:

ping.vbs

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="^Reply|^Request|^來自|^請求" Set myping=shell.Exec("
ping" & args) while Not myping.StdOut.AtEndOfStream strLine=myping.StdOut.ReadLine() 'WScript.Echo "原資料" & chr(9) & strLine 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

ping.bat

cscript ping.vbs 10.8.115.127 -n 10 >ping_log.txt

執行ping.bat即可

效果如下:

Microsoft (R) Windows Script Host Version 5.812
版權所有(C) Microsoft Corporation。保留所有權利。

2018/11/19 14:17:13 來自 10.8.115.115 的回覆: 位元組=32 時間<1ms TTL=200
2018/11/19 14:17:14 來自 10.8.115.115 的回覆: 位元組=32 時間<1ms TTL=200
2018/11/19 14:17:15 來自 10.8.115.115 的回覆: 位元組=32 時間<1ms TTL=200
2018/11/19 14:17:16 來自 10.8.115.115 的回覆: 位元組=32 時間=1ms TTL=200

 

 

 

【linux下】

ping $1 |while read line;do echo `date`$line;done >> ping_test.log

效果:

[email protected]/root/chen#ping 10.8.210.101 |while read line;do echo `date`$line;done
Mon Nov 19 14:23:42 CST 2018PING 10.8.210.101 (10.8.210.101) 56(84) bytes of data.
Mon Nov 19 14:23:42 CST 201864 bytes from 10.8.210.101: icmp_seq=1 ttl=64 time=0.257 ms
Mon Nov 19 14:23:43 CST 201864 bytes from 10.8.210.101: icmp_seq=2 ttl=64 time=0.163 ms
Mon Nov 19 14:23:44 CST 201864 bytes from 10.8.210.101: icmp_seq=3 ttl=64 time=0.141 ms
Mon Nov 19 14:23:45 CST 201864 bytes from 10.8.210.101: icmp_seq=4 ttl=64 time=0.160 ms
Mon Nov 19 14:23:46 CST 201864 bytes from 10.8.210.101: icmp_seq=5 ttl=64 time=0.148 ms