<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,'/usr/bin/sudo /usr/sbin/tshark <strong>&</strong>', '-a', 'duration:10', '-i', 'wlan0', '-T', 'text', '-V','>', 'out.txt'];</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,'/usr/bin/nohup','/usr/bin/sudo /usr/sbin/tshark <strong>> out.txt 2> out.err < /dev/null</strong> ', '-a', 'duration:10', '-i', 'wlan0', '-T', 'text','&'];</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"><<a href="mailto:mibaker88@gmail.com">mibaker88@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>Thanks Tim,</div>
<div> </div>
<div>Your subprocess examples got me started in the right direction. I'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't execute additional Python commands until the Tshark capture finishes.</div>
<div> </div>
<div>I'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>>>> test_subp(alt_cmd=1)</div>
<div>then the nohup command is added to the subprocess command list (along with a trailing '&' 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> '''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> '''</div>
<div> PLINK = 'C:\\Progra~1\\putty\\plink'<br> sess_name='LabComp1'<br> if alt_cmd:<br> '''This command does not work as expected. The tshark output file (out.txt)is created,<br>
but there is nothing in it '''<br> CMD_LIST=[PLINK,sess_name,'/usr/bin/nohup','/usr/bin/sudo /usr/sbin/tshark', '-a', 'duration:10', '-i', 'wlan0', '-T', 'text', '-V','>', 'out.txt','&'];<br>
else:<br> 'This command works great, writing tshark output to out.txt on the remote machine.'<br> 'Unfortunately, this command hangs the putty session until the tshark capture ends'<br> CMD_LIST=[PLINK,sess_name,'/usr/bin/sudo /usr/sbin/tshark', '-a', 'duration:10', '-i', 'wlan0', '-T', 'text', '-V','>', 'out.txt'];<br>
print "The command list you are sending to the subprocess is: \n", "\t", CMD_LIST<br> <br> PIPE = subprocess.PIPE<br> p = subprocess.Popen(CMD_LIST, stdout=PIPE, stderr=PIPE)<br> stdout, stderr = p.communicate ()<br>
print 'stdout = ', stdout<br> print 'stderr = ', 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"><<a href="mailto:mail@timgolden.me.uk" target="_blank">mail@timgolden.me.uk</a>></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'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><code><br>import os, sys<br>import subprocess<br><br>PLINK = "plink"<br>REMOTE_USER = "<a href="mailto:tgolden@web30.webfaction.com" target="_blank">tgolden@web30.webfaction.com</a>"<br>
PIPE = subprocess.PIPE<br><br>p = subprocess.Popen ([PLINK, REMOTE_USER, "ls"], stdout=PIPE)<br>stdout, stderr = p.communicate ()<br>print "#1:", stdout.splitlines ()[0]<br><br>with open ("out.txt", "w") as f:<br>
p = subprocess.Popen ([PLINK, REMOTE_USER, "cat .bashrc"], stdout=f)<br> p.communicate ()<br>print "#2:", open ("out.txt").read ().splitlines ()[0]<br><br>p = subprocess.Popen ([PLINK, REMOTE_USER], stdin=PIPE, stdout=PIPE)<br>
stdout, stderr = p.communicate ("ls\nexit\n")<br>print "#3", stdout<br><br>p = subprocess.Popen ([PLINK, REMOTE_USER], stdin=PIPE, stdout=PIPE)<br>p.stdin.write ("ls\nexit\n")<br>stdout, stderr = p.communicate ()<br>
print "#4", stdout<br><br></code><br><br>A few things to note, none of which I believe to be germane to the<br>issues you're experiencing:<br><br>* You almost never need to use shell=True on a Windows call to subprocess.<br>
If in doubt, don'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 ("...", "w") in your second example *may* be closing the<br>
file immediately. I doubt it, since you'd expect Popen to hold a<br> reference, but I haven'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>