Jump to content

Disable/Enable Local Area Connection


Recommended Posts

This question has been asked before but I have done alot of research and found nothing.

"Does anyone know a way to enable/disable a Local Area Connection?"

I don't want to use Devcon or something else that has to be installed. Valik mentioned that there are some vbs scripts but I couldn't find any. Please someone help me out with this.

Thanks in advance.

Link to comment
Share on other sites

Yes but I am trying to disable the Network adapter. For example, if you manually go into device manager and right click the network adapter and select disable or if you right click local area connection and select disable. I am trying to to either of these by some kind of command line.

Link to comment
Share on other sites

This question has been asked before but I have done alot of research and found nothing. 

          "Does anyone know a way to enable/disable a Local Area Connection?"

I don't want to use Devcon or something else that has to be installed.  Valik mentioned that there are some vbs scripts but I couldn't find any.  Please someone help me out with this.

Thanks in advance.

<{POST_SNAPBACK}>

Hello DjDeep,

Here is a script-only solution to disable/enable a network adapter.

It requires AutoIt3 Beta version 3.1.1.40 or higher.

You might have to modify it according to your operating system language.

; Disable and Enable a Network card using 'Shell.Application'
;
; See also: http://blog.mvpcn.net/icuc88/articles/244.aspx
;
; To do: Rewrite this into a UDF. Parameters: $oLanConnection and $bEnabled


$oLanConnection = "Local Area Connection"; Change this to the name of the adapter to be disabled !
$bEnable = true             ; Change this to 'false' to DISABLE the network adapter

if @OSType<>"WIN32_NT" then
    Msgbox(0,"","This script requires Windows 2000 or higher.")
    exit
endif


if @OSVersion="WIN_2000" then
    $strFolderName = "Network and Dial-up Connections"
else
    $strFolderName = "Network Connections"; Windows XP
endif

