Jump to content

Creating a Lan/Wan Switching Script


Recommended Posts

I am starting to get some new devices that either do not have Wan/Lan switching because they are tablets using external lan adapters or some new laptops that the Lan/Wan switching just does not work.

 

This seems like something that I could do in AutoIT pretty easy, but it would need to be solid and reliable to replace a built in Bios function.

Anybody tried this before?  I imagine I need to probably use DevCon or something and monitor my interface connection states.

Win8+ I have the get-netadapter cmdlet that would probably work well, but this environment is using Windows 7 :(

So maybe Get-WmiObject Win32_NetworkAdapter and look at Connection Status.  2 should be connected.

I could parse an IPConig or something to see if I get an IP for a given network adapter.

 

So yeah those are my starting ideas, kinda looking for the starting point and then I can try to put together some code.

Link to comment
Share on other sites

Ok this is what I came up with, so far looks solid in the information gathering, not sure if it will have issues in the field with multiple user logins etc so its not tested yet, nor have I tried the actual on/off function.

#RequireAdmin
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>

#pragma compile(Icon, E:\Users\it022565\Desktop\Desktop\System Tools\iconsext\icons\imageres_34.ico)

$sLanIndex = RegRead("HKLM\SOFTWARE\BoCC Automation\Network", "Lan Index")
$sWanIndex = RegRead("HKLM\SOFTWARE\BoCC Automation\Network", "Wan Index")

If $sLanIndex = "" Then
    $sLanIndex = InputBox("BoCC Automation", "Enter Lan Interface Index Number", "")
    $sWanIndex = InputBox("BoCC Automation", "Enter Wan Interface Index Number", "")
    RegWrite("HKLM\SOFTWARE\BoCC Automation\Network", "Lan Index", "REG_DWORD", $sLanIndex)
    RegWrite("HKLM\SOFTWARE\BoCC Automation\Network", "Wan Index", "REG_DWORD", $sWanIndex)
EndIf


AdlibRegister("CheckConnections", 5000)

;wmic nic get name, index, netconnectionstatus
;wmic nic where index=x get netconnectionstatus


While 1
    Sleep(10)
WEnd


Func CheckLan()
Local $iPID = Run(@comspec & ' /c wmic nic where index=' & $sLanIndex & ' get netconnectionstatus', "", @SW_HIDE, $STDOUT_CHILD)
ProcessWaitClose($iPID)
Local $sOutput = StdoutRead($iPID)
;MsgBox(0, "", $sOutput)
Local $aArray = StringSplit(StringTrimRight(StringStripCR($sOutput), StringLen(@CRLF)), @CRLF)
;_ArrayDisplay($aArray)
Return $aArray[2]
EndFunc


Func CheckWan()
Local $iPID = Run(@comspec & ' /c wmic nic where index=' & $sWanIndex & ' get netconnectionstatus', "", @SW_HIDE, $STDOUT_CHILD)
ProcessWaitClose($iPID)
Local $sOutput = StdoutRead($iPID)
;MsgBox(0, "", $sOutput)
Local $aArray = StringSplit(StringTrimRight(StringStripCR($sOutput), StringLen(@CRLF)), @CRLF)
;_ArrayDisplay($aArray)
Return $aArray[2]
EndFunc

Func CheckConnections()
    If CheckLan() = 2 Then
        If CheckLan() = 2 AND CheckWan() = 2 Then
            DisableWan()
        EndIf
    Else
        If CheckWan() <> 2 Then
            EnableWan()
        EndIf
    EndIf
EndFunc

Func DisableWan()
    Run(@ComSpec & ' /c wmic path win32_networkadapter where index=' & $sWanIndex & ' call disable', "", @SW_HIDE)
EndFunc

Func EnableWan()
    Run(@ComSpec & ' /c wmic path win32_networkadapter where index=' & $sWanIndex & ' call enable', "", @SW_HIDE)
EndFunc

 

Edit: Tested and verified.  Working great, across multiple users and even is reconnecting to WAN on disconnect :)

Next thing for me to do in freetime is add some error checking and make this so you can enter more than one Wan/Lan index for machines with multiple interfaces.

 

Edited by ViciousXUSMC
Link to comment
Share on other sites

So threw together a multi interface version seems to work, although I did manage to get it to crash on once saying it could not access an array, and also once I had it not actually disable interfaces until I unplugged and plugged back in a usb lan adapter (maybe that was a Windows issue more so than the script.)

 

