惠东县文章资讯

vbs ping实现的两种方式

2026-03-31 15:32:02 浏览次数:0
详细信息

方式一:使用 WMI (Windows Management Instrumentation)

Function PingByWMI(strComputer)
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colPings = objWMIService.ExecQuery _
        ("Select * From Win32_PingStatus Where Address = '" & strComputer & "'")

    PingByWMI = False

    For Each objPing In colPings
        If objPing.StatusCode = 0 Then
            PingByWMI = True
            Exit For
        End If
    Next

    Set colPings = Nothing
    Set objWMIService = Nothing
End Function

' 使用示例
If PingByWMI("www.google.com") Then
    WScript.Echo "Ping 成功!"
Else
    WScript.Echo "Ping 失败!"
End If

方式二:使用 Shell 命令

Function PingByShell(strHost)
    Set objShell = CreateObject("WScript.Shell")

    ' 使用 ping 命令,发送 2 个包,超时 1000ms
    strCommand = "ping -n 2 -w 1000 " & strHost

    ' 执行命令并隐藏窗口
    Set objExec = objShell.Exec(strCommand)

    strResult = objExec.StdOut.ReadAll

    Set objExec = Nothing
    Set objShell = Nothing

    ' 检查结果中是否包含 "TTL=" 或 "Reply from"
    If InStr(1, strResult, "TTL=", vbTextCompare) > 0 Or _
       InStr(1, strResult, "Reply from", vbTextCompare) > 0 Then
        PingByShell = True
    Else
        PingByShell = False
    End If
End Function

' 使用示例
If PingByShell("192.168.1.1") Then
    WScript.Echo "主机可达"
Else
    WScript.Echo "主机不可达"
End If

综合示例:带详细信息的版本

' 综合版本:返回更多详细信息
Function DetailedPing(strHost)
    On Error Resume Next

    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colPings = objWMIService.ExecQuery _
        ("Select * From Win32_PingStatus Where Address = '" & strHost & "'")

    For Each objPing In colPings
        If objPing.StatusCode = 0 Then
            DetailedPing = "成功!延迟: " & objPing.ResponseTime & "ms"
        Else
            Select Case objPing.StatusCode
                Case 11001: DetailedPing = "失败:缓冲溢出"
                Case 11002: DetailedPing = "失败:目标主机不存在"
                Case 11003: DetailedPing = "失败:目标主机不可达"
                Case 11004: DetailedPing = "失败:目标协议不可达"
                Case 11005: DetailedPing = "失败:数据包过大"
                Case 11006: DetailedPing = "失败:请求超时"
                Case 11007: DetailedPing = "失败:请求被拒绝"
                Case 11008: DetailedPing = "失败:TTL过期"
                Case 11009: DetailedPing = "失败:参数错误"
                Case 11010: DetailedPing = "失败:数据包丢失"
                Case 11011: DetailedPing = "失败:未知错误"
                Case Else: DetailedPing = "失败:错误代码 " & objPing.StatusCode
            End Select
        End If
    Next

    If Err.Number <> 0 Then
        DetailedPing = "错误:" & Err.Description
    End If

    Set colPings = Nothing
    Set objWMIService = Nothing
End Function

' 使用示例
WScript.Echo "Ping 结果:" & DetailedPing("8.8.8.8")

两种方式的比较:

特性 WMI 方式 Shell 方式
速度 较快 较慢(需要创建进程)
权限 需要管理员权限 标准用户权限即可
信息 可获取详细统计信息 只能判断是否可达
隐蔽性 无可见窗口 可隐藏窗口
兼容性 Windows 2000+ 所有 Windows 版本

实用技巧:

批量测试

arrHosts = Array("192.168.1.1", "www.google.com", "localhost")

For Each host In arrHosts WScript.Echo host & ": " & DetailedPing(host) Next


2. **连续监控**:
```vbscript
Do
    If Not PingByWMI("重要服务器") Then
        WScript.Echo "[" & Now & "] 服务器不可达!"
    End If
    WScript.Sleep 5000  ' 等待5秒
Loop

选择哪种方式取决于你的具体需求:

相关推荐