Windows, Paramiko and Python

This tutorial covers scripting SSH access in Windows using Python and Paramiko. This can be used for a variety of tasks, including network automation and administration.

The following technologies are used:

Windows 10
Python 3
Paramiko

Python Installation

Go to https://www.python.org/downloads/ and download the latest version. At the time of writing it was 3.8.1. be sure to read below before rushing through the installation.

Once python has downloaded run the executable. Make sure you select Customize installation and select next. By default all Optional features are selected, nothing here requires changes. The important Advanced Options to be enabled are “Add Python to environment variables”.

By selecting “Add Python to environmental

Now select install.

Once installed you can confirm the installation with the selected environmental variables by opening a command prompt and typing python. You will see the below if successful.

To exit this screen, type exit()

Installing Paramiko

To begin we need to update the python package manager. This is called pip. To do this open a command prompt and type the below:


python -m pip install –upgrade pip

Once complete we will install paramiko, to do this type the following command:

python -m pip install paramiko

Using Paramiko

Below is a short Python script demonstrating a simple way to use Paramiko. In this example the Windows machine we previously installed Python and Paramiko on will SSH to a Ubuntu desktop, run the command “ifconfig” then print the output.

#This imports the python modules we want to use
import paramiko
import time

#This sets up our SSH session to the client
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname="192.168.93.128",username="thetutorialhub",password="password123")

#This creates a shell connection to the SSH session
ubuntu_desktop = ssh_client.invoke_shell()

#This clears the initial input buffer
#This is only useful if you want to display the output of your commands
ubuntu_desktop.recv(5000)

#This is the command you are sending over the SSH session
ubuntu_desktop.send("ifconfig\n")

#This is to allow enough time for the receive buffer to capture the output of the command
time.sleep(2)

#This is the command that assigns the output into a variable
ubuntu_output = ubuntu_desktop.recv(5000)

#This is the ocommand to print that output
print(ubuntu_output.decode('utf8'))

#This will close the SSH session
ubuntu_desktop.close()



This is the output of the script.

Some important notes:
If you are not concerned with capturing the output, you can remove the sleep, recv and print commands.

The number in brackets in commands such as ubuntu_desktop.recv(5000)
are to specify how many bytes you would like to read from the output. If you do not receive the full amount of output expected, try increasing the sleep time and recv amount.

You need to decode the output if you want it to be human readable, without the command .decode(utf8) the output would look like the below:

The decode statement reads interprets the statements such as \n to be a newline.

Leave a Reply