Python 3 SSH with Paramiko

I have a number of projects which I am currently working on which usually involve a raspberry pi and so other TCP enabled object. One project, in particular, is to control a KVM server with buttons connected to a raspberry pi (follow on social media or RSS for that future post). I have been using Python 3 SSH with Paramiko to send commands KVM server from the pi. Here is how I do that…

Python 3 SSH with Paramiko - exitcode0

What is Paramiko?

Paramiko is a Python (2.7, 3.4+) implementation of the SSHv2 protocol, providing both client and server functionality. While it leverages a Python C extension for low level cryptography, Paramiko itself is a pure Python interface around SSH networking concepts.http://www.paramiko.org/

TLDR – Paramiko is a python library which lets you do SSH things.

The how-to: Python 3 SSH with Paramiko

For the purpose of this blog, I will be focusing on using paramiko to make client connections. Let’s include this library and dig into sending some dangerous commands to Linux boxes.

import paramiko

I have been using a function to complete my SSH command and passing my SSH variables when calling that function. Lets start with that funciton:

def func_do_ssh_Stuff(address, usr, pwd, command):
        try:
            print("ssh " + usr + "@" + address + ", running : " +
                         command)
            client = paramiko.SSHClient()
            client.load_system_host_keys() # this loads any local ssh keys
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

            client.connect(address, username=usr, password=pwd)
            _, ss_stdout, ss_stderr = client.exec_command(command)
            r_out, r_err = ss_stdout.readlines(), ss_stderr.read()
            print(r_err)
            if len(r_err) > 5:
                print(r_err)
            else:
                print(r_out)
            client.close()
        except IOError:
            print(".. host " + address + " is not up")
            return "host not up", "host not up"

What’s going on here? The function is expecting to be handed a username, password (or none if you plan to use key based authentication (recommended)), host address and the shell command to run once the connection is established.

Paramiko checks for local keys on the system which it can use when connecting to the host, then attempts a connection. Once the connection is established the command we passed to the function will be executed.

Finally, we print the output or error returned by the execution of that command. This is all encased in a ‘try, catch’ which will inform us if the SSH target is offline.

Calling the Paramiko SSH function

Now that we have our SSH function declared, we can call it:

func_do_ssh_Stuff("192.168.0.33", "root", "", "echo Hello World!")

Note: you will need to change the IP to suit and add a password (“”) if required.

I would highly recommend using key-based authentication on this one, otherwise, it will require you to hard code SSH passwords into your scripts… not a great idea.

And now that you can SSH from a Pi (or anything else which runs python), the world is your oyster!


More wonderful Python things:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: