Toggle enable/disable Ethernet adapter from CMD

佐手、 提交于 2019-12-14 03:07:43

问题


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

How can I do it right?


回答1:


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
)



回答2:


In above solution it disconnects and connects the internet connection so I improvised like this to Toggle enable and disable Ethernet adapter and this is working perfectly for me. This code disables adapter if it is enabled and enables if it is disabled.

netsh interface show interface "Ethernet" |find "Disabled" >nul && (
  echo disabled - enabling...
  netsh interface set interface "Ethernet" enabled
) || (
  echo enabled - disabling
  netsh interface set interface "Ethernet" disabled
)


来源:https://stackoverflow.com/questions/47957701/toggle-enable-disable-ethernet-adapter-from-cmd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!