如何在Windows XP中检测到Internet连接后启动vbs脚本?

时间:2011-06-26 03:16:30

标签: windows windows-services vbscript windows-xp registry

如何在建立wifi连接后启动vbs脚本?

提前致谢。

2 个答案:

答案 0 :(得分:1)

您可以启动一个VBScript,循环查找来自互联网站点/设备/响应的响应。当它看到它时,它将执行代码,否则它将尝试最多XX分钟并中止,例如:

Const strTarget = "cnn.com"

startTime = Time
boolExitFlag = False

Do

    ' Check to see if I can get a ping response from target
    If Ping(strTarget) Then

        ' Call the code to run on connect
        Call runOnWIFI              
        boolExitFlag = True
    End If


    WScript.sleep 1000 ' Pause for 1 seconds before next attempt

    ' Stop trying after 5 minutes   
    If DateDiff("s", startTime, time) => 300 then boolExitFlag = True

Loop while boolExitFlag <> True



' * * * * * * * * * * * * * * * * * * * * * * * * * * * 
' Subroutine to run when WIFI connection is detected
' * * * * * * * * * * * * * * * * * * * * * * * * * * * 
Sub runOnWIFI

    ' INSERT CODE TO RUN ON WIFI CONNECTION HERE

End Sub



' * * * * * * * * * * * * * * * * * * * * * * * * * * * 
' Subroutine to see if the target machine is online
' * * * * * * * * * * * * * * * * * * * * * * * * * * * 
Function Ping(strHost)

    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address = '" & strHost & "'")

    z = 0
    Do    
        z = z + 1
        For Each objRetStatus In objPing        
            If IsNull(objRetStatus.StatusCode) Or objRetStatus.StatusCode <> 0 Then            
                PingStatus = False        
            Else
                PingStatus = True              
            End If      
        Next    

        ' Try a few times in case machine doesn't respond right away
        wscript.sleep 200
        If z = 4 Then Exit Do

    Loop until PingStatus = True

    If PingStatus = True Then 
        Ping = True
    Else
        Ping = False
    End If

End Function

答案 1 :(得分:0)

您的应用程序只需使用cscript.exe运行.vbs文件即可。例如

cscript.exe ScriptToLaunch.vbs

要检测互联网连接,您只需使用某种“ping”命令即可。例如,请参阅VBS to check for active internet connection并将其调整为..您正在使用的任何开发堆栈。