【问题标题】:How to programmatically enable/disable network interfaces? (Windows XP)如何以编程方式启用/禁用网络接口? (Windows XP)
【发布时间】:2010-09-10 04:06:03
【问题描述】:

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

【问题讨论】:

    标签: python networking windows-xp


    【解决方案1】:

    使用 netsh 接口 用法 set interface [name = ] IfName [ [管理员 = ] 已启用|已禁用 [连接 = ] 已连接|已断开 [新名称 = ] 新名称 ]

    尝试将所有内容都包含在外括号内: netsh interface set interface name="thename" admin=disabled connect=DISCONNECTED newname="thename"

    另请参阅此 MS 知识库页面:http://support.microsoft.com/kb/262265/ 您可以遵循他们的任何建议。 要禁用适配器,您需要确定引用硬件设备的方法。如果计算机上没有多个具有相同名称的适配器,您可能会偏离接口的描述(或 PCI ID 工作良好)。之后,使用 devcon(禁用|启用)。 Devcon 是设备管理器的附加控制台界面。

    【讨论】:

    • 包含所有选项会产生以下消息:“无法连接、断开、启用或禁用专用接口。”它应该工作,但它没有。不过,还没有在另一台电脑上测试过;我将会。谢谢。
    • 我遇到了同样奇怪的问题。 Netsh 似乎无法按预期工作,在这个 Windows XP 系统上我正在尝试这样做。
    • 您找到解决“专用接口无法...”错误的方法了吗?
    【解决方案2】:

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

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

    它被翻译为 AFAIU,为通用 WMI 错误 0x80041001。可能是权限。

    【讨论】:

    • 显然我是作为本地管理员组的成员运行的,并且计算机不是域的一部分。
    • Traceback(最近一次调用最后一次):文件“C:\Python33\lib\site-packages\wmi.py”,第 416 行,在 call 结果 = self. ole_object.ExecMethod_ (self.method.Name) File ">", line 3, in ExecMethod_ pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'SWbemObjectEx', 'Invalid method Parameter (s) ', 无, 0, -2147217361), 无)
    • @HaifengLi:谢谢,但是我不再做Windows了,上面的代码也没有在Python 3上测试过(注意答案的日期)。
    • 感谢您的声明。
    【解决方案3】:

    我在互联网上找到了这个 .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
    

    【讨论】:

      【解决方案4】:

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

      如果你想做纯Python,或许可以开启一组管道与netsh进程通信。

      【讨论】:

        【解决方案5】:

        这是 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 接口设置接口 已停用

        【讨论】:

        • 我过去尝试过 netsh 建议的所有组合:netsh interface set interface [name=]wifi [admin=]DISABLED 带有“参数不正确”或“一个或多个基本参数不正确”消息。
        【解决方案6】:

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

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

        【讨论】:

        • 它似乎不适用于网络接口。 “devcon enable wifi”,其中 wifi 是无线接口的名称 回复:“未启用设备。”
        【解决方案7】:

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

        【讨论】:

        • 这确实是一个很好的起点。我在做这个工作。谢谢。
        猜你喜欢
        • 2010-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-03
        • 2016-05-07
        • 1970-01-01
        • 2014-01-09
        • 2011-03-04
        相关资源
        最近更新 更多