Getting started – about 10 minutes
We’ll start by writing a little program to that can test out whether we can successfully connect to an SSH host device. This will be a short program but it will lay the foundation upon which we will build more useful tools. So, let’s take a look at our first step:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import paramiko device_name = '10.62.1.254' username = 'testpix' password = 'testpix' ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ## to avoid missing_host_key error ssh.connect(device_name, username=username, password=password, allow_agent=False, look_for_keys=False) channel = ssh.invoke_shell() channel.send("?\n") results = channel.recv(1024).decode('ascii') print('Results: {0}'.format(results)) |
Since this is the foundation it is important to understand each element that we have worked into this short example.
1 |
import paramiko |
tells the Python interpreter that it should load the Paramiko library. When Python loads the library then you, as a programmer (you can start thinking of yourself as a programmer now!), can use the tools in the library to achieve your own goals. Loading the library gives us access to all of the features the Paramiko library provides.
Next we are assigning some basic connection info into variables so that we can reference them later.
1 |
device_name = '10.62.1.254' |
In most cases it is wise to assign hard coded values, like a device name, to variables. Then, if you need to make changes later you can make changes in one place. In this case we’re using the IP address as the name of our device. You can use a network name, if you like. For your own testing, you’ll need to update that IP address.
1 |
ssh = paramiko.SSHClient() |
There are a few different things that are happening in this statement. First, we are requesting a feature from the paramiko
library. The feature we are requesting is the SSHClient
. The full statement of paramiko.SSHClient()
creates an SSHClient
for you. This is a sort of raw “handle” through which you’ll be able to communicate with an SSH host. Which host? Ah, good question, but we haven’t told the SSHClient which host. Yet.
But before we get to that, let’s discuss the ssh
on the left side of the equal sign. When we created the SSHClient
we don’t have a way to refer to that client unless we save it somewhere. By assigning to ssh
we can use ssh
to refer to the SSHClient later in the program. You can say that ssh
is an instance of the SSHClient class.
1 |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
This cumbersome looking statement is simply telling our ssh
how to deal with missing host keys. If you do not add this code then Paramiko will terminate early and report a missing_host_key error. We want Paramiko to go ahead and just add the new host keys.
1 |
ssh.connect(device_name, username=username, password=password, allow_agent=False, look_for_keys=False) |
Now we’re getting down to the real meat of the program! This line is using our ssh
instance to connect to device_name
and log in using the provided credentials. If you provide set the device_name
to a netowrk name then the Paramiko library will resolve that to an IP address.
We are telling the ssh
instance that we want to execute the connect()
method with the provided parameters.
1 |
channel = ssh.invoke_shell() |
Once we have connected to the device we want to create a shell session on the device. The shell is basically just like having connected to the device using ssh from the Terminal prompt. Once you have a shell you can emulate typing commands into the device. Paramiko refers to the shell as a channel
so we chose a somewhat appropriate name.
1 |
channel.send("?\n") |
Next we use our ssh
instance to execute a command on the host. In this case we are executing a simple help command with ?
.
1 |
results = channel.recv(1024).decode('ascii') |
We make use of our channel
to get the output of the command from the host. In this case we use recv(1024)
to pull the results info, all at once. We’re saving the data into our results
variable. And then we simply print the results
to the screen:
1 |
print('Results: {0}'.format(results)) |
At this point it is not pretty. It is not flexible. We might still write home to Mom though because it is an actually functioning and moderately useful program. It is most useful as a platform that we can use to modify and develop into a solid utility program.
Recent Comments