代码之家  ›  专栏  ›  技术社区  ›  phpjunkie

子项目。带有或“|”符号的Popen不起作用

  •  1
  • phpjunkie  · 技术社区  · 1 年前

    我试图只列出我的Wi-Fi网络适配器的IP地址,以便能够检测它是否已连接并附加了IP地址。

    有了它本身,它正在发挥作用。。。

    from subprocess import Popen
    
    Popen([
        'netsh',
        'interface',
        'ip',
        'show',
        'addresses',
        'Wi-Fi'
    ]).communicate()
    

    输出:

    Configuration for interface "Wi-Fi"
        DHCP enabled:                         No
        IP Address:                           192.168.1.200
        Subnet Prefix:                        192.168.1.0/24 (mask 255.255.255.0)
        Default Gateway:                      192.168.1.1
        Gateway Metric:                       0
        InterfaceMetric:                      2
    

    但是,有了这个。。。

    from subprocess import Popen
    
    Popen([
        'netsh',
        'interface',
        'ip',
        'show',
        'addresses',
        'Wi-Fi',
        '|',
        'findstr',
        '/ir',
        'IP Address'
    ]).communicate()
    

    与或 | 列表中的符号,它正在生成此。。。

    Usage: show addresses  [[name=]<string>]
    
    Parameters:
    
           Tag         Value
           name      - The name or index of a specific interface.
    
    Remarks: Displays the IP address configuration for an interface or
             interfaces.
    
    The information displayed for this command consists of:
    
    Field              Description
    -----              -----------
    DHCP enabled       Shows whether the address comes from static or DHCP
                       configuration.
    IP Address         Shows the IP address configured for an interface.
    Subnet Mask        Shows the subnet mask associated with the IP address.
    Default Gateway    Shows the IP address of a default gateway for the interface.
    Gateway Metric     Shows the metric for the default gateway shown above.
                       Only applies if multiple default gateways are configured.
    Interface Metric   Shows the metric for an interface.
                       Only applies if multiple interfaces are configured.
    
    Examples:
    
           show addresses "Wired Ethernet Connection"
    

    表示我键入了错误的适配器名称。

    我尝试了以下论点的多种组合 netsh 在名单上没有任何运气。

    有人对此有什么见解吗?

    我目前最好的猜测是,Popen不知道如何处理或 | 符号。

    1 回复  |  直到 1 年前
        1
  •  1
  •   Matt    1 年前

    我认为您的输出流设置不正确。当我运行你的代码时,我得到:

    Configuration for interface "Wi-Fi"\
    ...my network info here...
    
    None
    

    当我删除你的打印声明时,我没有得到 None 打印输出,但网络信息仍在打印中。。。所以它看起来像你的 stdout var正在设置为 没有

    通过自己的谷歌搜索,我想出了以下可以让你摆脱困境的方法:

    from subprocess import PIPE, Popen
    
    command = [
        'netsh',
        'interface',
        'ip',
        'show',
        'addresses',
        'Wi-Fi',
        '|',
        'findstr',
        '/ir',
        'IP Address'
    ]
    with Popen(command, stdout=PIPE, stderr=None, shell=True) as process:
        output = process.communicate()[0].decode("utf-8")
        print(output)
    

    这产生了:

    IP Address: <My-IP>

    不完全确定你的代码有什么问题,但如果我删除 shell=True 从我的Popen电话中,我得到了和你一样的错误。