簡體   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