How to launch vbs script after internet connection is detected in windows xp? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
开发者_JAVA技巧Closed 8 years ago.
Improve this questionHow can i launch a vbs script after the wifi connection has been stablished?
Thanks in advance.
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
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..
精彩评论