回溯(最近一次调用最后一次): 文件“hello.py”,第 12 行,位于 接口(interface)=选项.接口(interface) AttributeError:“值”对象没有属性“接口(interface)”
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 来运行这个程序。
最佳答案
add_option
的
dest
参数定义 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 ")
关于python - 为什么我收到属性错误 "AttributeError: ' 值的对象没有属性 'interface' ”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59638967/