gpt4 book ai didi

sql-server - VBA 获取计算机 VPN 状态

转载 作者:行者123 更新时间:2023-12-04 20:43:46 24 4
gpt4 key购买 nike

我正在尝试在我的代码中构建一种查看计算机是否可以访问它访问的 SQL Server 的方法。

问题是我不能只获得网络状态以到达计算机需要在 VPN 上的 SQL Server(通过 Cisco)

我在 VBA 中看到了 Cisco API 引用,但不知道如何使用它,也不能 100% 确定这将是一种故障安全的测试方法

我有两个这样做的想法:

  • Ping SQL Server(我的首选) - 我尝试使用此代码,但无论服务器是否连接,它都会返回“通过”

  • 我的功能:
    Sub test()
    ' SQL Server address instead of xxxxxxx
    If Ping("xxxxxxx") Then
    MsgBox "Passed", vbOKOnly
    Else
    MsgBox "Failed", vbOKOnly
    End If
    End Sub
    Public Function Ping(ByVal ComputerIP As String) As Boolean
    'You can use also name of computer
    ' Return TRUE, if pin was successful
    Dim oPingResult As Variant
    For Each oPingResult In GetObject("winmgmts://./root/cimv2").ExecQuery _
    ("SELECT * FROM Win32_PingStatus WHERE Address = '" & ComputerName & "'")
    If IsObject(oPingResult) Then
    If oPingResult.StatusCode = 0 Then
    Ping = True
    'Debug.Print "ResponseTime", oPingResult.ResponseTime 'You can also return ping time
    Exit Function
    End If
    End If
    Next
    End Function
  • 我的另一个想法是,如果计算机连接到 VPN,以太网 LAN 适配器会更改为具有特定于连接的 DNS 后缀(但是我认为这是一个虚拟适配器,所以我不能只测试名称,因为它可能不在重复测试中的相同适配器)

  • 因此,我的第二个想法是扫描所有连接以检索其特定于连接的 dns 后缀并测试正确的名称,但是我不确定如何提取此信息。

    最佳答案

    已经修复了上面的代码 - 刚刚意识到我调用了错误的变量来 ping。

    应该:
    Sub test()
    ' SQL Server address instead of xxxxxxx
    If Ping("xxxxxxx") Then
    MsgBox "Passed", vbOKOnly
    Else
    MsgBox "Failed", vbOKOnly
    End If
    End Sub
    Public Function Ping(ByVal ComputerIP As String) As Boolean
    'You can use also name of computer
    ' Return TRUE, if pin was successful
    Dim oPingResult As Variant
    For Each oPingResult In GetObject("winmgmts://./root/cimv2").ExecQuery _
    ("SELECT * FROM Win32_PingStatus WHERE Address = '" & ComputerIP & "'")
    If IsObject(oPingResult) Then
    If oPingResult.StatusCode = 0 Then
    Ping = True
    'Debug.Print "ResponseTime", oPingResult.ResponseTime 'You can also return ping time
    Exit Function
    End If
    End If
    Next
    End Function

    现在工作

    关于sql-server - VBA 获取计算机 VPN 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25160095/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com