【问题标题】:python run shell command with pipelinepython用管道运行shell命令
【发布时间】:2018-10-12 06:45:27
【问题描述】:

我想在 python2 中运行ip -o -6 addr list | awk '{print $4}' | cut -d/ -f1,并获取 ipv6 地址。 os.system 确实运行了命令,但返回值为 0 或 1。

我查看了subprocess.call,当命令变得像| 一样复杂时,那些args 是什么?

【问题讨论】:

    标签: python-2.7


    【解决方案1】:
    proc = subprocess.Popen(["ip -o -6 addr list | awk '{{print $4}}' | cut -d/ -f1"], stdout=subprocess.PIPE, shell=True)
    ipv6_all = proc.communicate()[0]
    

    * question Eric Renouf 的回答中得到这个想法。

    【讨论】:

      相关资源