Using sudo to write to a file as root from a script

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Using sudo to write to a file as root from a script

Travis McGee
On Thu, 08 Aug 2013 23:11:09 -0500, Adam Mercer wrote:

> I'm trying to write a script that writes some content to a file root
> through sudo, but it's not working at all. I am using:

>   command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file]

You can't create a pipeline like this. All of the list elements after the
first will be passed as arguments to "echo".

Try:

  command = ['sudo', 'tee', config_file]
  p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  out, _ = p.communicate('channel')



Reply | Threaded
Open this post in threaded view
|

Using sudo to write to a file as root from a script

Travis McGee
On Fri, 09 Aug 2013 21:12:20 +0100, Nobody wrote:

> Try:
>
>   command = ['sudo', 'tee', config_file]
>   p = subprocess.Popen(command, stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)
>   out, _ = p.communicate('channel')

Oops; you also need stdin=subprocess.PIPE.