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

A

Adam Mercer

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
 
D

Denis McMahon

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
 
N

Nobody

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')
 
N

Nobody

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top