python - 找不到文件错误 : [Errno 2] No such file or directory: 'ifconfig'

标签 python linux subprocess

我被教导如何通过 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)

关于python - 找不到文件错误 : [Errno 2] No such file or directory: 'ifconfig' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63805710/

相关文章:

python - 将方法传递给 Python 中的函数

python - 使用python的调度程序

python - 使用 loc 的 Pandas 浮点值问题

python - scriptExit 1 与 pybedtools venn_mpl - snakemake 5.2.4

ruby - 如何删除ruby中的控制字符

linux - 我怎样才能找出这些文件或进程的作用 (linux)

linux - 更改除一个文件夹之外的所有文件夹的所有者

Python子进程?

Python:使用 shell=False 的子进程调用不起作用

python - 我如何在 Python 中的两个不同进程之间共享一个串口