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

Question:

I were taught how to change mac address by 2 methods of subprocess.call while the first one was successful but the second method is giving me error and I am unable to understand it
this is my code:

#! /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"])

this is the error:

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

I am unable to progress because of this error.

Asked By: ganesh shukla

||

Answers:

In linux by default the subprocess.call doesn’t use a shell to run our commands so you can’t use the shell commands like cd.

so just add one more parameter to subprocess.call i.e shell=True and then try to execute.

For ex:

subprocess.call(["ifconfig", interface, "down"], shell=True)
Answered By: Mahesh Anakali
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.