How to launch vbs script after internet connection is detected in windows xp? [closed]

房东的猫 提交于 2019-12-25 02:15:03

问题


How can i launch a vbs script after the wifi connection has been stablished?

Thanks in advance.


回答1:


You could launch a VBScript that loops looking for a response from an internet site/device/whatever. When it sees it, it will execute code, otherwise it will try for up to XX minutes and abort, for example:

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



回答2:


Your application can simply run the .vbs file with cscript.exe. For example

cscript.exe ScriptToLaunch.vbs

To detect internet connection, you can simply use a 'ping' command of some sort. For example, see VBS to check for active internet connection and adapt this to..whatever development stack it is you are using..



来源:https://stackoverflow.com/questions/6481924/how-to-launch-vbs-script-after-internet-connection-is-detected-in-windows-xp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!