繁体   English   中英

FileNotFoundError: [Errno 2] 没有这样的文件或目录:'ifconfig'

[英]FileNotFoundError: [Errno 2] No such file or directory: 'ifconfig'

我开始从 udemy 课程学习道德黑客。我们被教导如何通过 subprocess.call 的两种方法更改 mac 地址,而第一种方法成功了,但第二种方法给了我错误,我无法理解这是我的代码

#! /usr/bin/python3.5
import subprocess
interface = input("Enter Interface of Your Choice: ")
mac_chg = input("Enter New Mac Address: ")
print("[+]Changing Mac ADDRESS of " + interface + " to " + mac_chg)
subprocess.call(["ifconfig", interface, "down"])
subprocess.call(["ifconfig", interface, "hw", "ether", mac_chg])
subprocess.call(["ifconfig", interface, "up"])

这是错误

Traceback (most recent call last):
  File "/home/kali/PycharmProjects/hello/main.py", line 7, in <module>
    subprocess.call(["ifconfig", interface, "down"])
  File "/usr/lib/python3.8/subprocess.py", line 340, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/usr/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ifconfig'

Process finished with exit code 1

由于此错误,我无法继续前进,谢谢

在 linux 中,subprocess.call 默认不使用 shell 来运行我们的命令,所以你不能使用像 cd 这样的 shell 命令。

所以只需在 subprocess.call 中再添加一个参数,即 shell=True,然后尝试执行。

例如:

subprocess.call(["ifconfig", interface, "down"], shell=True)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM