0

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

Thanks in advance.

Carlos Barbosa
  • 1,422
  • 2
  • 23
  • 42
  • question belongs to superuser – Ibu Jun 26 '11 at 03:20
  • You need to provide more information if you want anyone to help, Carlos. – JohnZaj Jun 26 '11 at 03:32
  • 1
    Question seems pretty clear to me. How does one make a VBScript execute automatically upon a WiFi connection being established. Also this question is arguably about programming, as answers may eventually show. I'm usually a arrogant SOB about unclear questions, but this one is perfectly fair (with the caveat that it shows no research effort whatsoever -- but this isn't an easy one to search for). – Jean-François Corbett Jun 27 '11 at 09:17

2 Answers2

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
unrealtrip
  • 670
  • 4
  • 13
0

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..

JohnZaj
  • 3,080
  • 5
  • 37
  • 51