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

时间:2010-09-12 12:13:58

标签: windows networking vbscript ip-address

我正在尝试创建一个VBScript,通过检查其IPv4地址(由DHCP分配)检测运行的计算机是否在运行时连接到我们的LAN,然后根据它是否是打开特定URL在我们的网络内部或外部。该脚本将主要用于笔记本电脑,它将在工作(10.12.90.0/22)和家庭(通常是192.168 / 23,但这可能是真的)之间漫游。在这两种情况下,我只需要打开一次corect 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

1 个答案:

答案 0 :(得分:1)

如何检查内部网站是否可用,如果是加载公共网站,请加载它?你可以使用这样的函数:

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