时间:2020-11-20 18:24:58 | 栏目:VBS | 点击:次
对于vbs中ping的实现可以通过两种方式 :
1、调用系统ping命令
Set wshell = CreateObject("WScript.Shell") wshell.run("ping 182.183.101.1",0.true)
对于以上调用,如果想对其进行过滤,可以考虑将运行结果重定向到文件,在读到一个string中,查找其中是否有timeout或超时字符,判断是否超时。本打算直接拼接命令重定向到文件,但怎么都不成功,所以run调用一个bat,bat中写:ping 192.168.101.1 -n 1 -w 1200
2、使用wmi查询pingstate类处理:
'url = "www.baidu.com" url = "119.75.217.109" strComputer = "." Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colPings = objWMIService.ExecQuery ("Select * From Win32_PingStatus where Address = '" & url & "'") For Each objPing in colPings MsgBox url & " responded to ping." & vbcrlf &_ "Responding Address: " & objPing.ProtocolAddress & vbcrlf &_ "Responding Name: " & objPing.ProtocolAddressResolved & vbcrlf &_ "Bytes Sent: " & objPing.BufferSize & vbcrlf &_ "Time: " & objPing.ResponseTime & " ms" Next