[Tutor] Remote access from Windows PC to a Linux box

Mike Baker mibaker88 at gmail.com
Wed Apr 21 22:32:22 CEST 2010


Yashwin,

Thanks!  Your nohup redirection worked great!

  - Mike

On Wed, Apr 21, 2010 at 2:45 PM, Yashwin Kanchan
<yashwinkanchan at gmail.com>wrote:

> Hi Mike
>
> have you tried running the tshark process in the background...
>
> *CMD_LIST=[PLINK,sess_name,'/usr/bin/sudo /usr/sbin/tshark &', '-a',
> 'duration:10', '-i', 'wlan0', '-T', 'text', '-V','>', 'out.txt'];*
>
> Or if you are using NOHUP try redirecting this way...
>
> *CMD_LIST=[PLINK,sess_name,'/usr/bin/nohup','/usr/bin/sudo
> /usr/sbin/tshark > out.txt 2> out.err < /dev/null ', '-a', 'duration:10',
> '-i', 'wlan0', '-T', 'text','&'];*
>
> Regards
> Yashwin Kanchan
>
>
> On 21 April 2010 19:15, Mike Baker <mibaker88 at gmail.com> wrote:
>
>> Thanks Tim,
>>
>> 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.
>>
>> 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.
>>
>> 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.
>>
>> 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
>> >>> test_subp(alt_cmd=1)
>> 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.
>>
>>
>> Here is my code:
>> **********************************************
>> def test_subp(alt_cmd=0):
>>     '''Establish a Putty connection with one of our Linux machines in the
>> lab.
>>     Send Tshark command to start a data collection session over Putty.
>>     '''
>>     PLINK = 'C:\\Progra~1\\putty\\plink'
>>     sess_name='LabComp1'
>>     if alt_cmd:
>>         '''This command does not work as expected.  The tshark output file
>> (out.txt)is created,
>>         but there is nothing in it '''
>>         CMD_LIST=[PLINK,sess_name,'/usr/bin/nohup','/usr/bin/sudo
>> /usr/sbin/tshark', '-a', 'duration:10', '-i',  'wlan0', '-T', 'text',
>> '-V','>', 'out.txt','&'];
>>     else:
>>         'This command works great, writing tshark output to out.txt on the
>> remote machine.'
>>         'Unfortunately, this command hangs the putty session until the
>> tshark capture ends'
>>         CMD_LIST=[PLINK,sess_name,'/usr/bin/sudo /usr/sbin/tshark', '-a',
>> 'duration:10', '-i',  'wlan0', '-T', 'text', '-V','>', 'out.txt'];
>>     print "The command list you are sending to the subprocess is: \n",
>> "\t", CMD_LIST
>>
>>     PIPE = subprocess.PIPE
>>     p = subprocess.Popen(CMD_LIST, stdout=PIPE, stderr=PIPE)
>>     stdout, stderr = p.communicate ()
>>     print 'stdout = ', stdout
>>     print 'stderr = ', stderr
>> *********************************************************************
>>
>> For both runs (atl_cmd=0 or alt_cmd=1), the stdout and stderr printouts at
>> the end of the script are empty.
>>
>> Any suggestions would be appreciated.
>>
>> Thanks,
>>
>>     Mike
>>
>> *******************************************************************************************************
>>
>>  On Wed, Mar 31, 2010 at 7:42 AM, Tim Golden <mail at timgolden.me.uk>wrote:
>>
>>> On 30/03/2010 17:29, Mike Baker wrote:
>>>
>>>> I'm trying to connect to a Linux box from my Windows machine and execute
>>>> a
>>>> series of commands
>>>>
>>>> I want a script to always
>>>> execute the same series of commands without having to do so manually.
>>>> I
>>>> also have code that will execute a single command like cat a file and
>>>> write
>>>> the ouput to a new file. However, when I try to use the communicate
>>>> object
>>>> in subprocess, my window hangs.
>>>>
>>>
>>>
>>> This works for me:
>>>
>>> <code>
>>> import os, sys
>>> import subprocess
>>>
>>> PLINK = "plink"
>>> REMOTE_USER = "tgolden at web30.webfaction.com"
>>> PIPE = subprocess.PIPE
>>>
>>> p = subprocess.Popen ([PLINK, REMOTE_USER, "ls"], stdout=PIPE)
>>> stdout, stderr = p.communicate ()
>>> print "#1:", stdout.splitlines ()[0]
>>>
>>> with open ("out.txt", "w") as f:
>>>  p = subprocess.Popen ([PLINK, REMOTE_USER, "cat .bashrc"], stdout=f)
>>>  p.communicate ()
>>> print "#2:", open ("out.txt").read ().splitlines ()[0]
>>>
>>> p = subprocess.Popen ([PLINK, REMOTE_USER], stdin=PIPE, stdout=PIPE)
>>> stdout, stderr = p.communicate ("ls\nexit\n")
>>> print "#3", stdout
>>>
>>> p = subprocess.Popen ([PLINK, REMOTE_USER], stdin=PIPE, stdout=PIPE)
>>> p.stdin.write ("ls\nexit\n")
>>> stdout, stderr = p.communicate ()
>>> print "#4", stdout
>>>
>>> </code>
>>>
>>> A few things to note, none of which I believe to be germane to the
>>> issues you're experiencing:
>>>
>>> * You almost never need to use shell=True on a Windows call to
>>> subprocess.
>>>  If in doubt, don't use it.
>>>
>>> * Definitely better to pass the list-of-params style as the first param
>>>  of subprocess.Popen; it sorts out issues with embedded spaces etc.
>>>
>>> * The open ("...", "w") in your second example *may* be closing the
>>>  file immediately. I doubt it, since you'd expect Popen to hold a
>>>  reference, but I haven't checked the implementation.
>>>
>>> TJG
>>>
>>> _______________________________________________
>>> Tutor maillist  -  Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100421/79b0bd8a/attachment.html>


More information about the Tutor mailing list