Jump to content

HowTo toggle (disable/enable) a NIC using AutoIt?


Recommended Posts

I know this is an old thread, but I thought I'd add some to it. I modified the code some to meet my needs, and some may find it useful.

The first iteration is an app which enumerates and displays your existing NICs and allows you to disable them individually

Neat i like this layout too. I have a couple of suggestions though, why not just have the action occur when you click a checkbox instead of having an apply button; also It's better to ReDim your arrays than to create one with WAY more capacity than you can ever expect to use.

Dim $Checkbox[100]
Dim $Label[100]

I'm be happy to lend a hand with that logic, or check the helpfile for ReDim.

Edited by spudw2k
Link to comment
Share on other sites

Where my credit? :P QuickNIC

Oh crap, I'm really sorry about that. I didn't realize the code came from another thread, but after re-reading it, I see my error! :)

As for the DIM statements, I've actually done that in other scripts, but for prototyping I just used the Dim $Checkbox[100] and forgot to change it.

Here's my current version of this, which actually assigns a "label" to each card defined in an INI file. It's currently limited to 2, but could easily be increased to more by either adding a new $Network3 array and if statement, or (preferably) converting the $Network array into a multi-dimensional array and using a single if statement in a loop. I only needed 2 :(

;======================================================================================
;
; SwitchNetworks -  displays a systray app which allows you to switch between to NICs
;
; Author:       Tim Morris
;
; Credits:      Thanks to Danny35d, spudw2k (QuickNIC) and Cherdeg for the info from the AutoIT Forum
;               (http://www.autoitscript.com/forum/lofiversion/index.php?t72165.html)
;
;======================================================================================

#RequireAdmin
#include <Constants.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>

Opt("TrayMenuMode", 1)
Global $OSver = @OSVersion
Global $strEnable = "En&able"
Global $strDisable = "Disa&ble"
Global $colNetwork = ""
Global $msg

Dim $NicsFound[1][1] ; $NicsFound['NIC name']['NIC status (0=disabled)']
Dim $Network1[3] ; $Network1[1]=systray label for network 1, $network1[2]=network 1 NIC device
Dim $Network2[3] ; $Network2[1]=systray label for network 2, $network2[2]=network 2 NIC device
Dim $Icon[3]

_GetConfig() ; Read INI file or set defaults

TraySetIcon($Icon[1], $Icon[2])
TraySetToolTip("SwitchNetwork")

; Find the folder containing the network connection objects
; ==============================================================================================
Func _NetConsFolderObject()
    Local $wbemFlagReturnImmediately = 0x10
    Local $wbemFlagForwardOnly = 0x20
    Local $strComputer = "localhost"
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colNetwork = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    Return $colNetwork
EndFunc   ;==>_NetConsFolderObject

; Find the network connection objects
; ==============================================================================================
Func _GetNetConNames($colNetwork)
    Dim $strNetworks, $IDXname=0, $IDXstatus=0
    If IsObj($colNetwork) Then
        For $clsConn In $colNetwork
            If $clsConn.NetConnectionID <> '' Then 
                $strNetworks &= $clsConn.NetConnectionID & '|'
                ReDim $NicsFound[$IDXname+1][2]
                $NicsFound[$IDXname][0] = $clsConn.NetConnectionID
                $NicsFound[$IDXname][1] = $clsConn.NetConnectionStatus
                $IDXname = $IDXname + 1
            EndIf
        Next
    Else
        MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_NetworkAdapter")
    EndIf
    Return
EndFunc

Func AddTrayItems($arrNetworks)
    $Network1[0]=TrayCreateItem($Network1[1])
    $Network2[0]=TrayCreateItem($Network2[1])
    For $i = 0 To UBound($NicsFound, 1) - 1
        If $NicsFound[$i][0] = $Network2[2] Then
            If $NicsFound[$i][1] = 0 Then
                TrayItemSetState($Network2[0], 4)
            Else
                TrayItemSetState($Network2[0], 1)
            EndIf
        ElseIf $NicsFound[$i][0] = $Network1[2] Then
            If $NicsFound[$i][1] = 0 Then
                TrayItemSetState($Network1[0], 4)
            Else
                TrayItemSetState($Network1[0], 1)
            EndIf
        EndIf
    Next
EndFunc   ;==>AddTrayItems

Func _NicToggle($NetworkEnable, $NetworkDisable)
    If $OSver = "WIN_VISTA"  Then
        _NetConsFolderObject()
        If IsObj($colNetwork) Then
            For $clsConn In $colNetwork
                If $clsConn.NetConnectionID = $NetworkEnable[2] Then
                        $clsConn.Enable
                        TrayItemSetState($NetworkEnable[0], 1)
                ElseIf $clsConn.NetConnectionID = $NetworkDisable[2] Then
                    $clsConn.Disable
                    TrayItemSetState($NetworkDisable[0], 4)
                EndIf
            Next
        Else
            MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_NetworkAdapter")
        EndIf
    Else ; Windows 2000/XP/2003
        $objShell = ObjCreate("Shell.Application")
        $strNetConn = "Network Connections"
        $objCP = $objShell.Namespace(3)
        For $clsConn In $objCP.Items
            If $clsConn.Name = $strNetConn Then
            $colNetwork = $clsConn.GetFolder
            EndIf
        Next
        For $clsConn In $colNetwork.Items           
            If $clsConn.Name = $NetworkEnable[2] Then
                For $clsVerb In $clsConn.verbs
                    If $clsVerb.name = $strEnable Then
                        $clsVerb.DoIt
                        TrayItemSetState($NetworkEnable[0], 1)
                        Sleep(200)
                    EndIf
                Next
            ElseIf $clsConn.Name = $NetworkDisable[2] Then
                For $clsVerb In $clsConn.verbs
                    If $clsVerb.name = $strDisable Then
                        $clsVerb.DoIt
                        TrayItemSetState($NetworkDisable[0], 4)
                        Sleep(200)
                    EndIf
                Next
            EndIf
        Next
    EndIf
EndFunc   ;==>_NicToggle

Func _GetConfig()
    ;~ Sample SwitchNetwork.ini file:
    ;~ [Network1]
    ;~ Label=Brown
    ;~ NIC=Local Area Connection
    ;~ [Network2]
    ;~ Label=Green
    ;~ NIC=Wireless Network Connection  
    ;~ [Icons]
    ;~ TrayIcon=Shell32.dll
    ;~ Ordinal=-150
    
    $var1 = IniReadSection(@scriptdir & "\SwitchNetwork.ini", "Network1")
    $var2 = IniReadSection(@scriptdir & "\SwitchNetwork.ini", "Network2")
    $var3 = IniReadSection(@scriptdir & "\SwitchNetwork.ini", "Icons")
    If @error Then 
        SplashTextOn("INI Problem", @CRLF & "INI file is missing or corrupt." & @CRLF &  @CRLF & "Using defaults.", 300, 90, -1, -1, 1, "", 10)
        Sleep(4000)
        SplashOff()
        $Network1[1] = "Brown"
        $Network1[2] = "Local Area Connection"
        $Network2[1] = "Green"
        $Network2[2] = "Wireless Network Connection"
        $Icon[1] = "Shell32.dll"
        $Icon[2] = "-150"
    Else
        For $i = 1 To $var1[0][0]
            If $var1[$i][0] = "Label" Then $Network1[1] = $var1[$i][1]
            If $var1[$i][0] = "NIC" Then $Network1[2] = $var1[$i][1]
        Next
        For $i = 1 To $var2[0][0]
            If $var2[$i][0] = "Label" Then $Network2[1] = $var2[$i][1]
            If $var2[$i][0] = "NIC" Then $Network2[2] = $var2[$i][1]
        Next
        For $i = 1 To $var3[0][0]
            If $var3[$i][0] = "TrayIcon" Then $Icon[1] = $var3[$i][1]
            If $var3[$i][0] = "Ordinal" Then $Icon[2] = $var3[$i][1]
        Next
    EndIf
EndFunc   ;==>_GetConfig()

; ========================================================================================
; Begin Main Script
; ========================================================================================

$trayabout = TrayCreateItem("About")
$trayexit = TrayCreateItem("Exit")
$separator = TrayCreateItem("")

_NetConsFolderObject()
AddTrayItems(_GetNetConNames(_NetConsFolderObject()))
TraySetState()

While 1
    $msg = TrayGetMsg()

    If $msg > 0 And $msg <> $trayabout And $msg <> $trayexit Then
        $ChangeNet = TrayItemGetText($msg)
        ;ConsoleWrite($msg & ' ' & TrayItemGetState($msg) & ' = ' & $TRAY_CHECKED & ' ' & BitAND(TrayItemGetState($msg), $TRAY_CHECKED) & @crlf)                                
        If $ChangeNet = $Network2[1] Then
            _NicToggle($Network2, $Network1)
        ElseIf $ChangeNet = $Network1[1] Then
            _NicToggle($Network1, $Network2)
        EndIf
    EndIf

    If $msg = $trayabout Then
        SplashTextOn("About...", @CRLF & "SwitchNetwork allows you to quickly switch between network connections." & @CRLF & @CRLF & "10/16/2008" & @CRLF & @CRLF & "v0.1" & @CRLF, 300, 120, -1, -1, 1, "", 10)
        Sleep(4000)
        SplashOff()
    EndIf

    If $msg = $trayexit Then Exit
WEnd

and here's a sample INI file:

[Network1]
    Label=Brown
    NIC=Local Area Connection
[Network2]
    Label=Green
    NIC=Wireless Network Connection 
[Icons]
    TrayIcon=Shell32.dll
    Ordinal=-150

Again, sorry about the lack of credit, it wasn't intentional... (I can't edit my posts, but if someone can, I give them permission to correct the error :idea: )

-- Tim

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...