Monday, February 19, 2007

Dial GPRS connection if no LAN is available

Yes, I know. You could use a commercial tool to acomplish the following: dial a GPRS connection whenever no LAN (Or WLAN) is available. I decided to brew my own little script to do this.

The VB script dials the RAS connection "GPRS" when the connection with the name "Local Area Connection" is not connected to a network. The script does not check if this LAN connects to the internet.

Dim Shell, Hell, GPRS

Set Shell = CreateObject("WScript.Shell")
strComputer = "."
GPRS = "0"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapter Where NetConnectionID = 'Local Area Connection'")

For Each objItem in colItems
If objItem.NetConnectionStatus = 7 Then
If GPRS = "0" Then
' Wscript.Echo "LAN verbinding verbroken! Status: " & objItem.NetConnectionStatus
Shell.Run ("rasdial GPRS")
GPRS = "1"
End if
If GPRS = "1" Then
' Wscript.Echo "LAN verbinding actief! Status: " & objItem.NetConnectionStatus
Shell.Run ("rasdial GPRS /DISCONNECT")
GPRS = "0"
End if
End if
Wscript.Sleep 10000
Loop Until Hell="Freezes over!"

No comments: