Visualizing Python Module for Network Libraries (Netmiko and PyEZ)

Ever wondering How a python custom module or class is manufactured ? How does the developer write the python code and glue it together to create this nice and amazing “x” module ? What’s going on under-the-hood?

Documentation is good start of course, but we all know that it’s not usually updated with every new step or detail that developer add.

For Example,

we all know the powerful netmiko library created and maintained by Kirk Byers that utilize another popular SSH library called Paramiko. but we don’t understand the details and how the classes are connected together. we just write the below code  to execute a specific command in Cisco IOS platform

from netmiko import ConnectHandler

device = {"device_type":"cisco_ios",
"ip":"10.10.99.110",
"username":"admin",
"password":"access123"}

 

net_connect = ConnectHandler(**device)
output = net_connect.send_command("show arp")

and booom, it’s working like charm

if you need to understand the magic behind the “self.charm” that netmiko use to return the result,  Please follow the below steps (require Pycharm IDE)

Netmiko Module

Step1:

Open the Netmiko location inside the python library location folder (usually C:\Python27\Lib\site-packages) in Pycharm IDE

clip_image001

Step 2:

Right click on python module and choose Diagrams then Show Diagram

clip_image002

it will take some time to generate the diagram based on Your Java xms settings ( usually I assign 1024MB for it to work properly)

Step 3:

Now save the resulting  image into your desktop

clip_image003

 

And Here’s it (click on it to enlarge and zoom as much as you can)

netmiko

Understand the result UML graph

Based on the resulting graph you can see that Netmiko is supporting a lot of vendors  like HP Comware, entrasys , Cisco ASA, Force10, Arista, Avaya,..etc and all of these classes are are inheriting from parent netmiko.cisco_base_connection.CicsoSSHConnection class (I think because they use the same SSH style as Cisco) which in turn inherit from another Big parent Class called netmiko.cisco_base_connection.BaseConnection

Also you can see that Juniper has it’s own class (netmiko.juniper.juniper_ssh.JuniperSSH) that connect directly to the Big parent

And finally we connect to the parent of all parents in python. The Object class (remember everything in Python is an object at the end)

Also you can find a lot of interesting things like SCP transfer class, SNMP class and with each one your will find the methods and parameters used to initialize this class

So the ConnectHandler method is primarily  used to check the device_type availability in above vendor classes and based on it , it will use the corresponding SSH Class. A well designed module really, Thanks Kirk Byers !

image

image

Juniper PyEZ Module

Applying the same procedure in Juniper Python wrapper (PyEZ) used to connect to JunOS based platforms. we can get another useful diagram as below (Again click to enlarge)

image

Also you can see the support for the netconf, serial and telnet connection and Views and Tables classes that Juniper use to define the facts about the device. Very clever.

That’s all. Please share any other interesting findings in python modules

I hope this has been informative for your and I’d like to thank you for reading.

Advertisements

Share you opinion to benefit others :)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s