Paramiko Manual Installation and Simple Code

 PARAMIKO


Table of content:

  • Introduction
  • Installation of Paramiko without python-pip
  • Coding a simple script to run a command
  • Key Notes
  • Conclusion


Introduction

Paramiko is the official python Library for SSHv2. It is coded purely in python. It provide both client and server side functionality. We can run command line script on the client to execute on the host machine. To learn more about Paramiko, I recommend reading the official documentation.

Installation of Paramiko without using python-pip

python-pip or pip is is a package management system written in python. Pip allows us to install python packages directly from the terminal. 

I am going to install Paramiko directly from the source code. First lets download the source code from GitHub using git.

git clone https://github.com/paramiko/paramiko.git

This will download Paramiko locally. 

Next, change directory to paramiko source code where you will find setup.py

then run:

sudo python setup.py install

This will install Paramiko as a package in locally.


To see if it has been install, we run

pip list

this will lists all the packages install locally. Then we can look for Paramiko since the list is ordered alphabetically.


Uploading: 168808 of 168808 bytes uploaded.

Since it has been installed, we can how start to start some simple coding.

Coding a simple script to run a command

To write the code, I am going to use Visual Studio Code. You can use any editor you want. 

First create a python file. I have named mine TestParamiko.py.

Lets import paramiko

import paramiko

Then let's set up and SSH Client.

client = paramiko.SSHClient()

Since we are running it for a first time, we need to set a missing host key policy to enable it to connect as it does not have our host's key. Then we use a build-in function to automatically add a policy.

client.set_missing_host_key_policy(paramiko.AutoAddPolicy)


Now we can connect to the host using the function connect().

client.connect(username=username, password=passwd, hostname='192.168.100.67')

set your own username, password and IP.

To run a command, we use the function exec_command()

 stdin, stdout, stderr = client.exec_command(command)

Since exec_command() returns tuples, input, output, error in order, we we use python's tuple to store it on one line.

To read the output and error, we need to read it then decode the bytes.

print(stdout.read().decode())
print(stderr.read().decode())

To close the client, we use function close()

client.close()


Full code with some tweek:

import paramiko
import getpass
 
command = """
uname -a
"""
 
username = input("Enter username: ")
passwd = getpass.getpass("Enter Password: ")
 
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
client.connect(username=username, password=passwd, hostname='192.168.100.67')
 
stdin, stdout, stderr = client.exec_command(command)
print(stdout.read().decode())
print(stderr.read().decode())
 
client.close
()

I imported getpass to enable we to enter the password safely. It enables me not display the password when entering it.

The variable command is a string block.

The output of the command "uname -a" is



Some Key Notes

When calling the exec_command(), this function creates it own instance of shell. If two exec_command() is called or the function is put in a loop, each command is independent of each other. For example, 

lets say that the initial directory is : /home/user

then let's execute a command, exec_command("cd Desktop")

then re-run an exec_command again but this time exec_command("pwd")

we would get the /home/user instead of the expected result /home/user/Desktop.


Conclusion

Paramiko is a SSHv2 protocol made entirely in python.  I have set up an python script to run a simple command on the host machine. 


Comments

Popular Posts