Select 
    Case StringInStr("0409,0809,0c09,1009,1409,1809,1c09,2009,_
                    2409,2809,2c09,3009,3409", @OSLang) ; English (United States)
       $strEnableVerb  = "En&able"
       $strDisableVerb = "Disa&ble"

; Add here the correct Verbs for your Operating System Language
EndSelect


;Virtual folder containing icons for the Control Panel applications. (value = 3)
Const $ssfCONTROLS = 3 

$ShellApp = ObjCreate("Shell.Application")
$oControlPanel = $shellApp.Namespace($ssfCONTROLS)


; Find 'Network connections' control panel item
$oNetConnections=""

For $FolderItem in $oControlPanel.Items
    If $FolderItem.Name = $strFolderName then
        $oNetConnections = $FolderItem.GetFolder
        Exitloop
    Endif
Next

      
If not IsObj($oNetConnections) Then
    MsgBox(0,"Error","Couldn't find " & $strFolderName & " folder."
    Exit
EndIf
        

For $FolderItem In $oNetConnections.Items
    If StringLower($FolderItem.Name) = StringLower($oLanConnection) Then
        $oLanConnection = $FolderItem
        Exitloop
    EndIf
Next

If not IsObj($oLanConnection) Then
     MsgBox(0,"Error","Couldn't find " & $oLanConnection & " Item."
     Exit
EndIf

$oEnableVerb=""
$oDisableVerb=""

For $Verb In $oLanConnection.Verbs
 If $Verb.Name = $strEnableVerb Then
   $oEnableVerb = $Verb
 EndIf
 If $Verb.Name = $strDisableVerb Then
   $oDisableVerb = $Verb
 EndIf
Next

If $bEnable then
 If IsObj($oEnableVerb) Then $oEnableVerb.DoIt  ; Enable network card
Endif

If not $bEnable then
 If IsObj($oDisableVerb) Then $oDisableVerb.DoIt; Disable network card
EndIf

Sleep(1000)

Regards,

-Sven

Link to comment
Share on other sites

  • 2 weeks later...
Guest stryderusa

SvenP, Thanks for posting your code here. I have adapted it a bit to disable then re-enable the LAN connection.

The reason I needed this code was for use in a Network Quarantine system. When a client is allowed on to the regular network, they are switched from one VLAN to another and are then eligible to receive a new DHCP address.

The problem with this system is, the client must wait an indefinite period of time before the new DHCP address becomes available (based on the DHCP lease expiration). Even thought it is set to a minimum to expedite the switchover, the time is still not precise and the client may easily grow frustrated.

A simple IPCONFIG /RELEASE & /RENEW does not work for this because there is an issue with the system that requires the NIC to actually shut down and restart to ensure the switchover to the new VLAN. Unfortunately, many on-board NICs do not truly disconnect from a switch port via a Windows restart. This script comes in handy because it allows the client to instantly obtain a new DHCP address because WinXP will automatically renew

The problem I am having now is that clients may have different names for their LAN connection, rather than just the simple "Local Area Connection". In addition, multiple NICs add to the confusion by creating other LAN connections.

Is there a way to parse the active connection and determine the name on-the-fly, or would it be possible to adapt the script to search for the "Local Area Connection" object based on a wildcard name, such as "Local*" or just simply shutdown ALL LAN connections then restart them?

Thanks for any help or suggestions you can provide.

Robert

Link to comment
Share on other sites

  • 2 years later...

Here is a script-only solution to disable/enable a network adapter.

It requires AutoIt3 Beta version 3.1.1.40 or higher.

Hi Sven,

I'm sorry to bother you...but the code you supplied does not work on Windows Server 2008 or Windows Vista. Would it be an impossible effort for you to try to get it running on those OSses? I would very appreciate your help in getting this to play...

Best Regards,

Chris

Link to comment
Share on other sites

  • 4 weeks later...

I have made some ameliorations, but still not tested on 2003 Server and Vista.

;===================================================================================================

=================
; Name            : _NicControl
; Description      : Activate or deactivate a network controler (NIC)
; Syntax            : _NicControl($oLanConnection, [$bEnable, , [$bFullName]])
; Parameter(s)    : $oLanConnection - The name of the Lan connection.
;                     If equal to 'All' then change state of all NIC.
;                    $bEnable       - The state of the NIC (Default 1 for activate).
;                    $bFullName - Look for string $oLanConnection in NIC name (Default 1 for full name).
; Requirement(s)    : Win32_NT OS
; Return value(s)   : On Success - Return 1
;                    On Failure - Return 0 and @Extented is set.
;                     @Extented = 1 : Not Win32_NT OS
;                     @Extented = 2 : Network folder not find.
;                     @Extented = 3 : Lan connection not find.
; Author            : Tlem <tlem at tuxolem dot net>
;                     http://www.autoitscript.fr/forum/viewtopic.php?f=21&t=1092
; Note(s)          : Original author : SvenP
;                     http://www.autoitscript.com/forum/index.php?showtopic=12645&view=findpost&p=87000
; To Do             : Get errors for changing state of multiple NIC (Perhaps with array).
; ====================================================================================================

===============
Func _NicControl($oLanConnection, $bEnable = 1, $bFullName = 1)

; Langage selection.
    Select
    ; English (United States)
        Case StringInStr("0409,0809,0c09,1009,1409,1809,1c09,2009,2409,2809,2c09,3009,3409", @OSLang)
            $strEnableVerb = "En&able"
            $strDisableVerb = "Disa&ble"
            If @OSVersion = "WIN_2000" Then
                $strFolderName = "Network and Dial-up Connections"; Windows 2000
            Else
                $strFolderName = "Network Connections"; Windows XP
            EndIf

        ; Français (France)
        Case StringInStr("040c,080c,0c0c,100c,140c,180c", @OSLang)
            $strEnableVerb = "&Activer"
            $strDisableVerb = "&Désactiver"
            If @OSVersion = "WIN_2000" Then
                $strFolderName = "Connexions réseau et accès à distance"; Windows 2000
            Else
                $strFolderName = "Connexions réseau"; Windows XP
            EndIf

        ; Add here the correct Verbs for your Operating System Language

    EndSelect

; Control OS type.
    If @OSTYPE <> "WIN32_NT" Then
        SetError(1, 1)
        Return 0
    EndIf

; Virtual folder containing icons for the Control Panel applications. (value = 3)
    Const $ssfCONTROLS = 3

    $ShellApp = ObjCreate("Shell.Application")
    $oControlPanel = $ShellApp.Namespace($ssfCONTROLS)


; Find 'Network connections' control panel item
    $oNetConnections = ""
    For $FolderItem In $oControlPanel.Items
        If $FolderItem.Name = $strFolderName Then
            $oNetConnections = $FolderItem.GetFolder
            ExitLoop
        EndIf
    Next


; If no 'Network connections' folder then return error.
    If Not IsObj($oNetConnections) Then
        SetError(1, 2)
        Return 0
    EndIf

; Change all NIC state (Do not return errors).
    If StringLower($oLanConnection) = "all" Then
        For $FolderItem In $oNetConnections.Items
            _NicControl($FolderItem.name, $bEnable)
        Next
        Return 1
    EndIf


; Change NIC state by partial name (Do not return errors).
    If $bFullName = 0 Then
        For $FolderItem In $oNetConnections.Items
            If StringInStr(StringLower($FolderItem.name), StringLower($oLanConnection)) Then
                _NicControl($FolderItem, $bEnable)
            EndIf
        Next
        Return 1
    EndIf

; Find the collection of the network connection name.
    For $FolderItem In $oNetConnections.Items
        If StringLower($FolderItem.Name) = StringLower($oLanConnection) Then
            $oLanConnection = $FolderItem
            ExitLoop
        EndIf
    Next

; If no network connection name then return error.
    If Not IsObj($oLanConnection) Then
        SetError(1, 3)
        Return 0
    EndIf

    $oEnableVerb  = ""
    $oDisableVerb = ""

; Find the state of the network connection.
    For $Verb In $oLanConnection.Verbs
        If $Verb.Name = $strEnableVerb Then
            $oEnableVerb = $Verb
        EndIf
        If $Verb.Name = $strDisableVerb Then
            $oDisableVerb = $Verb
        EndIf
    Next
    
; Enable NIC
    If $bEnable Then
        If IsObj($oEnableVerb) Then $oEnableVerb.DoIt
    EndIf

; Disable NIC
    If Not $bEnable Then
        If IsObj($oDisableVerb) Then $oDisableVerb.DoIt
    EndIf

    $begin = TimerInit()
    While 1
        $dif = Int(TimerDiff($begin) / 1000)
        If $dif > 10 Then ExitLoop
    ; Control the state of the NIC to exit before the end of waiting time.
        If $bEnable = 1 And _GetNicState($oLanConnection, $strEnableVerb, $strDisableVerb) = 1 Then ExitLoop
        If $bEnable = 0 And _GetNicState($oLanConnection, $strEnableVerb, $strDisableVerb) = 0 Then ExitLoop
        Sleep(100)
    WEnd

; Set the return value of the function.
    $Res = _GetNicState($oLanConnection, $strEnableVerb, $strDisableVerb)
    If $bEnable = 1 And $Res = 0 Then
        Return 0
    ElseIf $bEnable = 0 And $Res = 1 Then
        Return 0
    Else
        Return 1
    EndIf
EndFunc  ;==>_NicControl

; Function that give the state of the lan connection (1 = Active  0 = Not Active).
Func _GetNicState($oLanConnection, $strEnableVerb, $strDisableVerb)
    For $Verb In $oLanConnection.Verbs
        If $Verb.Name = $strEnableVerb Then
            Return 0
        Else
            Return 1
        EndIf
    Next
EndFunc  ;==>_GetNicState

Best Regards.Thierry

Link to comment
Share on other sites

  • 1 month later...

Hi Tlem

I have sent you an email, and then decided to sign up here as well.

Although I am highly technical, it seems in this area I have no idea :(

I am using Windows Xp, and just trying to get a standalone exe to disable/re-enable my network

Originally I have tried the first script (above) using AutoIT

Which gave the error :

Aut2Exe (File "C:\Test.au3"):

StringInStr("0409,0809,0c09,1009,1409,1809,1c09,2009,2409,2809,2c09,3009,3409

Error:Unterminated string.

So then I tried the one above

No errors on build muttley

But nothing happened, my network does not disable :P

So then I went here

And downloaded NicControl.au3

No errors on build :)

But nothing happened, my network does not disable :dance: again!

I'm sue I'm missing the basics here, because others have replied with thank-yous.

Could you please help for me to compile this into a standalone exe?

Thank-you

Link to comment
Share on other sites

Hello.

I have reply to your email, but I add a comment here (for other people muttley )

On the new script NicControl.au3 (only Win_32 compatible) don't forget to add the instruction that you want to made on the top of the script (between line 26 and line 37) if you want a standalone exe or include NicControl.au3 to an other script and call function _NICContol('all', 0, 0) to deactivate all NIC card for example.

Edited by Tlem

Best Regards.Thierry

Link to comment
Share on other sites

Thank-you Tlem

Yes received your support about capital "_NICContol("All", 0)"

I was using NicControl <- which didn't work (I'm definately a newbie at all this)

Anyway it works muttley As standalone. Thank-you

But once the script has finished, I get this error:

----------------------------------------------------------------

AutoIT Error

Line -1:

Error:Variable used without being declared

----------------------------------------------------------------

I suspect that the line I have added "_NICContol("All", 0)" required more things

Anyway, I will check here and at my email.

Note: I'm quite sure that it is some basic understanding of AutoIT, so I have Googled for an answer, but due to sooo many different scripts out there, I am unsure what to do to get rid of the error.

If you, or anyone who may be able to tell me

Could reply with what to do, I would be most gratefull for your time

Thank-you for getting it to work.

Kim

Link to comment
Share on other sites

Hi Tlem,

Sorry for the double post

I have tried both "working" scripts (received from you)

And both do the exact same issue

Once the script finishes they popup with the error

So therefore the error must be to do with my AutoIT (even though latest version)

I will fully uninstall AutoIT, and check again

Otherwise any more feedback welcomed

Thanks again

Kim

Link to comment
Share on other sites

Humm !!!

I think it's probably due to possibility to run in 2 languages (probably a mistake in the code) !

I will take a look this night.

I have tested on French Windows and not US Windows, so try to run it in .au3 mode (not compiled) and tell me where is the problem.

Edit : I try different think, but don't find the mistake.

All is running on my PC. I will try to find an US Windows for testing in virtual machine.

Edited by Tlem

Best Regards.Thierry

Link to comment
Share on other sites

That's realy strange. It's meen that on your system $Verb.Name isn't En&able or Disa&ble !!!

It's a strange case !

I'll take a look on an US windows to see if I have the same problem.

For the moment, replace this :

; Find the state of the network connection.
    For $Verb In $oLanConnection.Verbs
        If $Verb.Name = $strEnableVerb Then
            $oEnableVerb = $Verb
            $State = 0
        EndIf
        If $Verb.Name = $strDisableVerb Then
            $oDisableVerb = $Verb
            $State = 1
        EndIf
    Next

By this :

; Find the state of the network connection.
    For $Verb In $oLanConnection.Verbs
        If $Verb.Name = $strEnableVerb Then
            $oEnableVerb = $Verb
            $State = 0
        Else
;If $Verb.Name = $strDisableVerb Then
            $oDisableVerb = $Verb
            $State = 1
        EndIf
    Next

It's not the final solution, I must do a little debug of code on US OS, but like this your compiled exe don't return error. muttley

Edited by Tlem

Best Regards.Thierry

Link to comment
Share on other sites

Start Network fully works (ie I'm putting in the _NICContol("All") )

Stop Network does not work (ie I'm putting in the _NICContol("All", 0) )

it also does some strange things:

It creates a "New Connection Wizard" shortcut on the Desktop

It creates a "Network Setup Wizard" shortcut on the Desktop

That's funny

Anyway, I'll keep checking the forum here, thanks for the input so far

As you can see, I'm pretty quick at the testing, from your post.

Kim

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