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

ishish ishish at domhain.de
Fri Aug 9 04:46:06 EDT 2013


Am 09.08.2013 05:47, schrieb David:
> On 9 August 2013 14:11, Adam Mercer <ramercer at gmail.com> 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:
>
> [...]
>
> At a quick glance, I have a couple of suggestions.
>
>>   command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file]
>
> sudo doesn't work like this. It doesn't read from standard input. You
> need to supply the command as an argument to sudo. Get the sudo 
> syntax
> correct by learning to use it in a shell (eg terminal running bash )
> before trying to use it from python code.
>
> Also, I think that passing the pipe character '|' as an argument to
> Popen is not the correct way to use pipes.
>
> So, if you figure out how to use sudo without '|' you will solve both
> these issues.

Or try to change the permissions

os.system("chmod 666 config_file)

do what you want with the file and set the permissions back to their 
original state.



More information about the Python-list mailing list