Python如何从迭代中获取与subprocess.Popen一起运行的变量输入

问题描述 投票:1回答:1

下面的代码使用http脚本迭代列表。然后应该将变量输入到x变量中。输出打印到文件。但文件产生输出说明script=",尝试过其他变化,但总是有错误。

with open("nmap_http_output.txt", "w") as f: 
    for x in scripts:   # iterate over http scripts
            print(x)  # for debugging purposes - this prints
            print(ip) # for debugging purposes - this prints
            nmap_http_ps = subprocess.Popen(['nmap', '-p80', ' --script=', x, ip], stdout=f, stderr=subprocess.STDOUT)
            output = nmap_http_ps.communicate()
            print(output)

输出是:

Nmap done: 1 IP address (1 host up) scanned in 0.30 seconds
Starting Nmap 7.60 ( https://nmap.org ) at 2017-12-18 08:51 CST
Failed to resolve " --script=".
Failed to resolve """.
Unable to split netmask from target expression: "/usr/share/nmap/scripts/http-auth-finder.nse"
Failed to resolve """.
python subprocess nmap
1个回答
0
投票

看起来你有Popen错误的参数。

with open("nmap_http_output.txt", "w") as f: 
    for x in scripts:   # iterate over http scripts
        print(x)  # for debugging purposes - this prints
        print(ip) # for debugging purposes - this prints
        nmap_http_ps = subprocess.Popen(
            ['nmap', '-p', '80', ' --script=%s' % x, ip], stdout=f, stderr=subprocess.STDOUT
            )
        output = nmap_http_ps.communicate()
        print(output)

Popen期望一个命令的“元素”列表,但你必须要小心什么构成一个“元素”。我相信(现在不能测试)你必须解决两件事:

  • '-p'成为'-p', '80'(两个元素 - 一个开关和一个参数)
  • ' --script=', x成为'--script=%s' % x(而不是两个元素,这应该只是一个 - 并且还有一个领先的空间太多)
© www.soinside.com 2019 - 2024. All rights reserved.