Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52240 > unrolled thread

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

Started byAdam Mercer <ramercer@gmail.com>
First post2013-08-08 23:11 -0500
Last post2013-08-09 21:16 +0100
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Using sudo to write to a file as root from a script Adam Mercer <ramercer@gmail.com> - 2013-08-08 23:11 -0500
    Re: Using sudo to write to a file as root from a script Denis McMahon <denismfmcmahon@gmail.com> - 2013-08-09 04:36 +0000
    Re: Using sudo to write to a file as root from a script Nobody <nobody@nowhere.com> - 2013-08-09 21:12 +0100
      Re: Using sudo to write to a file as root from a script Nobody <nobody@nowhere.com> - 2013-08-09 21:16 +0100

#52240 — Using sudo to write to a file as root from a script

FromAdam Mercer <ramercer@gmail.com>
Date2013-08-08 23:11 -0500
SubjectUsing sudo to write to a file as root from a script
Message-ID<mailman.383.1376022002.1251.python-list@python.org>
Hi

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:

  channel = 'stable'
  config_file = '/opt/ldg/etc/channel.conf'
  command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file]
  p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  out, _ = p.communicate()

But it seems as if this isn't doing anything.

I just want to write the contents of the variable channel to the file
/opt/ldg/etc/channel.conf. But whatever I try just doesn't work. Can
anyone offer any pointers?

Cheers

Adam

[toc] | [next] | [standalone]


#52242

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-08-09 04:36 +0000
Message-ID<ku1rk5$ddf$1@dont-email.me>
In reply to#52240
On Thu, 08 Aug 2013 23:11:09 -0500, Adam Mercer wrote:

> Hi
> 
> 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:
> 
>   channel = 'stable'
>   config_file = '/opt/ldg/etc/channel.conf'
>   command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file]
>   p = subprocess.Popen(command, stdout=subprocess.PIPE,
>   stderr=subprocess.PIPE)
>   out, _ = p.communicate()
> 
> But it seems as if this isn't doing anything.
> 
> I just want to write the contents of the variable channel to the file
> /opt/ldg/etc/channel.conf. But whatever I try just doesn't work. Can
> anyone offer any pointers?

Do you find anything with:

$ grep sudo /var/log/auth.log

(you may need to specify a different log)

Is the process that's trying to use the sudo command allowed to do so 
without a password?

man sudoers

Note - after editing /etc/sudoers you must set the permissions back to 440

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#52270

FromNobody <nobody@nowhere.com>
Date2013-08-09 21:12 +0100
Message-ID<pan.2013.08.09.20.12.19.320000@nowhere.com>
In reply to#52240
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')

[toc] | [prev] | [next] | [standalone]


#52271

FromNobody <nobody@nowhere.com>
Date2013-08-09 21:16 +0100
Message-ID<pan.2013.08.09.20.16.13.353000@nowhere.com>
In reply to#52270
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web