[Tutor] Obtaining result from a sendline

eryksun eryksun at gmail.com
Wed Oct 31 06:44:01 CET 2012


On Tue, Oct 30, 2012 at 9:47 PM, Dave Wilder <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)


More information about the Tutor mailing list