初次接触到这个问题的朋友可能感觉有些奇怪,您要用网络,却又要停用网络连接。有过管理经验的朋友们就不会感觉奇怪,因为在某些特殊的情况下确实有这种奇怪的需求,例如进行一些服务的切换时,可能需要先停用当前网络连接,然后调整网络设置,然后再启用它。这个动作如果需要定时或者定条件的触发,而执行可能在非工作时间发生,那么管理员就一定要想办法用脚本执行了。
很多朋友们可能马上会想起用netsh 或者用vbs脚本来做,但两个东西用起来都比较麻烦,而且最让人头疼的就是网络连接 或者说 network connection interface名称,在不同的操作系统上也不同,简体中文叫本地网络连接,繁体叫当前网络连线,英文叫Local Area Connection ,如果这个名字被改掉了呢?管理员要想写一个通用脚本,就要考虑到所有可能的情况,结果这个脚本就搞的异常复杂。(在本文的后面,我会给出netsh 和vbs的脚本)
今天我要说到一个很好的工具 devcon,您可以在微软下载站点下载,下载地址在
http://download.microsoft.com/download/1/1/f/11f7dd10-272d-4cd2-896f-9ce67f3e0240/devcon.exe它的命令行格式为:
devcon [-r] [-m:\\] [...]
-r if specified will reboot machine after command is complete, if needed.
is name of target machine.
is command to perform (see below).
... is one or more arguments if required by command.
For help on a specific command, type: devcon help
classfilter Allows modification of class filters.
classes List all device setup classes.
disable Disable devices that match the specific hardware or instance ID.
driverfiles List driver files installed for devices.
drivernodes Lists all the driver nodes of devices.
enable Enable devices that match the specific hardware or instance ID.
使用它来禁用网卡,当然也就停用了网络连接。如果要考虑通用性的问题,那么这么写就好了:
devcon disable =net
启用网络,这么写就好了:
devcon enable =net
这里的 net,其实是 设备类,这个信息在哪里查询呢?Winkey(windows徽标键)+R,打开运行对话框,输入devmgmt.msc,回车,打开设备管理器,找到您需要禁用的网卡,查看它的属性,在属性页上选择详细信息,在属性下拉框中,选择设备类。
不过照我这么写呢,还会停用一些其它相关的设备,例如 ROOT\*ISATAP\0000 ,例如 ROOT\VMWARE\0000 。如果您需要精确的禁用某一个网络设备,那么就要使用硬件ID,例如 PCI\VEN_14E4&DEV_1600&SUBSYS_01C21028&REV_02
不过硬件ID在不同的计算机上,由于硬件的不同就可能会不同,这样就会让脚本又失去了通用性。
大家会注意到devcon还有很多其它的功能,而且这些参数都支持vista,很不错。
find Find devices that match the specific hardware or instance ID.
findall Find devices including those that are not present.
help Display this information.
hwids Lists hardware ID's of devices.
install Manually install a device.
listclass List all devices for a setup class.
reboot Reboot local machine.
remove Remove devices that match the specific hardware or instance ID.
rescan Scan for new hardware.
resources Lists hardware resources of devices.
restart Restart devices that match the specific hardware or instance ID.
sethwid Modify Hardware ID's of listed root-enumerated devices.
stack Lists expected driver stack of devices.
status List running status of devices.
update Manually update a device.
updateni Manually update a device (non interactive).
附录:用netsh 和vbs脚本停止网络连接的方法
netsh:
netsh interface set interface name="Local Area Connection" admin=“disabled”
netsh interface set interface "本地连接" "disabled"
netsh interface set interface "本地连接" "enabled"
vbs:
Const ssfCONTROLS = 3
sConnectionName = "本地连接"
sEnableVerb = "启用(&A)"
sDisableVerb = "禁用(&B)"
set shellApp = createobject("shell.application")
set oControlPanel = shellApp.Namespace(ssfCONTROLS)
set oNetConnections = nothing
for each folderitem in oControlPanel.items
if folderitem.name = "网络和拨号连接" then
set oNetConnections = folderitem.getfolder: exit for
end if
next
if oNetConnections is nothing then
msgbox "未找到网络和拨号连接文件夹"
wscript.quit
end if
set oLanConnection = nothing
for each folderitem in oNetConnections.items
if lcase(folderitem.name) = lcase(sConnectionName) then
set oLanConnection = folderitem: exit for
end if
next
if oLanConnection is nothing then
msgbox "未找到 '" & sConnectionName & "' item"
wscript.quit
end if
bEnabled = true
set oEnableVerb = nothing
set oDisableVerb = nothing
s = "Verbs: " & vbcrlf
for each verb in oLanConnection.verbs
s = s & vbcrlf & verb.name
if verb.name = sEnableVerb then
set oEnableVerb = verb
bEnabled = false
end if
if verb.name = sDisableVerb then
set oDisableVerb = verb
end if
next
'debugging displays left just in case...
'
'msgbox s ': wscript.quit
'msgbox "Enabled: " & bEnabled ': wscript.quit
'not sure why, but invokeverb always seemed to work
'for enable but not disable.
'
'saving a reference to the appropriate verb object
'and calling the DoIt method always seems to work.
'
if bEnabled then
' oLanConnection.invokeverb sDisableVerb
oDisableVerb.DoIt
else
' oLanConnection.invokeverb sEnableVerb
oEnableVerb.DoIt
end if
'adjust the sleep duration below as needed...
'
'if you let the oLanConnection go out of scope
'and be destroyed too soon, the action of the verb
'may not take...
'
wscript.sleep 400