Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'subject:: [': 0.03; 'argument': 0.04; 'session.': 0.07; 'skip:/ 10': 0.07; 'subject:How': 0.09; 'python': 0.09; 'grep': 0.09; 'subject:command': 0.09; 'cc:addr:python-list': 0.10; 'looked': 0.10; 'subject:python': 0.11; '0.6': 0.16; 'aux': 0.16; 'cc:name:python list': 0.16; 'echo': 0.16; 'shell:': 0.16; 'subject:Tutor': 0.16; 'subject:run': 0.16; 'wrote:': 0.17; 'specify': 0.17; 'thu,': 0.17; 'accepting': 0.18; 'jan': 0.18; 'shell': 0.18; '>>>': 0.18; 'input': 0.18; 'subject:] ': 0.19; 'variable': 0.20; 'email addr:gmail.com>': 0.20; 'all,': 0.21; 'trying': 0.21; '>>>': 0.22; 'class.': 0.23; 'specially': 0.23; 'cc:2**1': 0.24; 'command': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'url:wiki': 0.26; 'am,': 0.27; 'lines': 0.28; 'run': 0.28; 'arts': 0.29; 'multiline': 0.29; 'url:mailman': 0.29; 'code': 0.31; 'url:python': 0.32; 'file': 0.32; 'url:listinfo': 0.32; 'skip:s 30': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'skip:* 20': 0.35; 'especially': 0.35; 'received:192.168.0': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'url:org': 0.36; 'anything': 0.36; 'skip:p 20': 0.36; 'execute': 0.37; 'received:209': 0.37; 'possible.': 0.38; 'several': 0.39; 'received:209.85.214': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'url:mail': 0.40; 'within': 0.64; 'making': 0.64; 'dangerous': 0.66; 'directly.': 0.78; 'day!': 0.83; '2013': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:message-id:date:from:user-agent:mime-version:to:cc :subject:references:in-reply-to:content-type; bh=CVcu6BFF/VDqEqggcc7+3eYROPAwmxSu2XRBm7QmZq0=; b=ZWeYzB4L6iIJt8dCtuCfztrw15vP9ruJNO7yzG7erarsgI7TB9Gr0PBfA5x+ljb2ih nei6IIUQz0kyIt3Pr8G0BsZg/UdcrY3ocddzz7xqQilrlTaZzauekloTBJrAwXMCABD3 dbH1bRW9G5PK7+OOUZ8vzN9ufqdBv3SIayUm+tl3ehNRvs8NPAmNlqA/SL8NnM7IKipd 9mEEL0hFhmqzcW4jVaSyndL/MSjPdYAeKIuvcVJwOuxMchpqrnbGzZc4rpTFfhKw/6VI LItf6LVD/WyXI5oOU3MwYFlenJ3aNWI9nTTxK9maPkQ6y6iEy8P46EdO947iR0gqt7mo x64w== X-Received: by 10.204.128.148 with SMTP id k20mr35478110bks.107.1357844840063; Thu, 10 Jan 2013 11:07:20 -0800 (PST) Date: Thu, 10 Jan 2013 20:07:16 +0100 From: Karim User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: Matty Sarro Subject: Re: [Tutor] How to run multiline shell command within python References: <50EE5934.7090609@gmail.com> <50EECF75.5020400@gmail.com> In-Reply-To: Content-Type: multipart/alternative; boundary="------------000709090607000401050002" Cc: Python list , tutor python 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: 212 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1357844843 news.xs4all.nl 6966 [2001:888:2000:d::a6]:37768 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36583 This is a multi-part message in MIME format. --------------000709090607000401050002 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 10/01/2013 16:21, Matty Sarro wrote: > Have you looked a the pexpect class? It works like gangbusters, > especially if you're trying to run something with an interactive shell. > > http://www.noah.org/wiki/pexpect > > > On Thu, Jan 10, 2013 at 9:25 AM, Karim > wrote: > > On 10/01/2013 09:31, Hugo Arts wrote: >> On Thu, Jan 10, 2013 at 7:01 AM, Karim > > wrote: >> >> >> >> Hello all, >> >> I want to run multiline shell command within python without >> using a command file but directly execute several lines of shell. >> I already use *subprocess.checkoutput("csh -f >> my_file.csh".split())* but I want to know if it is posssible >> to avoid making file and execute >> shell lines of code directly. >> >> >> Yes, this is very possible. Specify shell=True as an argument and >> you can do anything you can do in a shell: >> >> >>> commands = """echo hello >> ... echo hello | wc -l >> ... ps aux | grep python""" >> >>> b = subprocess.check_output(commands, shell=True) >> >>> print(b.decode('ascii')) >> hello >> 1 >> hugo 1255 1.0 0.6 777316 49924 ? Sl 09:14 0:08 >> /usr/bin/python2 /usr/bi >> hugo 6529 0.0 0.0 42408 7196 pts/0 S+ 09:23 0:00 >> python >> hugo 6559 0.0 0.0 10656 1128 pts/0 S+ 09:28 0:00 >> grep python >> >> >>> >> >> watch out though, accepting user input into the commands variable >> will lead to shell injection, which can be a dangerous security >> vulnerability. >> >> HTH, >> Hugo > > Many thanks Hugo. It makes my day! > In my case there are no possibilities for shell injection. It is > internal to a class. > > Regards > Karim > > > -- > http://mail.python.org/mailman/listinfo/python-list > > Thanks Matty! I will have a look specially for interactive session. Regards Karim --------------000709090607000401050002 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
On 10/01/2013 16:21, Matty Sarro wrote:
Have you looked a the pexpect class? It works like gangbusters, especially if you're trying to run something with an interactive shell.



On Thu, Jan 10, 2013 at 9:25 AM, Karim <kliateni@gmail.com> wrote:
On 10/01/2013 09:31, Hugo Arts wrote:
On Thu, Jan 10, 2013 at 7:01 AM, Karim <kliateni@gmail.com> wrote:


Hello all,

I want to run multiline shell command within python without using a command file but directly execute several lines of shell.
I already use *subprocess.checkoutput("csh -f my_file.csh".split())* but I want to know if it is posssible to avoid making file and execute
shell lines of code directly.


Yes, this is very possible. Specify shell=True as an argument and you can do anything you can do in a shell:

 >>> commands = """echo hello
... echo hello | wc -l
... ps aux | grep python"""
>>> b = subprocess.check_output(commands, shell=True)
>>> print(b.decode('ascii'))
hello
1
hugo      1255  1.0  0.6 777316 49924 ?        Sl   09:14   0:08 /usr/bin/python2 /usr/bi
hugo      6529  0.0  0.0  42408  7196 pts/0    S+   09:23   0:00 python
hugo      6559  0.0  0.0  10656  1128 pts/0    S+   09:28   0:00 grep python

>>> 

watch out though, accepting user input into the commands variable will lead to shell injection, which can be a dangerous security vulnerability.

HTH,
Hugo

Many thanks Hugo. It makes my day!
In my case there are no possibilities for shell injection. It is internal to a class.

Regards
Karim


--
http://mail.python.org/mailman/listinfo/python-list



Thanks Matty!

I will have a look specially for interactive session.

Regards
Karim
--------------000709090607000401050002--