[Tutor] Obtaining result from a sendline

Dave Wilder D.Wilder at F5.com
Wed Oct 31 14:28:36 CET 2012


Sorry for the poor  info on my part Steven et al.  I did not do my homework.



I thought that sshclient was an imported module, however it is pxssh that I am importing as sshClient.

import pxssh as sshClient



There is no particular reason I need to debug this using the debugger to troubleshoot this.  I was just using it to step through the program at the time.



Thank you eryksun for the help…especially the quick response and great explanation. Both of your suggestions will  work for me, particularly the 2nd  one.



I am not actually using “str” for the reasons you described.  I just renamed it when I emailed my question, which was probably not a good idea.



Thanks,



Dave



-----Original Message-----
From: eryksun [mailto:eryksun at gmail.com]
Sent: Wednesday, October 31, 2012 1:44 AM
To: Dave Wilder
Cc: tutor at python.org
Subject: Re: [Tutor] Obtaining result from a sendline



On Tue, Oct 30, 2012 at 9:47 PM, Dave Wilder <D.Wilder at f5.com<mailto:D.Wilder at f5.com>> wrote:

>

> However, all I get back is a numerical value.  I am looking to get the

> actual output from the “ls /var/tmp”



That would be the number of bytes sent.



> ssh = sshClient.pxssh()

> ssh.login(server=ip_addr, username=user, password=pswd) str =

> ssh.sendline('ls /var/tmp')



It's not a good idea to shadow the name of the built-in type str.



The pxssh object should have the methods read, read_nonblocking, readline, and readlines. But you probably want the output from between prompts. The prompt() method calls expect() to match the custom prompt, and returns False if there's a timeout (the default is 20 seconds). This sets the instance attribute "before" with the text that came before the match:



    ssh.sendline('ls /var/tmp')

    if ssh.prompt():

        output = ssh.before



Alternatively, if you've setup a public key on the host via ssh-keygen, you could use subprocess.check_output() instead:



    import subprocess



    cmd = ['ssh', '-l', login, hostname, 'ls /var/tmp']

    output = subprocess.check_output(cmd)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121031/0c7f26e1/attachment.html>


More information about the Tutor mailing list