文件“/usr/lib/python2.7/subprocess.py”,第 1047 行,在 _execute_child 中提高 child_exception
File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child raise child_exception
#! /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
在传递给 subprocess.call
的可执行文件的名称中有尾随 space,在其他参数中有前导 space。这是第一行,其他两行类似:
subprocess.call(["ifconfig ", interface, " down"])
# trailing space here ----^ ^^
# leading spaces here --------------------||
这会导致您的计算机寻找名为 ifconfig
的可执行文件,该可执行文件的末尾带有 space,但找不到此可执行文件,因此您会收到错误消息。
当然,如果您收到的错误在错误消息中包含可执行文件的名称,例如 No such file or directory: "ifconfig "
.
,可能会更容易找到问题
也许您认为您需要 space 是因为您认为
subprocess.call(["ifconfig", interface, "down"])
最终会尝试 运行 类似 ifconfigeth0down
的东西吗?事实并非如此:使用 subprocess.call
的原因是确保可执行文件获得您提供的确切参数,即使参数包含 spaces 或类似内容。
删除多余的 space 并重试。
#! /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
在传递给 subprocess.call
的可执行文件的名称中有尾随 space,在其他参数中有前导 space。这是第一行,其他两行类似:
subprocess.call(["ifconfig ", interface, " down"])
# trailing space here ----^ ^^
# leading spaces here --------------------||
这会导致您的计算机寻找名为 ifconfig
的可执行文件,该可执行文件的末尾带有 space,但找不到此可执行文件,因此您会收到错误消息。
当然,如果您收到的错误在错误消息中包含可执行文件的名称,例如 No such file or directory: "ifconfig "
.
也许您认为您需要 space 是因为您认为
subprocess.call(["ifconfig", interface, "down"])
最终会尝试 运行 类似 ifconfigeth0down
的东西吗?事实并非如此:使用 subprocess.call
的原因是确保可执行文件获得您提供的确切参数,即使参数包含 spaces 或类似内容。
删除多余的 space 并重试。