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


Groups > comp.lang.python > #36566

Re: [Tutor] How to run multiline shell command within python

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin.stu.neva.ru!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <hugo.yoshi@gmail.com>
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; 'skip:/ 10': 0.07; 'subject:How': 0.09; 'python': 0.09; 'grep': 0.09; 'subject:command': 0.09; 'cc:addr :python-list': 0.10; 'subject:python': 0.11; '0.6': 0.16; 'aux': 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; 'skip:p 30': 0.20; 'variable': 0.20; 'email addr:gmail.com&gt;': 0.20; 'all,': 0.21; 'cc:no real name:2**0': 0.24; 'cc:2**1': 0.24; 'command': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'lines': 0.28; 'run': 0.28; 'multiline': 0.29; 'skip:& 10': 0.29; 'code': 0.31; 'file': 0.32; 'skip:s 30': 0.33; 'skip:& 20': 0.33; 'received:google.com': 0.34; 'skip:* 20': 0.35; 'received:209.85': 0.35; 'but': 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; 'header:Received:5': 0.40; 'skip:\xc2 10': 0.62; 'within': 0.64; 'making': 0.64; 'dangerous': 0.66; 'directly.': 0.78; '2013': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=ob+Ymsbf+P1TuC+Pc9RYcpX0t8D6tdJNe4DqLFCf6w0=; b=c1RXF3Hdb2AtiqrbLcy5zbmBCiVkjZaGdPBmiQvhIVQs3vIVXnYX6GJTGmABdLKhms i6O7/S+/TYilc9nzxJhqBWpFn5FFVMsn+9718t4SxKN8dTxcocMiRBSgOEs9meq1oYF0 zBztsCB0vnh/V+WSMEQdpN4Jrp0ezBs2m/byIjY0GHXNIe4Vhsd0sWmNjLLXD53aY2Rh Hz9LzjXp6QwfPNwxwDBAscjV4ewCEyKwwO3jef82UScXN8v05wwcZlj/vhGPkLKhNlBw pxbfieUvSbQL1QRSGOt/Ic+SffCATNCst01oI3sKiaq8N5lD2wRWrpRbrDjE8ytXObid O0kw==
MIME-Version 1.0
In-Reply-To <50EE5934.7090609@gmail.com>
References <b1c7ea47-47b4-4784-8037-e0856fdaa7cd@googlegroups.com> <50EE5934.7090609@gmail.com>
From Hugo Arts <hugo.yoshi@gmail.com>
Date Thu, 10 Jan 2013 09:31:43 +0100
Subject Re: [Tutor] How to run multiline shell command within python
To Karim <kliateni@gmail.com>
Content-Type multipart/alternative; boundary=00151761ca5810a95c04d2eb049a
Cc python-list@python.org, *tutor python <tutor@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.366.1357806741.2939.python-list@python.org> (permalink)
Lines 83
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1357806741 news.xs4all.nl 6934 [2001:888:2000:d::a6]:46350
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:36566

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Psycopg2 SyntaxError: invalid syntax on "INSERT INTO" database andydtaylor@gmail.com - 2013-01-09 15:52 -0800
  Re: Psycopg2 SyntaxError: invalid syntax on "INSERT INTO" database John Gordon <gordon@panix.com> - 2013-01-10 00:08 +0000
    Re: Psycopg2 SyntaxError: invalid syntax on "INSERT INTO" database andydtaylor@gmail.com - 2013-01-09 16:19 -0800
      Re: Psycopg2 SyntaxError: invalid syntax on "INSERT INTO" database Mitya Sirenef <msirenef@lightbird.net> - 2013-01-09 20:07 -0500
      Re: Psycopg2 SyntaxError: invalid syntax on "INSERT INTO" database MRAB <python@mrabarnett.plus.com> - 2013-01-10 01:11 +0000
        Re: Psycopg2 SyntaxError: invalid syntax on "INSERT INTO" database andydtaylor@gmail.com - 2013-01-09 18:20 -0800
          Re: Psycopg2 SyntaxError: invalid syntax on "INSERT INTO" database Mitya Sirenef <msirenef@lightbird.net> - 2013-01-09 21:56 -0500
        Re: Psycopg2 SyntaxError: invalid syntax on "INSERT INTO" database andydtaylor@gmail.com - 2013-01-09 18:20 -0800
  How to run multiline shell command within python Karim <kliateni@gmail.com> - 2013-01-10 07:01 +0100
  Re: [Tutor] How to run multiline shell command within python Hugo Arts <hugo.yoshi@gmail.com> - 2013-01-10 09:31 +0100
  Re: [Tutor] How to run multiline shell command within python Karim <kliateni@gmail.com> - 2013-01-10 15:25 +0100
  Re: [Tutor] How to run multiline shell command within python Matty Sarro <msarro@gmail.com> - 2013-01-10 10:21 -0500
  Re: [Tutor] How to run multiline shell command within python Karim <kliateni@gmail.com> - 2013-01-10 20:07 +0100

csiph-web