toom
toom

Reputation: 13316

How do I execute the following command using subprocess?

I want to execute the following command using subprocess:

sudo sh -c "echo nameserver 1.1.1.1 > /etc/resolv.conf"

In the shell it does work well.

This is what I did:

update_resolv_conf_cmd = (["sudo", "sh", "-c", '"echo nameserver 1.1.1.1 > /etc/resolv.conf"'])
subprocess.Popen(update_resolv_conf_cmd, stdout=subprocess.PIPE, shell=True)

However, this does not work.

Upvotes: 0

Views: 40

Answers (1)

snd
snd

Reputation: 238

I think this may work out

import subprocess

subprocess.call("sudo sh -c 'echo nameserver 1.1.1.1 > /etc/resolv.conf'", shell=True)

Upvotes: 1

Related Questions