从CMD切换启用/禁用以太网适配器 [英] Toggle enable/disable Ethernet adapter from CMD

查看:867
本文介绍了从CMD切换启用/禁用以太网适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 .bat 文件,该文件可以启用/禁用我的以太网适配器,但是我对编码或cmd语法了解不多.我正在考虑在以下方式中使用 netsh 命令:

I'm trying to create a .bat file which can enable/disable my ethernet adapter, but I don't have much knowledge about coding or the cmd syntax. I was thinking about using the netsh command in something like:

IF " ~Ethernet adapter is enabled~ " GOTO :disable ELSE GOTO :enable

:disable
    netsh interface set interface "Ethernet" disabled
        
:enable
    netsh interface set interface "Ethernet" enabled
    

我该怎么办?


编辑

我已经使用了很长一段时间的 Stephan的解决方案. 如今,我将下面的代码与 BAT转换为EXE转换器一起使用,以更轻松,更安静地运行它:

How can I do it right?


EDIT

I've used Stephan's solution as it was for a long time.
Nowadays, I use the code below alongside a BAT to EXE converter to run it easier and silently:

REM @echo off

REM :: BatchGotAdmin
REM :-------------------------------------
REM REM  --> Check for permissions
REM     IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
REM >nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
REM ) ELSE (
REM >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM )

REM REM --> If error flag set, we do not have admin.
REM if '%errorlevel%' NEQ '0' (
REM     echo Requesting administrative privileges...
REM     goto UACPrompt
REM ) else ( goto gotAdmin )

REM :UACPrompt
REM     echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
REM     set params= %*
REM     echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"

REM     "%temp%\getadmin.vbs"
REM     del "%temp%\getadmin.vbs"
REM     exit /B

REM :gotAdmin
REM     pushd "%CD%"
REM     CD /D "%~dp0"
REM :--------------------------------------    
REM @echo on

netsh interface show interface "Ethernet" |find "Connected" >nul && (
  netsh interface set interface "Ethernet" disabled
) || (
  netsh interface set interface "Ethernet" enabled
)

推荐答案

如果您已经熟悉netsh interface命令,为什么不使用它呢?

If you are already familiar with the netsh interface command, why don't you use it?

netsh interface show interface "Ethernet" |find "Connected" >nul && (
  echo connected - disconnecting...
  netsh interface set interface "Ethernet" disabled
) || (
  echo disconnected - connecting
  netsh interface set interface "Ethernet" enabled
)

这篇关于从CMD切换启用/禁用以太网适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