Sales & Support: 1.800.377.6329

Windows scripted control of Ethernet cards

Posted on March 31st, 2011 by DJ Roberts at Galil
Posted in Software |

It is sometimes desirable to be able to automatically control the Enabled/Disabled state of a PC’s Network Interface Cards (NIC). This can be useful during development to disable corporate LANs, wireless connections, or to remove connectivity of a development PC from a stand-alone motion control application. By using the “netsh” command line utility such a task is easily scriptable.

“Netsh is a command-line scripting utility that allows you to, either locally or remotely, display or modify the network configuration of a computer that is currently running.”

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx?mfr=true

To begin, open a console window and issue the command “ipconfig”

C:\Users\user>ipconfig

Windows IP Configuration

Ethernet adapter Local Area Connection 3:

Connection-specific DNS Suffix  . :
IPv4 Address. . . . . . . . . . . : 192.168.10.201
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix  . : galilmc.com
Link-local IPv6 Address . . . . . : fe80::8ce5:b3d3:e792:b644%11
IPv4 Address. . . . . . . . . . . : 10.0.1.190
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . : 10.0.62.2

C:\Users\user>

Make a note of the name of the network interface cards for which you’d like to disable. In this case, we choose “Local Area Connection” to disable the card connected to the corporate LAN.

To disable this adapter from the console, type:

C:\Users\user>netsh interface set interface "Local Area Connection" DISABLED

Reissuing ipconfig demonstrates that the interface is no longer listed. To reenable the interface, use

C:\Users\DJ>netsh interface set interface "Local Area Connection" ENABLED

The following is a batch script which will disable, pause for a key stroke, and then enabled a user-defined set of network cards.

@echo off
cls
REM Change the network names below to match
REM the network interfaces which should be
REM disabled. Find the names from ipconfig.
REM Add or remove more lines as needed
REM *************************************************************
netsh interface set interface "Local Area Connection" DISABLED
netsh interface set interface "Local Area Connection 3" DISABLED
netsh interface set interface "Local Area Connection 4" DISABLED
REM *************************************************************
echo Network Card(s) disabled.
Pause
REM *************************************************************
netsh interface set interface "Local Area Connection" ENABLED
netsh interface set interface "Local Area Connection 3" ENABLED
netsh interface set interface "Local Area Connection 4" ENABLED
REM *************************************************************
echo Network Card(s) enabled
Pause

For switching between Static and Dynamic (Thanks to Cory B)

@echo off
echo Choose:
echo [A] Set Static IP
echo [B] Set DHCP
echo.
:choice
SET /P C=[A,B]?
for %%? in (A) do if /I "%C%"=="%%?" goto A
for %%? in (B) do if /I "%C%"=="%%?" goto B
goto choice
:A
@echo off
netsh int ipv4 set address name="Private_NIC" static 192.168.0.1 255.255.0.0
goto end

:B
@echo off
netsh int ipv4 set address name = "Private_NIC" source = dhcp
goto end
:end

Sorry, comments for this entry are closed at this time.