1、方法一
打开 powershell命令窗口
ping.exe -t 8.8.8.8 | Foreach{"{0} - {1}" -f (Get-Date),$_} >c:\pingtest.txt
2、方法二
本部分引用前辈的代码。首先创建一个文本文档名字为Pingtest.txt,内容如下:
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
然后修改本文件Pingtest.txt后缀名为.vbs。即Pingtest.vbs
创建文件的路径以后要用到,注意全英文路径,本示例在D:\下
注意:以上内容编码均使用ANSI编码方式,且空格是在英文状态下输入的
同上,创建一个文本文档名字为Ping.txt,内容如下:
cscript D:\pingtest.vbs 192.168.1.1 -t -l 100 -w 5000>log.txt
1
然后修改本文件Pingtest.txt后缀名为.bat。即Pingtest.bat
参数解读:
D:\pingtst.vbs 为上一步中创建文件的路径
192.168.1.1 修改为你要测试的ip地址,log 为记录日志的文件名
-t 一直ping
-l 封包大小
-w 超时时间
再次注意:
以上内容编码均使用ANSI编码方式,且空格是在英文状态下输入的。