Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何以编程方式启用/禁用网络接口?(Windows XP)_Python_Networking_Windows Xp - Fatal编程技术网

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

Python 如何以编程方式启用/禁用网络接口?(Windows XP),python,networking,windows-xp,Python,Networking,Windows Xp,我需要在Windows XP中通过脚本完全启用/禁用网络接口。我正在寻找一个python解决方案,但是任何通用的方法(例如WMI、一些命令行、一些windows调用)都是受欢迎的,并将进行调整。谢谢。该工具可以控制NIC,但不能直接控制接口。它是设备管理器小程序的命令行版本 devcon disable (id or portion of name) devcon enable (id or portion of name) 这是VB.Net Dim objectQuery As New Ob

我需要在Windows XP中通过脚本完全启用/禁用网络接口。我正在寻找一个python解决方案,但是任何通用的方法(例如WMI、一些命令行、一些windows调用)都是受欢迎的,并将进行调整。谢谢。

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

devcon disable (id or portion of name)
devcon enable (id or portion of name)
这是VB.Net

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接口集接口 残废


除了RASAPI之外,我似乎找不到任何控制MSDN接口的基本API,但我认为它们不适用于非拨号连接。正如您自己所建议的,netsh可能是一个选项,假定它也有一个编程接口:


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

您可能需要使用WMI。这可以作为一个良好的起点:

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

>>> import wmi; c=wmi.WMI()
>>> o=c.query("select * from Win32_NetworkAdapter where NetConnectionID='wifi'")[0]
>>> o.EnableDevice(1)
(-2147217407,)
AFAIU将其转换为通用WMI错误0x80041001。可能是权限。

使用netsh接口 用法集接口[name=]IfName [[admin=]已启用|已禁用 [连接=]已连接|已断开 [新名称=]新名称]

尝试将所有内容包括在外支架内: netsh interface set interface name=“thename”admin=disabled connect=DISCONNECTED newname=“thename”

另请参见本MS KB页: 你可以听从他们的建议。
要禁用适配器,需要确定引用硬件设备的方法。如果计算机上没有多个具有相同名称的适配器,则可能会偏离接口的描述(或者PCI ID工作正常)。之后,使用devcon(disable | enable)。Devcon是设备管理器的附加控制台界面。

我在internet上找到了这个.VBS脚本。它有一个很酷的优点,就是在我无法让NETSH为此工作的机器上工作

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

它似乎不适用于网络接口。“devcon enable wifi”,其中wifi是无线接口的名称,回答:“未启用任何设备”。我过去尝试过netsh建议的所有组合:
netsh interface set interface[name=]wifi[admin=]DISABLED
与“参数不正确”或“一个或多个基本参数不正确”这确实是一个很好的起点。我正在努力。谢谢。显然,我是作为本地管理员组的成员运行此程序的,并且计算机不是域的一部分。回溯(最近一次调用):文件“C:\Python33\lib\site packages\wmi.py”,第416行,在调用结果=self.ole\u object.ExecMethod\uuz(self.method.Name)文件“”中,第3行,在ExecMethod\upywintypes.com\u错误:(-2147352567,'异常发生',(0,'SWbemObjectEx','无效的方法参数',无,0,-2147217361),无)@HaifengLi:谢谢,但我不再使用Windows,上面的代码也没有在Python 3上测试过(注意答案的日期)。谢谢您的声明。包括所有选项都会产生以下消息:“无法连接、断开、启用或禁用专用接口。“它应该能工作,但不能。不过,还没有在另一台计算机上测试过;我会。谢谢。我遇到了同样的奇怪问题。Netsh似乎不能按预期工作,在我尝试在Windows XP系统上执行此操作。您是否找到了解决“专用接口不能…”错误的方法?