Thursday, October 30, 2008

Disconnect GPRS connection if your NIC is connected

This is a VBScript I created some time ago. It will connect your GPRS connection if your NIC is not connected and disconnect the GPRS connection if the NIC is connected. It will work with other dial-up connections as well.

The script will run as a service with SrvAny and will reconnect the GPRS connection if the connection was dropped. I'm using WMI to determine the status of the LAN connection. For some reason I did not find the correct status of the GPRS connection in WMI and needed to use the output of ipconfig to determine if GPRS was still up.

Dim Shell, Hell, GPRS, ExecuteObj, strcmd, GPRSConnected

On error resume next

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

Do
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 connection disconnected!"
Shell.Run ("rasdial GPRS"), 0
GPRS = "1"
End if
Else
If GPRS = "1" Then
' Wscript.Echo "LAN connection connected!"
Shell.Run ("rasdial GPRS /DISCONNECT"), 0
GPRS = "0"
End if
End if
Next

If GPRS = "1" Then
Set ExecuteObj = Shell.Exec("ipconfig.exe")
Do While Not ExecuteObj.StdOut.AtEndOfStream
strcmd = ExecuteObj.StdOut.ReadLine()
If Instr(strcmd, "PPP") > 0 Then
GPRSConnected = "1"
End if
Loop
If not GPRSConnected = "1" Then
' Wscript.Echo "GPRS connection disconnected!"
Shell.Run ("rasdial GPRS"), 0
End if
Set ExecuteObj = nothing
Set GPRSConnected = nothing
End if

Wscript.Sleep 10000

Loop Until Hell="Freezes over!"

No comments: