问题
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