Jump to content











Photo
- - - - -

How can I (easily) connect/disconnect a LAN/Ethernet connection?


  • Please log in to reply
26 replies to this topic

#1 gbrao

gbrao

    Frequent Member

  • Advanced user
  • 354 posts
  •  
    India

Posted 13 May 2017 - 08:56 AM

I have two computers - one is connected to the net using a WiFi USB adapter and the other uses a (wired) LAN/Ethernet connection.

 

With the computer with WiFi I can easliy connect/disconnect the internet with a single click (from the system tray icon -> Connect/Disconnect). I also uncheck "connect automatically when ...". So I have full control over when the computer can access the net. (I like to have the internet disconnected when booting windows btw).

 

Is there a easy one-click way to do the same with a (wired) LAN/Ethernet connection?



#2 nguyentu

nguyentu

    Member

  • Members
  • 75 posts
  •  
    Vietnam

Posted 13 May 2017 - 09:08 AM

It is possible to Disable LAN/Ethernet connection in two clicks.
 
Go to Network Connections and right-click on an adapter to create a shortcut, then right-click on the shortcut to turn on/off faster.
Control Panel\Network and Internet\Network Connections

  • gbrao likes this

#3 gbrao

gbrao

    Frequent Member

  • Advanced user
  • 354 posts
  •  
    India

Posted 13 May 2017 - 09:46 AM

 

It is possible to Disable LAN/Ethernet connection in two clicks.
 
Go to Network Connections and right-click on an adapter to create a shortcut, then right-click on the shortcut to turn on/off faster.
Control Panel\Network and Internet\Network Connections

 

 

For the wired (LAN) connection there's no connect/disconnect option. Only disable. If I don't get a better solution, will use this. Thanks.



#4 DarknessAngel

DarknessAngel

    Newbie

  • Members
  • 21 posts
  •  
    South Korea

Posted 13 May 2017 - 10:54 AM

You can use 'devcon' for en/disable device (just run script)



#5 gbrao

gbrao

    Frequent Member

  • Advanced user
  • 354 posts
  •  
    India

Posted 13 May 2017 - 11:12 AM

I found this : https://msdn.microso...y/ms811721.aspx

 

But it does not show up on my Win7 64bit PC.

 

There's only enable/disable LAN.



#6 erwan.l

erwan.l

    Gold Member

  • Developer
  • 1920 posts
  • Location:Nantes - France
  •  
    France

Posted 13 May 2017 - 11:59 AM

a batch may be?

 

Get NIC list and index number:
wmic nic get name, index

Enable NIC with index number: (eg: 0)
wmic path win32_networkadapter where index=0 call enable

Disable NIC with index number: (eg: 7)
wmic path win32_networkadapter where index=0 call disable



#7 erwan.l

erwan.l

    Gold Member

  • Developer
  • 1920 posts
  • Location:Nantes - France
  •  
    France

Posted 13 May 2017 - 12:01 PM

or using netsh command line :

 

netsh interface set interface "LAN01" DISABLED

netsh interface set interface "LAN01" ENABLED



#8 Zoso_The_Internet_Tard

Zoso_The_Internet_Tard

    Silver Member

  • Advanced user
  • 545 posts
  • Interests:An investigation is underway to determine whether Trump has any ties to America.

Posted 13 May 2017 - 01:52 PM

Umm, just pull the cable, if you want to make sure it's really DCed. Side benefit: Your data cant be monitored by the NSA.

#9 erwan.l

erwan.l

    Gold Member

  • Developer
  • 1920 posts
  • Location:Nantes - France
  •  
    France

Posted 13 May 2017 - 01:53 PM

Umm, just pull the cable, if you want to make sure it's really DCed. Side benefit: Your data cant be monitored by the NSA.

 

Now that you are finger pointing the NSA, they might just pull out your cable :)



#10 gbrao

gbrao

    Frequent Member

  • Advanced user
  • 354 posts
  •  
    India

Posted 13 May 2017 - 02:43 PM

This post #2 : http://reboot.pro/to...on/#entry203513

seems to be the easiest way to disable/enable.

 

What I really wanted was a way to connect/disconnect the wired connection. If it's not possible to connect/disconnect I'll use the method in #2.



#11 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13649 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 May 2017 - 02:48 PM

As a matter of fact normally there is an option to show the wired connection in the system tray, and it is two clicks, one right, one left on disable.

 

:duff:

Wonko



#12 Zharif

