[Tutor] How to run multiline shell command within python
Karim
kliateni at gmail.com
Thu Jan 10 14:07:16 EST 2013
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 <kliateni at gmail.com
> <mailto:kliateni at gmail.com>> wrote:
>
> On 10/01/2013 09:31, Hugo Arts wrote:
>> On Thu, Jan 10, 2013 at 7:01 AM, Karim <kliateni at gmail.com
>> <mailto:kliateni at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130110/658a7116/attachment.html>
More information about the Python-list
mailing list