查看: 12|回复: 0

vbs ping实现的两种方式

[复制链接]

0

主题

0

回帖

0

积分

积极分子

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2011-9-1
发表于 2020-3-31 10:56:20 | 显示全部楼层 |阅读模式

对于vbs中ping的实现可以通过两种方式 :

  • 1、调用系统ping命令;
  • 2、使用wmi查询pingstate类处理。

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

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部