关于python:如何以编程方式启用/禁用网络接口? (Windows XP)

关于python:如何以编程方式启用/禁用网络接口? (Windows XP)

How to programmatically enable/disable network interfaces? (Windows XP)

我需要从Windows XP中的脚本启用/禁用完全网络接口。 我正在寻找python解决方案,但是欢迎任何常规方式(例如WMI,某些命令行ànetsh,某些Windows调用)并将进行调整。 谢谢。


使用netsh界面
用法集接口[name =] IfName
[[admin =]启用|禁用
[connect =] CONNECTED | DISCONNECTED
[newname =] NewName]

尝试将所有内容包括在外括号中:
netsh接口设置接口名称=" thename" admin = disabled connect = DISCONNECTED newname =" thename"

另请参见此MS KB页面:http://support.microsoft.com/kb/262265/
您可以遵循他们的任何建议。
要禁用适配器,您将需要确定引用硬件设备的方法。如果计算机上不存在多个具有相同名称的适配器,则可以取消该接口的描述(或PCI ID效果很好)。之后,使用devcon(禁用)。 Devcon是设备管理器的附加控制台界面。


到目前为止,我已经找到了以下Python解决方案:

1
2
3
4
>>> import wmi; c=wmi.WMI()
>>> o=c.query("select * from Win32_NetworkAdapter where NetConnectionID='wifi'")[0]
>>> o.EnableDevice(1)
(-2147217407,)

它将AFAIU转换为通用WMI错误0x80041001。可能是权限。


我在互联网上找到了这个.VBS脚本。它具有在无法使NETSH为此目的工作的计算机上实际工作的很酷的优势。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Const ssfCONTROLS = 3

sConnectionName ="Local Area Connection"

sEnableVerb ="En&able"
sDisableVerb ="Disa&ble"

set shellApp = createobject("shell.application")
set oControlPanel = shellApp.Namespace(ssfCONTROLS)

set oNetConnections = nothing
for each folderitem in oControlPanel.items
  if folderitem.name ="Network Connections" then
        set oNetConnections = folderitem.getfolder: exit for
end if
next

if oNetConnections is nothing then
msgbox"Couldn't find 'Network Connections' folder"
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"Couldn't find '" & 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 1000

除了RAS API之外,我似乎找不到用于控制MSDN上的接口的任何基本API,但我认为它们不适用于非拨号连接。正如您自己建议的那样,netsh可能是一个选择,据说它还具有编程接口:http://msdn.microsoft.com/zh-cn/library/ms708353(VS.85).aspx

如果要成为纯Python,则可以打开一组管道以与netsh进程进行通信。


您可能需要使用WMI。这可以作为一个很好的起点:
http://msdn.microsoft.com/en-us/library/aa394595.aspx


这是VB.Net

1
2
3
4
5
6
7
8
Dim objectQuery As New ObjectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId IS NOT NULL")
         Dim searcher As New ManagementObjectSearcher(scope, objectQuery)
         Dim os As ManagementObject
         Dim moColl As ManagementObjectCollection = searcher.Get()
         Dim _list As String =""
         For Each os In moColl
             Console.WriteLine(os("NetConnectionId"))
         Next os

那将获得您计算机上的所有接口。然后,您可以执行netsh将其禁用。

netsh interface set interface
DISABLED


devcon工具可以控制NIC,但不能直接控制接口。它是设备管理器小程序的命令行版本。

1
2
devcon disable (id or portion of name)
devcon enable (id or portion of name)

推荐阅读

    linux运行脚本命令?

    linux运行脚本命令?,系统,代码,服务,文件,工具,平台,网站,脚本,命令,方法,Lin

    linux控制台编程命令?

    linux控制台编程命令?,系统,工具,环境,命令,名称,标准,不了,工作,发行,基础,s

    linux编程常用命令?

    linux编程常用命令?,系统,工作,信息,命令,地址,管理,工具,网络,基础,目录,lin

    linux脚本命令修改?

    linux脚本命令修改?,密码,系统,文件,资料,工具,软件,基础,地址,标准,命令,lin

    linux脚本命令教学?

    linux脚本命令教学?,标准,数据,系统,脚本,代码,流程,官网,底部,命令,变量,lin

    linux命令注释脚本?

    linux命令注释脚本?,代码,工具,名称,工作,脚本,发行,服务,环境,数据,基础,lin

    脚本linux上运行命令?

    脚本linux上运行命令?,工具,代码,时间,密码,系统,环境,名字,位置,第三,下来,t

    linux修改脚本的命令?

    linux修改脚本的命令?,系统,密码,服务,工作,工具,环境,信息,百度,代码,脚本,

    linux运行命令的脚本?

    linux运行命令的脚本?,系统,服务,工具,脚本,意外,技术,分析,文件,方法,命令,s

    linux脚本命令单引号?

    linux脚本命令单引号?,系统,工作,美元,地址,命令,信息,情况,标准,管理,引号,l

    执行linux脚本命令行?

    执行linux脚本命令行?,工具,位置,地方,环境,数据,状态,暂停,增长,系统,基础,

    linux暂停脚本命令?

    linux暂停脚本命令?,暂停,系统,状态,工具,命令,进程,材料,电脑,公开,名称,Lin

    linux运行脚本的命令?

    linux运行脚本的命令?,系统,工具,代码,服务,脚本,状态,密码,环境,位置,暂停,l

    linux命令解决方案?

    linux命令解决方案?,系统,管理,数据,电子,工作,电脑,软件,情况,不了,档案,lin

    编程解析linux命令?

    编程解析linux命令?,系统,标准,基础,设备,发行,电脑,工具,密码,名字,适当,如

    linux脚本多个命令?

    linux脚本多个命令?,系统,工作,标准,服务,命令,软件,管理,连续,电脑,设备,lin

    linux脚本注释命令?

    linux脚本注释命令?,代码,系统,工作,名称,地方,脚本,命令,信息,状态,简介,lin

    脚本运行linux命令?

    脚本运行linux命令?,系统,环境,工具,工作,位置,底部,代码,发行,官网,终端,lin

    linux启用命令模式?

    linux启用命令模式?,系统,密码,数字,首页,电脑,情况,终端,界面,模式,命令,安