Zharif

    Frequent Member

  • .script developer
  • 149 posts
  • Location:Germany
  •  
    Germany

Posted 13 May 2017 - 04:37 PM

Here a batch that might be of help.

It uses the netsh approach. Works for Win7 and Win10.

 

-First detecting the correct interface name for the LAN-Adapter.

 For me, in Win7 it is "LAN-Connection". In Win10 it is "ETHERNET".

 As seen and unfortunately, the strings seem to differ and may be localised.

 

-then trying to disable/enable the appropriate network adapter.

 Without passing the commandline parameter "-e"  (=enable) or by double-clicking the batch

 the network adapter will be disabled by default. Passing "-e" the network adapter will be re-enabled.

 

ConnLAN.cmd

@ECHO OFF
:: if first commandline parameter is '-e' then enable adapter. Otherwise disable adapter.
IF /I "%~1"=="-e" (SET _Action=ENABLED& SET _MsgStr=enable) ELSE (SET _Action=DISABLED& SET _MsgStr=disable)

:: Win7:  interface name is LAN-Connection.
:: Win10: interface name is ETHERNET.
:: Include both in searchmask for Findstr
SET "_SearchMask=ETHERNET LAN"

:: Just to display all available network adapters - may be removed
netsh interface show interface
ECHO.

FOR /F "tokens=3* delims= " %%a  IN ('netsh interface show interface') DO (
  ECHO %%b|FINDSTR /I "%_SEARCHMASK%" >NUL 2>&1 && SET "_CONN=%%b"
)
ECHO Interface Name found: "%_CONN%"
ECHO.

SET /P "=Trying to %_MsgStr% '%_CONN%' ..." <NUL
netsh interface set interface "%_CONN%" "%_ACTION%" >NUL 2>&1
IF ERRORLEVEL 1 (ECHO failed) ELSE (ECHO success)
ECHO.&ECHO.
PAUSE&EXIT


#13 Alexander Ceed

Alexander Ceed

    Frequent Member

  • Advanced user
  • 205 posts

Posted 14 May 2017 - 12:24 AM

From Zharif's script i made another script that automatically enables the adapter if is disabled, and disables if is enabled.

 

 

ethernet_enable_disable.cmd

set "SearchMask=ethernet lan"

for /f "tokens=1* delims= " %%a in ('netsh interface show interface') do (
  @for /f "tokens=2* delims= " %%c in ("%%b") do (
  echo %%d|findstr /i "%SearchMask%" >nul 2>&1 && set "interface=%%d" && set state=%%a
  )
)


if /i %state%==Enabled (
  netsh interface set interface %interface% disabled
) else (
  netsh interface set interface %interface% enabled
)

  • gbrao likes this

#14 Zharif

Zharif

    Frequent Member

  • .script developer
  • 149 posts
  • Location:Germany
  •  
    Germany

Posted 14 May 2017 - 07:36 AM

@Alexander Creed,

 

there's no need for a nested loop.

Just use this one for example:

FOR /F "tokens=1-3* delims= " %%a  IN ('netsh interface show interface') DO (
ECHO %%d|FINDSTR /I "%_SEARCHMASK%" >NUL 2>&1 && SET "_CONN=%%d" && SET "_STATE=%%a"
)

BTW, your batch examples' functionality suffers a little bit more from localised strings (mentioned in my previous post).

On german Win7 or 10 it is NOT "activated or deactivated". It is "deaktiviert or aktiviert".

Do you know the expression for e.g. Greek?

That's the reason why my batch excludes the adapter status.

Furthermore, it also seems to be unclear if LAN, ETHERNET is a sufficient searchmask.

Google searches suggest, that there're more expressions around to consider (e.g. local network area).

 

Edit:

I think there's no general solution using the netsh utility without writing the current adapter status to a file.

Independently from the language locale, you're able then to make a simple string comparison.

But writing to HD should be the last option available I think.



#15 gbrao

gbrao

    Frequent Member

  • Advanced user
  • 354 posts
  •  
    India

Posted 14 May 2017 - 07:43 AM

%interface% in the netsh commands need to be quoted I think.



#16 Zharif

Zharif

    Frequent Member

  • .script developer
  • 149 posts
  • Location:Germany
  •  
    Germany

Posted 14 May 2017 - 07:57 AM

Yes, of cause gbrao,

corrected in my script - thanks.



#17 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13649 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 14 May 2017 - 01:00 PM

