#! /usr/bin/env python
import subprocess
import optparse
def get_arguments():
parser=optparse.OptionParser()
parser.add_option("-i", "--interface", dest="interface", help="interface to change mac adress for")
parser.add_option("-m", "--mac", dest="new_mac", help="change mac address")
return parser.parse_args()
def change_mac(interface, new_mac):
print("changing you mac address")
subprocess.call(["ifconfig ", interface, " down"])
subprocess.call(["ifconfig ", interface, " hw", " ether", new_mac])
subprocess.call(["ifconfig ", interface, " up"])
(options,arguments)=get_arguments()
change_mac(options.interface, options.new_mac)我得到的错误如下:
Traceback (most recent call last):
File "mac_tester.py", line 21, in <module>
change_mac(options.interface, options.new_mac)
File "mac_tester.py", line 15, in change_mac
subprocess.call(["ifconfig ", interface, " down"])
File "/usr/lib/python2.7/subprocess.py", line 172, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 394, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory发布于 2022-01-30 12:46:11
在传递给subprocess.call的可执行文件的名称中有一个尾随空间,在其他参数中有一个前导空格。下面是第一行,另外两行相似:
subprocess.call(["ifconfig ", interface, " down"])
# trailing space here ----^ ^^
# leading spaces here --------------------||这会导致您的计算机寻找一个名为ifconfig的可执行文件,它的末尾有一个空格,但是它找不到这个可执行文件,因此您会得到一个错误。
当然,如果您收到的错误包含了错误消息中可执行文件的名称,例如No such file or directory: "ifconfig ",则可能更容易找到问题。
也许你认为你需要这些空间,因为你认为
subprocess.call(["ifconfig", interface, "down"])最终会试图运行类似ifconfigeth0down的程序吗?情况并非如此:使用subprocess.call的原因是确保可执行文件获得您给它的确切参数,即使参数包含空格或类似的参数。
移除多余的空格,然后再试一次。
https://stackoverflow.com/questions/70912860
复制相似问题