回溯(最近通话最近): 在第12行的文件“ hello.py”中 接口= options.interface AttributeError:“值”对象没有属性“接口”
import subprocess
import optparse
parser = optparse.OptionParser()
parser.add_option("-i", "--interface", dest=" interface ", help=" Interface to change its MAC address ")
parser.add_option("-m", "--mac", dest=" mac ", help=" new mac address ")
(options, arguments) = parser.parse_args()
interface = options.interface
mac = options.mac
print("[+] Changing mac address for " + interface + " to " + mac)
subprocess.call(["ifconfig ", interface, " down"])
subprocess.call(["ifconfig ", interface, " hw", "ether", mac])
subprocess.call(["ifconfig ", interface, " up"])
注意:-我正在使用virtualBox运行该程序。
答案 0 :(得分:2)
dest
的 add_option
参数定义Values
对象的成员。您使用的名称前后都有空格。这定义了其中带有空格的成员,而经典字段访问无法访问这些成员。
要调试它,请执行以下操作:
print(dir(options))
此打印:
[' interface ', ' mac ', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', … other members ...]
删除空格,您将可以访问interface
和mac
:
parser.add_option("-i", "--interface", dest="interface", help=" Interface to change its MAC address ")
parser.add_option("-m", "--mac", dest="mac", help=" new mac address ")