The WMIC approach is definitely more "accurate" and "language neutral". 

The issue (here like there) comes from the need to select the "proper" interface manually (as choosing it automatically may be prone to errors). 

The IpAddress (that would be the most natural thing to check for) is a stupid string array data type and WMIC (actually WQL) won't work with this data type, making the "where" query impossible, and additionally if the interface is disabled the IP address will be nowhere to be seen.

 

Something *like* this (only an example/base) might do, at least it finds ONLY physical devices (typically with device ID starting with PCI, excluding all the "virtual" ones that typically start with ROOT):



@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET Status0=Disabled
SET Status2=Enabled
SET Action0=enable
SET Action2=disable
FOR /F "tokens=2,3,4 delims=," %%A IN ('WMIC path win32_NetworkAdapter Get Index^, Name^,NetConnectionStatus ^,PNPDeviceID /format:csv ^| FIND "PCI\VEN"') DO CALL :PhysNIC %%A "%%B" %%C
PAUSE
GOTO :EOF

:PhysNIC
SET Index=%1
ECHO Physical Nic found:
ECHO Index=%1 : %~2
SET Status=!Status%3!&SET Action=!Action%3!
CALL :%Status%
GOTO :EOF


:Enabled
WMIC path win32_NetworkAdapterConfiguration where "Index = %Index%" Get IPAddress /format:list

:Disabled
ECHO Device is currently %Status%
ECHO Do you want to %Action% it?
ECHO Imagine here a prompt for confirmation or skip ...
GOTO :EOF

:duff:

Wonko


  • ReTokener likes this

#18 Alexander Ceed

Alexander Ceed

    Frequent Member

  • Advanced user
  • 205 posts

Posted 14 May 2017 - 01:06 PM

Furthermore, it also seems to be unclear if LAN, ETHERNET is a sufficient searchmask.

 

It can be one of the following: lan, ethernet, ethernet0, ethernet1, ethernet 1, local area network, local area network 1.

If you can adapt your script to include these adapter names, it might work on a lot more machines.



#19 erwan.l

erwan.l

    Gold Member

  • Developer
  • 1920 posts
  • Location:Nantes - France
  •  
    France

Posted 14 May 2017 - 02:10 PM

there should also be a way, thru script, to detect the active/default interface (i.e the one routing to internet) and disable it.



#20 Alexander Ceed

Alexander Ceed

    Frequent Member

  • Advanced user
  • 205 posts

Posted 14 May 2017 - 02:39 PM

there should also be a way, thru script, to detect the active/default interface (i.e the one routing to internet) and disable it.

What if you have multiple active adapters? For instance, one ethernet, two vmware and one cisco anyconnect? Not to mention wireless adapters.



#21 erwan.l

erwan.l

    Gold Member

  • Developer
  • 1920 posts
  • Location:Nantes - France
  •  
    France

Posted 14 May 2017 - 03:55 PM

What if you have multiple active adapters? For instance, one ethernet, two vmware and one cisco anyconnect? Not to mention wireless adapters.


There can be only one adapter being the default route but may be we are deviating from the main topic.

#22 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13649 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 14 May 2017 - 05:23 PM

there should also be a way, thru script, to detect the active/default interface (i.e the one routing to internet) and disable it.

Sure, BUT what if the active/default interface is currently disabled?

 

IF it is enabled, that would be:

WMIC path win32_NetworkAdapter Where NetconnectionStatus=2 Get Index, Name

and/or:

WMIC path win32_NetworkAdapterConfiguration Where "IPEnabled = True" Get Index, Description /format:list

 

And still we haven't solved the "multiple instance" possibility, I did have a machine that I used as "router" between two subnets (with two Nic's) but I switched to a Linux, so I cannot make a test there.

 

:duff:

Wonko



#23 Zharif

Zharif

    Frequent Member

  • .script developer
  • 149 posts
  • Location:Germany
  •  
    Germany

Posted 14 May 2017 - 07:18 PM

Wonko,

I was really longing for your code contribution here.

As always, a "nice to have" one.

For me your code only works after adding the open and closed bracket after "DO".

I also felt free to add some line spacing and the missing action.

 

As for Wonko - just a working example batch (ALL credits to Wonko):

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET Status0=Disabled
SET Status2=Enabled
SET Action0=enable
SET Action2=disable

