<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">On 10/01/2013 16:21, Matty Sarro wrote:<br>
</div>
<blockquote
cite="mid:CAHUGJcEOgNunWTcFyXzz5BxVfRg9CUU9U05yzj7__zk32QHj9g@mail.gmail.com"
type="cite">
<div dir="ltr">Have you looked a the pexpect class? It works like
gangbusters, especially if you're trying to run something with
an interactive shell.
<div><br>
</div>
<div><a moz-do-not-send="true"
href="http://www.noah.org/wiki/pexpect">http://www.noah.org/wiki/pexpect</a><br>
</div>
</div>
<div class="gmail_extra"><br>
<br>
<div class="gmail_quote">On Thu, Jan 10, 2013 at 9:25 AM, Karim
<span dir="ltr"><<a moz-do-not-send="true"
href="mailto:kliateni@gmail.com" target="_blank">kliateni@gmail.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div text="#000000" bgcolor="#FFFFFF">
<div>
<div class="h5">
<div>On 10/01/2013 09:31, Hugo Arts wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">On Thu, Jan 10, 2013 at 7:01 AM,
Karim <span dir="ltr"><<a
moz-do-not-send="true"
href="mailto:kliateni@gmail.com"
target="_blank">kliateni@gmail.com</a>></span>
wrote:<br>
<div class="gmail_extra">
<div class="gmail_quote">
<blockquote class="gmail_quote"
style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><br>
<br>
Hello all,<br>
<br>
I want to run multiline shell command within
python without using a command file but
directly execute several lines of shell.<br>
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<br>
shell lines of code directly.<br>
<br>
</blockquote>
<div><br>
</div>
<div>Yes, this is very possible. Specify
shell=True as an argument and you can do
anything you can do in a shell:</div>
<div><br>
</div>
<div> >>> commands = """echo hello</div>
<div>... echo hello | wc -l</div>
<div>... ps aux | grep python"""</div>
<div>>>> b =
subprocess.check_output(commands,
shell=True)</div>
<div>>>> print(b.decode('ascii'))</div>
<div> hello</div>
<div>1</div>
<div>hugo 1255 1.0 0.6 777316 49924 ?
Sl 09:14 0:08 /usr/bin/python2
/usr/bi</div>
<div>hugo 6529 0.0 0.0 42408 7196
pts/0 S+ 09:23 0:00 python</div>
<div>hugo 6559 0.0 0.0 10656 1128
pts/0 S+ 09:28 0:00 grep python</div>
<div><br>
</div>
<div>>>> </div>
<div><br>
</div>
<div>watch out though, accepting user input
into the commands variable will lead to
shell injection, which can be a dangerous
security vulnerability.</div>
<div> <br>
</div>
<div>HTH,</div>
<div>Hugo</div>
</div>
</div>
</div>
</blockquote>
<br>
</div>
</div>
Many thanks Hugo. It makes my day!<br>
In my case there are no possibilities for shell injection.
It is internal to a class.<br>
<br>
Regards<span class="HOEnZb"><font color="#888888"><br>
Karim<br>
<br>
</font></span></div>
<br>
--<br>
<a moz-do-not-send="true"
href="http://mail.python.org/mailman/listinfo/python-list"
target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
<br>
</blockquote>
</div>
<br>
</div>
</blockquote>
<br>
Thanks Matty!<br>
<br>
I will have a look specially for interactive session.<br>
<br>
Regards<br>
Karim<br>
</body>
</html>