<div>Hi Mike</div><div><br></div><div>have you tried running the tshark process in the background...<br></div><div><br></div><div><em>CMD_LIST=[PLINK,sess_name,&#39;/usr/bin/sudo /usr/sbin/tshark <strong>&amp;</strong>&#39;, &#39;-a&#39;, &#39;duration:10&#39;, &#39;-i&#39;,  &#39;wlan0&#39;, &#39;-T&#39;, &#39;text&#39;, &#39;-V&#39;,&#39;&gt;&#39;, &#39;out.txt&#39;];</em></div>
<div><br></div><div>Or if you are using NOHUP try redirecting this way...</div><div><br></div><div><em>CMD_LIST=[PLINK,sess_name,&#39;/usr/bin/nohup&#39;,&#39;/usr/bin/sudo /usr/sbin/tshark <strong>&gt; out.txt 2&gt; out.err &lt; /dev/null</strong> &#39;, &#39;-a&#39;, &#39;duration:10&#39;, &#39;-i&#39;,  &#39;wlan0&#39;, &#39;-T&#39;, &#39;text&#39;,&#39;&amp;&#39;];</em></div>
<div><br></div><div>Regards</div><div>Yashwin Kanchan</div><div><br></div><div><br></div><div class="gmail_quote">On 21 April 2010 19:15, Mike Baker <span dir="ltr">&lt;<a href="mailto:mibaker88@gmail.com">mibaker88@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div>Thanks Tim,</div>
<div> </div>
<div>Your subprocess examples got me started in the right direction.  I&#39;ve moved on to a slightly more advanced problem that I need help with.</div>
<div> </div>
<div>I want to remotely start a Tshark packet capture session on one of our Linux machines in the lab.  I want to start the session from my Windows machine running Python 2.5.  The output capture file needs to be saved on the remote Linux machine.</div>


<div> </div>
<div>The example below nearly does what I want.  It starts Tshark via Putty, runs for 10 seconds then writes the capture file (out.txt) to a remote Linux machine.  The problem is that the putty session hangs open while Tshark is running. So, I can&#39;t execute additional Python commands until the Tshark capture finishes.</div>


<div> </div>
<div>I&#39;ve experimented with the Unix nohup command, but it does not seem to work as expected with Tshark.  If you call my function below with</div>
<div>&gt;&gt;&gt; test_subp(alt_cmd=1)</div>
<div>then the nohup command is added to the subprocess command list (along with a trailing &#39;&amp;&#39; to send the command to background).  This should work.  Using this alternate command, out.txt gets created, but is always empty. </div>


<div> </div>
<div> </div>
<div>Here is my code:</div>
<div>**********************************************</div>
<div>def test_subp(alt_cmd=0):<br>    &#39;&#39;&#39;Establish a Putty connection with one of our Linux machines in the lab.<br>    Send Tshark command to start a data collection session over Putty.<br>    &#39;&#39;&#39;</div>


<div>    PLINK = &#39;C:\\Progra~1\\putty\\plink&#39;<br>    sess_name=&#39;LabComp1&#39;<br>    if alt_cmd:<br>        &#39;&#39;&#39;This command does not work as expected.  The tshark output file (out.txt)is created,<br>

        but there is nothing in it &#39;&#39;&#39;<br>        CMD_LIST=[PLINK,sess_name,&#39;/usr/bin/nohup&#39;,&#39;/usr/bin/sudo /usr/sbin/tshark&#39;, &#39;-a&#39;, &#39;duration:10&#39;, &#39;-i&#39;,  &#39;wlan0&#39;, &#39;-T&#39;, &#39;text&#39;, &#39;-V&#39;,&#39;&gt;&#39;, &#39;out.txt&#39;,&#39;&amp;&#39;];<br>

    else:<br>        &#39;This command works great, writing tshark output to out.txt on the remote machine.&#39;<br>        &#39;Unfortunately, this command hangs the putty session until the tshark capture ends&#39;<br>        CMD_LIST=[PLINK,sess_name,&#39;/usr/bin/sudo /usr/sbin/tshark&#39;, &#39;-a&#39;, &#39;duration:10&#39;, &#39;-i&#39;,  &#39;wlan0&#39;, &#39;-T&#39;, &#39;text&#39;, &#39;-V&#39;,&#39;&gt;&#39;, &#39;out.txt&#39;];<br>

    print &quot;The command list you are sending to the subprocess is: \n&quot;, &quot;\t&quot;, CMD_LIST<br>        <br>    PIPE = subprocess.PIPE<br>    p = subprocess.Popen(CMD_LIST, stdout=PIPE, stderr=PIPE)<br>    stdout, stderr = p.communicate ()<br>

    print &#39;stdout = &#39;, stdout<br>    print &#39;stderr = &#39;, stderr</div>
<div>*********************************************************************</div>
<div> </div>
<div>For both runs (atl_cmd=0 or alt_cmd=1), the stdout and stderr printouts at the end of the script are empty.</div>
<div> </div>
<div>Any suggestions would be appreciated.  </div>
<div> </div>
<div>Thanks,</div>
<div> </div>
<div>    Mike</div>
<div>*******************************************************************************************************<br><br></div><div><div class="h5">
<div class="gmail_quote">On Wed, Mar 31, 2010 at 7:42 AM, Tim Golden <span dir="ltr">&lt;<a href="mailto:mail@timgolden.me.uk" target="_blank">mail@timgolden.me.uk</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="padding-left:1ex;margin:0px 0px 0px 0.8ex;border-left:#ccc 1px solid">
<div>On 30/03/2010 17:29, Mike Baker wrote:<br></div>
<blockquote class="gmail_quote" style="padding-left:1ex;margin:0px 0px 0px 0.8ex;border-left:#ccc 1px solid">
<div>I&#39;m trying to connect to a Linux box from my Windows machine and execute a<br>series of commands<br><br></div>
<div>I want a script to always<br>execute the same series of commands without having to do so manually.   I<br>also have code that will execute a single command like cat a file and write<br>the ouput to a new file. However, when I try to use the communicate object<br>

in subprocess, my window hangs.<br></div></blockquote><br><br>This works for me:<br><br>&lt;code&gt;<br>import os, sys<br>import subprocess<br><br>PLINK = &quot;plink&quot;<br>REMOTE_USER = &quot;<a href="mailto:tgolden@web30.webfaction.com" target="_blank">tgolden@web30.webfaction.com</a>&quot;<br>

PIPE = subprocess.PIPE<br><br>p = subprocess.Popen ([PLINK, REMOTE_USER, &quot;ls&quot;], stdout=PIPE)<br>stdout, stderr = p.communicate ()<br>print &quot;#1:&quot;, stdout.splitlines ()[0]<br><br>with open (&quot;out.txt&quot;, &quot;w&quot;) as f:<br>

 p = subprocess.Popen ([PLINK, REMOTE_USER, &quot;cat .bashrc&quot;], stdout=f)<br> p.communicate ()<br>print &quot;#2:&quot;, open (&quot;out.txt&quot;).read ().splitlines ()[0]<br><br>p = subprocess.Popen ([PLINK, REMOTE_USER], stdin=PIPE, stdout=PIPE)<br>

stdout, stderr = p.communicate (&quot;ls\nexit\n&quot;)<br>print &quot;#3&quot;, stdout<br><br>p = subprocess.Popen ([PLINK, REMOTE_USER], stdin=PIPE, stdout=PIPE)<br>p.stdin.write (&quot;ls\nexit\n&quot;)<br>stdout, stderr = p.communicate ()<br>

print &quot;#4&quot;, stdout<br><br>&lt;/code&gt;<br><br>A few things to note, none of which I believe to be germane to the<br>issues you&#39;re experiencing:<br><br>* You almost never need to use shell=True on a Windows call to subprocess.<br>

 If in doubt, don&#39;t use it.<br><br>* Definitely better to pass the list-of-params style as the first param<br> of subprocess.Popen; it sorts out issues with embedded spaces etc.<br><br>* The open (&quot;...&quot;, &quot;w&quot;) in your second example *may* be closing the<br>

 file immediately. I doubt it, since you&#39;d expect Popen to hold a<br> reference, but I haven&#39;t checked the implementation.<br><br>TJG 
<div>

<div><br>_______________________________________________<br>Tutor maillist  -  <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>To unsubscribe or change subscription options:<br><a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>

</div></div></blockquote></div><br>
</div></div><br>_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
<br></blockquote></div><br>