Subprocess Python ..将命令串在一起

use*_*384 0 python linux subprocess python-3.x

我正在编写一个Python程序,需要返回在我的一个漏洞扫描中扫描的活动主机.我在返回XML之前使用过这种方法,但是当我尝试使用cut和grep这些额外的程序时,我遇到了问题.也许它不喜欢"管道"| 或者我可能在这里使用逗号做了一些完全错误但是我已经尝试了各种各样的东西,并且似乎无法让它返回结果,就像我从命令行独立运行命令一样.非常感谢您提供的任何帮助.

def activeHostsQuery():
    args = ['curl', '-s', '-k', '-H', 'X-Requested-With: curl demoapp', '-u','username:password', 'https://qualysapi.qualys.com/api/2.0/fo/scan/?action=fetch&scan_ref=scan/1111111.22222&mode=brief&output_format=csv', '|', 'cut', '-d', '-f1', '|', 'sort', '|', 'uniq', '|', 'grep', '-E', '"\"[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\""', '|', 'wc', '-l']

    activeHostsNumber = subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0]
    return activeHostsNumber
Run Code Online (Sandbox Code Playgroud)

Cha*_*ffy 6

将命令串在一起的正确方法 - 如果你想保留shell,你应该这样做 - 就是创建多个Popen对象.

def activeHostsQuery():
    args1 = ['curl', '-s', '-k',
             '-H', 'X-Requested-With: curl demoapp',
             '-u','username:password',
             'https://qualysapi.qualys.com/api/2.0/fo/scan/?action=fetch&scan_ref=scan/1111111.22222&mode=brief&output_format=csv']
    args2 = ['cut', '-d', '-f1']
    args3 = ['sort', '-u']
    args4 = ['grep', '-E', '"\"[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\""']
    args5 = ['wc', '-l']

    p1 = subprocess.Popen(args1, stdout=subprocess.PIPE)
    p2 = subprocess.Popen(args2, stdin=p1.stdout, stdout=subprocess.PIPE); p1.stdout.close()
    p3 = subprocess.Popen(args3, stdin=p2.stdout, stdout=subprocess.PIPE); p2.stdout.close()
    p4 = subprocess.Popen(args4, stdin=p3.stdout, stdout=subprocess.PIPE); p3.stdout.close()
    p5 = subprocess.Popen(args5, stdin=p4.stdout, stdout=subprocess.PIPE); p4.stdout.close()
    activeHostsNumber = p5.communicate()[0]
    return activeHostsNumber
Run Code Online (Sandbox Code Playgroud)

这样做的好处是不涉及shell - 您可以将任意变量替换到参数列表中,而不必担心它们会被字符串拆分,误解,导致重定向或其他任何内容,以及用于生成的参数之间的区别您的名单将受到尊重.

现在,在这种特殊情况下,我会在本机Python中完成所有工作 - 当你拥有本机HTTP库时,甚至没有理由使用curl - 但是知道如何使用subprocess.Popen构建管道在任何情况下都很有用.