Q. How can I enable or disable an interface (Local Area Connection) from the command-line?
Jerold Schulman
October 22, 2006
1 Min Read
I have scripted ConnInterface.bat to enable (connect) or disable (disconnect) an interface, like Local Area Connection, from the command-line.
ConnInterface.bat uses Cnic.exe which you must extracted from TechniquesForSimp2006_05.exe and stored in a folder that is in your path.
The syntax for using ConnInterface.bat is:
[call] ConnInterface Action InterfaceName Status
where:
Action is a C to connect, a D to disconnect, or and S to only return status.Interface is the name of the interface, like "Local Area Connection".Status is a call directed environment variable that will contain the interface status after the Action is performed, either C for connected, D for disconnected, or E for error.
NOTE: See tip 10620 How can I rename an interface (Local Area Connection) from the command-line?
NOTE: See tip 3378 How can I change the Local Area Connection name and/or the connection icon on the taskbar, using the registry?
ConnInterface.bat contains:
@echo offif {%3}=={} @echo Syntax: ConnInterface Action InterfaceName Status&goto :EOFsetlocalset Action=%1set IFN=%2set Action=%Action:"=%if /i "%action%" EQU "C" goto conif /i "%action%" EQU "D" goto disif /i "%action%" EQU "S" goto sta@echo Error: ConnInterface %1 %2 %3 - Action is must be C,D, or Sendlocalgoto :EOF:conset IC=cnic %IFN% -c -s -vgoto doit:disset IC=cnic %IFN% -d -s -vgoto doit:staset status=Eset wrk=Efor /f "Tokens=*" %%s in ('cnic %IFN% -s -v^|find "connected"') do ( set wrk=%%s)set wrk=%wrk:"=%if "%wrk%" EQU "E" goto finishset wrk=%wrk:*disconnected=D%if "%wrk%" EQU "D" set status=D&goto finishset wrk=%wrk:*connected=C%if "%wrk%" EQU "C" set status=C:finishendlocal&set %3=%status%goto :EOF:doitset status=Efor /f "Tokens=*" %%s in ('%IC%^|find "Operation succeded"') do ( set status=Y)if "%status%" EQU "E" goto finishgoto sta
About the Author
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.
You May Also Like