0

I'm having problems with call method in the subprocess module while executing multiple commands in a single script. The following code works just fine:

subprocess.call('ifconfig eth0 down', shell=True)
subprocess.call('ifconfig eth0 hw ether 00:99:99:99:99:99', shell=True)
subprocess.call('ifconfig eth0 up', shell=True)

But from some source I found out the following code which is said to work fine but isn't working with me:

subprocess.call(['ifconfig eth0 down'])
subprocess.call(['ifconfig eth0 hw ether 00:99:99:99:99:99'])
subprocess.call(['ifconfig eth0 up'])

In my case it gives the following error:

Traceback (most recent call last):
  File "new.py", line 3, in <module>
    subprocess.call(['ifconfig eth0 down'])
  File "/home/anaconda3/lib/python3.7/subprocess.py", line 339, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/home/anaconda3/lib/python3.7/subprocess.py", line 800, in __init__
    restore_signals, start_new_session)
  File "/home/anaconda3/lib/python3.7/subprocess.py", line 1551, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ifconfig eth0 down': 'ifconfig eth0 down'

Anyone has any idea what might be the issue. Thanks in advance.

1 Answers1

0

You didn't link to "some source", but I suspect you have misread something. Those examples should probably look something like this:

subprocess.call(['ifconfig', 'eth0', 'down'])

That is, if you are not setting shell=True, then the first argument to subprocess.call needs to be a list of command line arguments.

larsks
  • 277,717
  • 41
  • 399
  • 399