High-level ssh client library

Registered by Sergey Skripnick

There is no common ssh client library. Different projects use own functions, or ssh function from processutils, which is not flexible enough.

Paramiko is not the answer, because it too low-level. For example:

 import paramiko
 ssh = paramiko.SSHClient()
 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 ssh.connect('host', username='usef')
 ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('sh /root/outerr.sh')
 print ssh_stdout.read()
 print ssh_stderr.read()

In example above, if there is data on stderr which do not fit in buffer then we got deadlock on stdout reading.

If we want to receive stdout, stderr and exit status, we should write even more code.

Working with this library may look like this:

    ssh = sshclient.SSH('root', 'example.com', port=33)
    status, stdout, stderr = ssh.execute('ps ax')

Execute command with huge output:

    ssh = sshclient.SSH('root', 'example.com')
    def stdout_callback(data):
        LOG.debug(data)
    ssh.run('tail -f /var/log/syslog', stdout_callback=stdout_callback, timeout=False)

Execute local script on remote side:

    ssh = sshclient.SSH('user', 'example.com')
    status, out, err = ssh.execute('/bin/sh -e', stdin=open('~/myscript.sh', 'r'))

Potential library consumers:

ironic
quantum
savanna
tempest
heat

cinder uses own advanced ssh library
nova uses command line ssh

Blueprint information

Status:
Started
Approver:
None
Priority:
Undefined
Drafter:
Sergey Skripnick
Direction:
Needs approval
Assignee:
None
Definition:
Discussion
Series goal:
None
Implementation:
Good progress
Milestone target:
None
Started by
Sergey Skripnick

Related branches

Sprints

Whiteboard

Why not just use paramiko? --jogo
Paramiko is too low level. A bunch of code needed for simply get stdout from command. --sergey
Can you add more details to this BP to clarify what you mean -- jogo
Done. --sergey

Why not fix paramiko? -- lifeless
It is not a bug in paramiko, paramiko just low level. -- sergey

I have to agree with lifeless. Have you asked the Paramiko maintainers if they would be willing to accept a patch for a higher-level API such as this? -- dhellmann

Addressed by: https://review.openstack.org/64054
    High level ssh library

Gerrit topic: https://review.openstack.org/#q,topic:bp/common-ssh-client,n,z

Consider using Spur.py library (http://mike.zwobble.org/2013/02/spur-py-a-simplified-interface-for-ssh-and-subprocess-in-python/) -- mkulkin

spur looks like another reasonable candidate. I've removed this from the icehouse-2 series until we agree on the approach. Sergey, please start a mailing list thread evaluating the alternatives (it's really a pain to communicate through the whiteboard on the blueprint :-). - dhellmann

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.