VBS:检查IP地址并打开URL(但仅一次)

发布于 2024-09-19 11:05:38 字数 1201 浏览 3 评论 0原文

我正在尝试创建一个 VBScript,它将通过检查其 IPv4 地址(由 DHCP 分配)来检测正在运行的计算机是否连接到我们工作中的 LAN,然后根据它是在内部还是外部打开一个特定的 URL我们的网络。该脚本将主要用于在工作(10.12.90.0/22)和家庭(通常是 192.168/23,但这可能是任何东西)之间漫游的笔记本电脑。在这两种情况下,我只需要打开一次正确的 URL,因为几乎总是有多个网络适配器(有线/无线/蓝牙等)。

下面的脚本在我测试时似乎可以工作,但作为一名程序员,我不确定是否有更好的方法来做到这一点。理想情况下,我希望避免因为延迟而对服务器执行 ping 操作。

strComputer = "."

strInternal = "http://intranet/"
strExternal = "http://www.mydomain.com/"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True",,48)

For Each objItem in colItems
    strIPAddress = objItem.IPAddress(0)
    arrIPAddress = Split(strIPAddress, ".")

    If (arrIPAddress(0) = "10") And (arrIPAddress(1) = "12") Then
        ipChecked = 1
        Run strInternal
    Else
        If ipChecked = 1 Then
            WScript.Sleep(10)
        Else
            ipChecked = 1
            Run strExternal
        End If
    End If

Next

Sub Run(ByVal sFile)
Dim shell
    Set shell = CreateObject("WScript.Shell")
    shell.Run Chr(34) & sFile & Chr(34), 1, false
    Set shell = Nothing
End Sub

I'm trying to create a VBScript which will detect if the computer it is being run on is connected to our LAN at work by checking its IPv4 address (assigned by DHCP) and then open a specific URL depending upon whether it is inside or outside our network. The script will be mainly used on laptops which will roam between work (10.12.90.0/22) and home (usually 192.168/23, but this could be anything really). In both cases I need to open the corect URL only once, because there will almost always be more than one network adapter (wired/wireless/bluetooth etc.).

The script below appears to work when I tested it, but not being a programmer I am not sure if there is a better way to do this. Ideally, I would like to avoid pinging servers because of the delay.

strComputer = "."

strInternal = "http://intranet/"
strExternal = "http://www.mydomain.com/"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True",,48)

For Each objItem in colItems
    strIPAddress = objItem.IPAddress(0)
    arrIPAddress = Split(strIPAddress, ".")

    If (arrIPAddress(0) = "10") And (arrIPAddress(1) = "12") Then
        ipChecked = 1
        Run strInternal
    Else
        If ipChecked = 1 Then
            WScript.Sleep(10)
        Else
            ipChecked = 1
            Run strExternal
        End If
    End If

Next

Sub Run(ByVal sFile)
Dim shell
    Set shell = CreateObject("WScript.Shell")
    shell.Run Chr(34) & sFile & Chr(34), 1, false
    Set shell = Nothing
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猛虎独行 2024-09-26 11:05:38

检查内部网站是否可用,如果可用则加载它,否则加载公共网站怎么样?您可以使用这样的函数:

Function UrlExists(xURL)
    On Error Resume Next
    Err.Clear
    Dim objXML

    Set objXML = CreateObject("Microsoft.XMLHTTP")
    objXML.Open "HEAD",xURL,False
    objXML.Send

    If Err.Number <> 0 Or objXML.Status <> 200 Then
        UrlExists = False
    Else
        UrlExists = True
    End If
    Set objXML = Nothing
End Function

然后从主脚本中调用它:

strInternal = "http://intranet/default.htm"       
strExternal = "http://www.mydomain.com/"  

If URLExists(strInternal) Then
 Run strInternal
Else
 Run strExternal 
End If  

How about checking to see if the internal website is available, load it if it is else load the public website? You could use a function like this:

Function UrlExists(xURL)
    On Error Resume Next
    Err.Clear
    Dim objXML

    Set objXML = CreateObject("Microsoft.XMLHTTP")
    objXML.Open "HEAD",xURL,False
    objXML.Send

    If Err.Number <> 0 Or objXML.Status <> 200 Then
        UrlExists = False
    Else
        UrlExists = True
    End If
    Set objXML = Nothing
End Function

and then call it from your main script:

strInternal = "http://intranet/default.htm"       
strExternal = "http://www.mydomain.com/"  

If URLExists(strInternal) Then
 Run strInternal
Else
 Run strExternal 
End If  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文