This brings to question, I cant exactly see what like 3367 or what ever it was is on the script, is there anyway to debug a compiled script or will I have to run this uncompiled until I can crash it again so I can reference the line number properly.

 

I also know there are a few debug options I can turn on, where the icon shows what line is currently running, but that would help more to see where it stalls more so than where it crashes.

 

Code below, somebody may see an error in my logic that needs to be fixed, but it is working most of the time.

 

Code:

#RequireAdmin
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>

#pragma compile(Icon, E:\Users\it022565\Desktop\Desktop\System Tools\iconsext\icons\imageres_34.ico)

$sLanIndex = RegRead("HKLM\SOFTWARE\BoCC Automation\Network", "Lan Index")
$sWanIndex = RegRead("HKLM\SOFTWARE\BoCC Automation\Network", "Wan Index")

If $sLanIndex = "" Then
    $sLanIndex = InputBox("BoCC Automation", "Enter Lan Interface Index Number(s)", "1,3,7")
    $sWanIndex = InputBox("BoCC Automation", "Enter Wan Interface Index Number(s)", "1,3,7")
    RegWrite("HKLM\SOFTWARE\BoCC Automation\Network", "Lan Index", "REG_SZ", $sLanIndex)
    RegWrite("HKLM\SOFTWARE\BoCC Automation\Network", "Wan Index", "REG_SZ", $sWanIndex)
EndIf

$aLanIndex = StringSplit($sLanIndex, ",")
$aWanIndex = StringSplit($sWanIndex, ",")

AdlibRegister("CheckConnections", 5000)

;wmic nic get name, index, netconnectionstatus
;wmic nic where index=x get netconnectionstatus


While 1
    Sleep(10)
WEnd


Func CheckLan()
Local $aResults[$aLanIndex[0]+1]
For $i = 1 to $aLanIndex[0]
Local $iPID = Run(@comspec & ' /c wmic nic where index=' & $aLanIndex[$i] & ' get netconnectionstatus', "", @SW_HIDE, $STDOUT_CHILD)
ProcessWaitClose($iPID)
Local $sOutput = StdoutRead($iPID)
;MsgBox(0, "", $sOutput)
Local $aArray = StringSplit(StringTrimRight(StringStripCR($sOutput), StringLen(@CRLF)), @CRLF)
;_ArrayDisplay($aArray)
$aResults[0] = $aLanIndex[0]
$aResults[$i] = $aArray[2]
Sleep(200)
Next
Return $aResults
EndFunc


Func CheckWan()
Local $aResults[$aWanIndex[0]+1]
For $i = 1 to $aWanIndex[0]
Local $iPID = Run(@comspec & ' /c wmic nic where index=' & $aWanIndex[$i] & ' get netconnectionstatus', "", @SW_HIDE, $STDOUT_CHILD)
ProcessWaitClose($iPID)
Local $sOutput = StdoutRead($iPID)
;MsgBox(0, "", $sOutput)
Local $aArray = StringSplit(StringTrimRight(StringStripCR($sOutput), StringLen(@CRLF)), @CRLF)
;_ArrayDisplay($aArray)
$aResults[0] = $aWanIndex[0]
$aResults[$i] = $aArray[2]
Sleep(200)
Next
Return $aResults
EndFunc

Func CheckConnections()
    If _ArraySearch(CheckLan(), 2, 1) <> -1 Then
        If _ArraySearch(CheckLan(), 2, 1) <> -1 AND _ArraySearch(CheckWan(), 2, 1) <> -1 Then
            DisableWan()
        EndIf
    Else
        If _ArraySearch(CheckWan(), 2, 1) = -1 Then
            EnableWan()
        EndIf
    EndIf
EndFunc

Func DisableWan()
    For $i = 1 to $aWanIndex[0]
    RunWait(@ComSpec & ' /c wmic path win32_networkadapter where index=' & $aWanIndex[$i] & ' call disable', "", @SW_HIDE)
    Sleep(200)
    Next
EndFunc

Func EnableWan()
    For $i = 1 to $aWanIndex[0]
    RunWait(@ComSpec & ' /c wmic path win32_networkadapter where index=' & $aWanIndex[$i] & ' call enable', "", @SW_HIDE)
    Sleep(200)
    Next
EndFunc

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...