Python Scripting For Network Engineer (Paramiko) Part-1

python-logo-master-v3-TM

Wake up on 5.00 AM GMT+7, i start thinking what should i do on this morning, better im playing Dota 2 šŸ˜› or Write Some article. and my heart say something sh*t like, used your time for something useful and be a good person with helping each other.

This article is my promise to you from previous article,when im talking about ansible, i was promise to you i will create a new session about what is paramiko? And example scripting used paramiko phyton to manage your network device

1. What is paramiko :

Paramiko is a Python (2.6+, 3.3+) implementation of the SSHv2 protocolĀ [1], providing both client and server functionality. While it leverages a Python C extension for low level cryptography (Cryptography), Paramiko itself is a pure Python interface around SSH networking concepts.

  • Paramiko is python interface around SSH networking
  • I will used it connect to Network Device or Linux System
  • After Create Connection you can execute any task with python script or run other bash script on linux system
  • More Information you can get at http://www.paramiko.org

2. How to install paramiko phyton

In this article i still used ubuntu 14.04 LTS to run my python script, on default ubuntu dont have paramiko phyton module on that system, we can check used python interface from linux terminal

Go to phyton interface with command

#python

and try to import module paramiko on python script, like example picture below

> import paramiko

Import paramiko

So we will install module paramiko python first to ubuntu system with command

#sudo apt-get update

#sudo apt-get install python-paramiko

On the picture above we can see, import module was error ā€œImportError : No module named paramikoā€

Install paramiko

After we success installed paramiko python on our system, next we try again to import module paramiko again to our python script, its should be success

Import paramiko Succcess

as you can see, we not get error message when we import paramiko on python script

3. Using paramiko on python scripting

Next we will create a simple basic python scripting used python paramiko to do a remote connection to network device, just test remote connection used python paramiko, so you will understandĀ  basic simple scripting using python paramiko. create file python script with command

#vim paramiko-login.py

And create python script like example on the below

============================================================

import paramiko <call module paramiko>
import time
import os
import getpass

terus_tanya = True
while terus_tanya: <<<<<<<<<<<<<<<looping function and conditions>
ip = raw_input(‘Masukkan IP Tujuan:’)
response = os.system(“ping -c 1 ” + ip)

if response == 0:
terus_tanya = False
print “Destination is UP”
else:
terus_tanya = True
print “Destination is Down”

username = raw_input(‘Masukkan Username Anda:’) <input string>
password = getpass.getpass(“Password: “) <save password used module getpass>
port = 22

/*Noted : on this script i used function raw_input phyton to make user give input value and save the value to username variable, because i dont want to definedĀ  username and showed the usename value on this script and used getpass function to password cridential, because i dont want when user input they password it will showed on command prompt*/

remote = paramiko.SSHClient()
remote.set_missing_host_key_policy(paramiko.AutoAddPolicy())
remote.connect(ip, username=username, password=password, look_for_keys=False, allow_agent=False)
print “you are login Sir”

/*above is script to called SSH function and do login session with SSH protocol*/

 

===============================================================

Script one right

Save python script login parramiko to a file

4. give grant permission on ubuntu system to execute that python script with command

#sudo chmod 111

Permission file

And check file permission with command

#ll or ls -l

Paramiko permission status

5. Now we can execute the python script with command

#python paramiko.py

on this i will do a remote session used python paramiko script from my ubuntu system on IP 192.168.98.155 to Security Device Palo Alto on IP 192.168.98.51 through SSH protocol, the result i show to you on the example picture below

Test Script

Example picture above show we success execute python scripting and success login to security device palo alto on ip 192.168.98.51

To make sure our python script success We must check on Palo Alto dashboard log system to get information IP ubuntu system ex (192.168.98.155) have success create SSH connection to palo alto and success do auhtentication admin

authentication paramiko on dashboard palio

From picture above its look like we success login to palo alto device used python paramiko. and this is a end of session article Python Paramiko Part 1

next section, we will create python script to get information on device after we success login to their system with paramiko python scripting, try to configure device, and many more, the last we will create simple application tools to manage our network device via paramiko python scripting

hope you enjoyed it, Thanks

 

Leave a comment