FOR /F "tokens=2,3,4 delims=," %%A IN ('WMIC path win32_NetworkAdapter Get Index^, Name^, NetConnectionStatus^, PNPDeviceID /format:csv ^| FIND "PCI\VEN"') DO CALL :PhysNIC %%A "%%B" %%C
GOTO :EOF

:PhysNIC
    SET Index=%1
    ECHO Physical Nic found:
    ECHO Index=%1 : %~2
    SET Status=!Status%3!&SET Action=!Action%3!
    CALL :%Status% %%A
GOTO :EOF

:Enabled
    WMIC path win32_NetworkAdapterConfiguration where "Index = %Index%" Get IPAddress /format

:Disabled
    IF %Status%==%Status0% ECHO.
    ECHO Device is currently %Status%
    SET /P "Choice= Press enter to %Action% it."
    IF "%Choice%"=="" wmic path win32_networkadapter where index=%Index% call %Action%
GOTO :EOF

In the meanwhile I tried to rewrite the netsh approach.

Through the localisation issues I decided to display all adapters with a menu numbering.

In this way the user is able to choose the adapter of his choice - a more accurate decision is possible then.

 

Here's the example code (works similar to the first one I posted):

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS

:: if first commandline parameter is '-e' then enable adapter. Otherwise disable adapter.
IF /I "%~1"=="-e" (SET _Action=ENABLED& SET "_MsgStr=enable"
) ELSE (SET _Action=DISABLED& SET _MsgStr=disable)

:: check for netsh.exe
DIR /B "%SYSTEMROOT%\System32\netsh.exe" >NUL 2>&1||ECHO.&& ECHO netsh.exe not found ...exit script&& GOTO:_END

:: get, count-up and store all network adapters by Name [_NICn] - tested in XP, 7 and 10
:: this loop also works for vmware virtual machines [tests in XP and Seven] were column 'Status' may be empty
FOR /F  "skip=3 tokens=3* delims= " %%a  IN ('netsh interface show interface') DO (
SET /A "_CountA+=1" & IF "%%b"=="" (CALL SET _NIC%%_COUNTA%%=%%a) ELSE (CALL SET _NIC%%_COUNTA%%=%%b))
SET _NIC >NUL 2>&1||ECHO  No network adapters found ...exit script&& GOTO:_End

:_Menu
:: display netsh interface screen with menu numbers and get max menu count
CLS
SET "_CountB=& SET _CountMAX=& SET _Input=& SET _CHOICE="
ECHO.& FOR /F  "skip=3 tokens=* delims= " %%a  IN ('netsh interface show interface') DO (
SET /A _CountB+=1& CALL ECHO %%_COUNTB%%:    %%a & CALL SET "_CountMax=%%_COUNTB%%")

:: Choice dialog
ECHO.& SET /P "_INPUT=Please select a network adapter by menu number."

:: validate user input [_CHOICE variable should be defined after validation]
CALL :_Validate _INPUT _CountMAX _CHOICE

:: re-display menu if _Choice variable is not defined
IF NOT DEFINED _CHOICE PAUSE>NUL&GOTO:_Menu

:: if _Choice matches stored _NIC name enable/disable adapter
FOR /F "tokens=1* delims==" %%a IN ('SET _NIC') DO (
  IF "_NIC%_CHOICE%" EQU "%%a" (
    SET /P "=Trying to %_MsgStr% '%%b' ..." <NUL
    netsh interface set interface "%%b" "%_ACTION%" >NUL 2>&1 && ECHO success||ECHO failed
  )
)

:_End
PAUSE&GOTO:EOF

:_Validate
:: robust against poisoned user input
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
IF NOT DEFINED %1 goto:_ExValidate
FOR /L %%a IN (1,1,!%2!) DO IF "!%1!" EQU "%%a" SET "_Var=%%a"

:_ExValidate
IF NOT DEFINED _Var ECHO Invalid input. Digits only from 1 to !%2! are allowed.
ENDLOCAL & SET %3=%_VAR%
GOTO:EOF

...suggestions are welcome.



#24 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13649 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 14 May 2017 - 07:52 PM

@Zharif

Actually my bad :w00t:, somehow there was CR/LF in the pasted code, the CALL is on same line as DO

 

 BUT you should not put the GOTO :EOF inside the brackets ... :whistling:

 

:duff:

Wonko



#25 Zharif

Zharif

    Frequent Member

  • .script developer
  • 149 posts
  • Location:Germany
  •  
    Germany

Posted 14 May 2017 - 07:59 PM

Yes, corrected.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users