Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'subject:file': 0.07; 'sys': 0.07; '%s"': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:> 40': 0.09; 'subject:script': 0.09; '"%s"\'': 0.16; '>on': 0.16; '>to': 0.16; '>try:': 0.16; 'adam': 0.16; 'message-id:@4ax.com': 0.16; 'oserror': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'retcode': 0.16; 'stdin=none,': 0.16; 'sudo': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'input': 0.22; 'aug': 0.22; 'preferred': 0.22; 'url:home': 0.24; 'header:X-Complaints-To:1': 0.27; 'external': 0.29; 'run': 0.32; 'running': 0.33; 'fri,': 0.33; 'subject:from': 0.34; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'impression': 0.36; 'skip:> 10': 0.36; 'charset:us-ascii': 0.36; 'two': 0.37; 'feed': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'calls,': 0.84; 'channel,': 0.84; 'subject:Using': 0.84; 'received:108': 0.93; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Using sudo to write to a file as root from a script Date: Fri, 09 Aug 2013 19:47:08 -0400 Organization: IISS Elusive Unicorn References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-108-68-178-245.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376092036 news.xs4all.nl 15888 [2001:888:2000:d::a6]:57415 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52285 On Fri, 9 Aug 2013 08:21:22 -0500, Adam Mercer declaimed the following: >On Thu, Aug 8, 2013 at 11:47 PM, David wrote: >import subprocess >import sys > >channel='stable' >config_file='/opt/ldg/etc/channel.conf' >command="echo -n %s | sudo tee %s > /dev/null" % (channel, config_file) > >try: > retcode = subprocess.call(command, shell=True) > if retcode < 0: > sys.exit('Error: Failed to set channel.conf') >except OSError as e: > sys.exit('Error: Execution failed "%s"' % e) > >But I was under the impression that Popen was the preferred approach >to running external processes? > It is... But you have to consider that you are attempting to run two processes with one invocation... You need two subprocess calls, one for the sudo and one for the echo, and you need to take the output of the echo process and feed it to the input of the sudo process. Something like (UNTESTED): sudo = subprocess.Popen("sudo tee %s" % config_file, stdin=subprocess.PIPE, stdout=subprocess.PIPE) echo = subprocess.Popen("echo -n %s" % channel, stdin=None, stdout=subprocess.PIPE) sudo.communicate(echo.communicate[0]) -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/