The Python module Paramiko enables you to leverage Python to automate the management of CISCO routers and switches. In this guide, I will explain how to install Paramiko via pip and provide a usage example. The module makes Python act as an SSH client or server, enabling us to automate any device to which we can connect via SSH.

Installing Python ^

We will start by installing Python on your PC, server, MAC, or Linux computer. Navigate to The latest version, as of this writing, is 3.10.4. Click the download link for your OS, and then kick off the installer.

Python installation options

Python installation options

A couple of items to note on the first installation screen:

Add Python to path—Native Python programs can be launched from the Windows command line. Without Python in the path, the OS will have no idea where Python is installed and will return an error. Select the checkbox.

Customize the installation—By default, Python installs in C:\Users\<user>\AppData\Local\Programs\Python\Python<versionnumber>. I personally find this an odd place to install software. If you choose the customize option, you can place it elsewhere.

I would also note that Python will not uninstall a previous version or prompt you that you have a previous version. It will simply create the folder structure and install the software. Unless you are testing multiple versions, I recommend that you use the Programs and Features option in Windows or the Linux package manager to remove old versions. Otherwise, Python will use the first version it finds in the Path when you launch it. If you do wish to run multiple versions, you will need to specify which version by specifying the path to the Python executable when you call it.

python --version and press enter. If everything is installed properly, we should see the version we installed displayed.

Displaying the Python version

Displaying the Python version

Installing Paramiko with pip ^

Now that we have Python installed, we must add the Paramiko module to Python. Python uses the concept of modules or packages to add functionality to the basic Python language. There are hundreds, if not thousands, of modules for Python to accomplish various programming tasks. We install packages or modules with the Python package installer, pip. The syntax is as follows:

pip install paramiko
Installing the Paramiko module with pip

Installing the Paramiko module with pip

Modules often depend on other modules or other programs to be loaded to support the module. pip will install any dependent modules or programs as needed.

Successful installation of Paramiko

Successful installation of Paramiko

Working with IDLE ^

To write Python scripts, we need a text editor. However, to make our lives easier, it is better to use an integrated development environment (IDE). An IDE helps us write and run code by highlighting errors in our scripts, giving us suggestions, and pointing out issues before we attempt to run the scripts. There are many different IDEs available, and most of them are free to download and use. The Python installation includes an IDE called IDLE.

IDLE is an acronym for integrated development and learning environment. IDLE allows you to experiment interactively with commands without having to write and save the code. This can come in handy when learning how to manage CISCO devices with Paramiko.

IDLE launches into interactive mode, as seen by the >>> prompt.

Initial IDLE screen in interactive mode

Initial IDLE screen in interactive mode

In this mode, we can type commands and see the results:

IDLE output from running an interactive command

IDLE output from running an interactive command

To start creating a script, select File > New File. A new window opens where we can write our code.

Result of opening a new file to create a script

Result of opening a new file to create a script

Paramiko example ^

Below is an example script to connect to a CISCO device and issue the show clock command. We can send any command we can type at a console prompt, as if we are sitting at the device. I chose the show clock command as a simple example. You can cut and paste this example into IDLE, edit it for your environment, and test it out.

# script to connect to a Cisco device and run commands
# by John Kull
import paramiko 
host1 = ''
user = 'admin'
secret = 'P@ssword'
port = 22
ssh = paramiko.SSHClient()
ssh.connect(hostname=host1, username=user, password=secret, port=port)
stdin, stdout, stderr = ssh.exec_command('show clock')
list = stdout.readlines()

Below is an explanation of what each line does:

Lines 1 and 2 are comments. In Python, any line that starts with # is a comment. Comments help us remember what a line does and are also helpful if you share your code with others.

Line 3 imports the Paramiko module for use in our program. Modules enhance the functionality of Python, but when we write code, we must tell Python we want to use the module in our script. The import command lets Python know we will use the Paramiko module in our script.

Lines 4–7 assign variables to the host, username, password, and port we wish to use for our connection. You can edit these lines for the device you wish to connect to.

Line 8 sets up a variable to our "instance" of Paramiko.

Line 9 sets a policy so that when we connect to a new device the first time, we will not stop the program because the SSH key is unknown.

Line 10 makes the actual connection to the device using the variable names assigned above.

Line 11 sends the show clock command to my CISCO router.

Line 15 takes the output from stdout, uses the read lines method, and assigns it to the variable named list.

The final line prints the response from the router back to the screen by printing the value of list. The screenshot below shows the result of running the script. From this point, we can use additional commands to manipulate or save the data. We'll have a look at a more advanced case in a future article.

Result of running the code above

Result of running the code above

Conclusion ^

This article looked at how to install Python and the Paramiko module. We looked at an example of how you can use Python to connect to a CISCO device or other devices that support SSH, send commands, and receive output back from the device.

Subscribe to 4sysops newsletter!

In my next post I will explain how you can use Python for Backup Cisco config.


Leave a reply

Your email address will not be published.


© 4sysops 2006 - 2022


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account