Monday, June 11, 2012

What is the simplest way to SSH using Python?


What is the absolute simplest way to SSH to a remote server from a local Python (3.0) script, supply a login/password, execute a command and print the output to the Python console? I would rather not use any large external library and not install anything on the remote server.



Source: Tips4all

6 comments:

  1. I haven't tried it, but this ssh module might help, which in turn uses paramiko. I believe everything is client-side.

    EDIT:

    Ostensibly, the link to the code accompanying the above blog post is broken, and looking through the blog's comments it seems it has made its way to a proper Python package, pysftp.

    ReplyDelete
  2. You can code it yourself using Paramiko, as suggested above. Alternatively, you can look into Fabric, a python application for doing all the things you asked about:


    Fabric is a Python library and
    command-line tool designed to
    streamline deploying applications or
    performing system administration tasks
    via the SSH protocol. It provides
    tools for running arbitrary shell
    commands (either as a normal login
    user, or via sudo), uploading and
    downloading files, and so forth.


    I think this fits your needs. It is also not a large library and requires no server installation, although it does have dependencies on paramiko and pycrypt that require installation on the client.

    The app used to be here. It can now be found here.

    * The official, canonical repository is git.fabfile.org
    * The official Github mirror is GitHub/bitprophet/fabric


    There are several good articles on it, though you should be careful because it has changed in the last six months:

    Deploying Django with Fabric

    Tools of the Modern Python Hacker: Virtualenv, Fabric and Pip

    Simple & Easy Deployment with Fabric and Virtualenv

    ReplyDelete
  3. If you want to avoid any extra modules, you can use the subprocess module to run

    ssh [host] [command]


    and capture the output.

    Try something like:

    process = subprocess.Popen("ssh example.com ls", shell=True,
    stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output,stderr = process.communicate()
    status = process.poll()
    print output


    To deal with usernames and passwords, you can use subprocess to interact with the ssh process, or you could install a public key on the server to avoid the password prompt.

    ReplyDelete
  4. Your definition of "simplest" is important here - simple code means using a module (though "large external library" is an exaggeration).

    I believe the most up-to-date (actively developed) module is paramiko. It comes with demo scripts in the download, and has detailed online API documentation. You could also try PxSSH, which is contained in pexpect. There's a short sample along with the documentation at the first link.

    Again with respect to simplicity, note that good error-detection is always going to make your code look more complex, but you should be able to reuse a lot of code from the sample scripts then forget about it.

    ReplyDelete
  5. I have written Python bindings for libssh2. Libssh2 is a client-side library implementing the SSH2 protocol.

    import socket
    import libssh2

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(('exmaple.com', 22))

    session = libssh2.Session()
    session.startup(sock)
    session.userauth_password('john', '******')

    channel = session.channel()
    channel.execute('ls -l')

    print channel.read(1024)

    ReplyDelete
  6. Like hughdbrown, I like Fabric. Please notice that while it implement its own declarative scripting (for making deploys and the such) it can also be imported as a Python module and used on your programs without having to write a Fabric script.

    Fabric has a new maintainer and is in the process of being rewriten; that means that most tutorials you'll (currently) find on the web will not work with the current version. Also, Google still shows the old Fabric page as the first result.

    For up to date documentation you can check: http://docs.fabfile.org

    ReplyDelete