Select Page

Merging SSH connectivity and database access – about 10 minutes

We already have some amazingly useful functionality. Now we can start to tie things together. We have the code to connect to an SSH host and execute a simple command. We also have the code to save and retrieve our device information with a database. Now we can use our mad Python skillz to loop through the database entries and execute a command on every single device in there. This is starting to sound like our nefarious plan to have our sidekick go through all of our network devices and save configurations. Now we’re getting somewhere really useful!

In this iteration we are going to add some functions to our program so that we can start to organize all of this program code. When we began our project here we had just a little bit of program code. Now we’re starting to have more code so it seems like a reasonable time to start do a little cleanup in preparation for adding more code. Here’s what our next iteration looks like:


The functions are declared using Python’s def keyword. (The Python documentation provides lots of details if you want to dig in deeply.) A key point is to remember to indent your code for the function. So, in this iteration of the project we have created three functions (and we tried to name them extremely descriptively):

  1. setup_database(): This function contains the Python code to create our database plus add some data. The body of the function is the same database access code we have used in previous project iterations.
  2. read_from_all_devices(): This function makes use of the database to find our list of devices and then it goes through each device, connects to the device, gets the output of a command, and prints the results.
  3. main(): This is where our program is going to begin executing.

Now that we have moved our code into functions, Python will not execute these functions unless we tell it do so. We get the whole main() ball rolling by having added the function call of main() at the very bottom of the program. When Python encounters this statement it will begin to execute the code that we defined in the body of the main() function. Functions are a way to group together a bunch of statements into one bundle and keep them together, but separate from the rest of the program.

Our new function simply calls two other functions to get the real work done. When we call setup_database() Python branches to the code in the function and executes the code just as it executed the code before we created the function.

Next our main() function will execute our read_from_all_devices(). This function is doing all the same networking that we had developed in previous iterations of the program. Notice that we to create a new conn within the function. Each function has its own variables that are limited to their scope, meaning the variables can be used only within the function. The scoping, or isolation, of variables can make your program’s code safer (because you don’t accidentally overwrite a variable), but it can also make things more complicated when you want to share data among functions.

The read_from_all_devices() function also does a little extra credit work by parsing the data we have received back from the SSH host. Note that it stores the results of channel.recv() into a variable named results. Now the variable holds the data that came back from the remote server.

Share This