#

Wednesday, August 27, 2025

Private Memberships

We can have Private Attributes and Private Methods inside a Class which should be accessed from within the Class only. It is just something Python community use, not something enforced. It is just an  These can be seen mostly inside Modules where the authors use the "_" at the beginning of the attribute / method indicating, don't try to call it from outside the Class which may break the functionality.

Here in this example, you can see that "_domains" is a private attribute. There can be private methods as well which starts with "_". 

However you can modify it if you want, when you call it from somewhere else with using "_" Infront of the attribute / method.
 
Property Decorator with Getters & Setters

A decorator in Python is a function that takes another function as its argument, and returns another function. Property Decorator in Python is a built in decorator 

In this example, we can see _name and _id are private attributes. 

As an example, 1st Syntax @property (line 6) and the method under it (line 7-8) is just coded to return the value of "_name" when just "name" is called from outside the class or anywhere in the program. If this method is not created, the value of _name can be outputted only by calling "R1._name". Because of this method, it is possible to call the same with "R1.name()". But to me more Pythonic, we can use Property Decorator (which is what we have done here with @property keyword), we can call just like a natural attribute "R1.name".



This @property is called a "Getter" as it is used to get values.





Setters can be used to set another value to an attribute, based on a condition.

Line 14 starts a "Setter" which sets a new value (line 22) later given in the program to the original value ( line 20) if the condition on line 16 matches else it will pop up the error message.

Saturday, August 16, 2025

Object Oriented Programming (OOP) helps group objects based on classes. These classes consist of Properties (Attributes) and Behaviours (Methods) which are inherited to the objects we create from that classes. So these classes act as blueprints of creating objects.

Instances are nothing but the classes that hold the data and can perform the behaviours.

Following is a code of a class.








Above code is a Class named "Routers" and working_layer is a Class Attribute.
"brand" and "OS" are Instance Attributes.
"description" is a Method, specifically an Instance Method which is the most common method we see.

Following is an example of creating an object.

Since there are 2 Instance attributes defined within the class, we have to pass values for those 2 attributes like arguments.

Here an object named "R1" is created based on the class "Routers" and brand and OS is mentioned when creating the object. Later they were retrieved using <object name>dot operator<attribute>.


Another object is created from the same "Routers" class giving it's Instance Attributes.

Since the "working_layer" is a class attribute, both R1 and R2 shares the same info while the other info is different.




If we call the Method, the out put will be like the following, 


Wednesday, July 2, 2025

Following is the official link of IPAddress library of Python.

https://docs.python.org/3/library/ipaddress.html
 
Let's install IPAddress library by installing it in Ubuntu which is my dev environment.

python3 -m pip install ipaddress

To see what it can do, I am using iPython in a Venv

To know how to use iPython in a Venv, go to end of the following post.


Now let's start by importing ipaddress library into iPython. Following example shows how the functions of IPAddress library can be used to get all the hosts in a given subnet, find the subnet mask and wild card masks which are very handy in writing Networking related scripts.
















If you want to define a variable with an IP address,

In this example, the given IP address is assigned to my_ip variable and then some functions had run to check details of it like to check it is a private IP or not multicast IP or not.




Following is just an example of using IPAddress library which checks whether 2 IPs are in the same subnet or not.

ip_network function calculates the network id of the given IP and store in a variable to do this.











Monday, June 30, 2025

When retrieving show command outputs, both Scrapli and NAPALM libraries can do pretty much the same thing but Scapli is sending the exact command where NAPALM is using a common command to get the same type of information from different platforms. As an example, NAPALM getter like "get_bgp_neighbors" will retrieve the bgp neighbor information from Cisco, Juniper or any other platform which NAPALM supports.

Following table are the platforms which NAPALM supports at the moment.


First we have to install NAPALM library in our environment, mine is Ubuntu.

python3 -m pip install napalm

Following code gets the device details of the device and print it with the default format.





Part of the output will be something like the following..





Converting Results to a Structured Data Format

For this we need to import the "rich" library. If your system does not have it yet, it can be installed by the following commands.

pip install --user rich
pip install rich

Now by using the following additions to the code will provide a more readable output.





It is just importing the rich library and the result is printed as "rprint" instead of normal "print".

If you need to get a specific information our from the above output, it can be done like in the following.


Above outputs the serial number of the device like the following.




If there are multiple devices in a list it's just modifying the list "DEVICES" like the following.




Wednesday, June 25, 2025

Scrapli is a widely used library in Python for network related work. In this post let's see how it can be used to do very simple taks. Following link gives more information on Scrapli as it is the official site.

https://carlmontanari.github.io/scrapli/

First of all installing Scrapli..

python3 -m pip install scrapli

Following is the router I used to access in EVE-NG and the IP address of E0/0 interface is 192.168.1.13


Note that interface is bridged to the physical port of the VM and it is in the same LAN as my Devnet PC (Ubuntu)
















I had to change the ssh_config file of Ubuntu to match the older Ciphers like the following and auth_secondary which is the enable password needed to be configured in order to access the devices in EVE-NG.

Following white colored text were the text I needed to add.




















Following was the output..





Converting Results to a Structured Data Format

This can be using one of the following solutions.

1. Text fsm
2. Genie

I will perform the following example by Textfsm. For that we need to install Textfsm templates to Scrapli library.

use following command in cli;

python3 -m pip install scrapli[textfsm]

Following code will get the "show version" output to the "JSON" structured format.


Output:-

















Configuring Devices with Scrapli

Following example will configure ospf process and ospf router id using Scrapli.














When this code is run, configured commands will be shown as the output as per the last line also.
Note that the commands are sent as a list and will be configured as line by line and no need to send "configure terminal" command as it will be taken care by the library.






If the Config commands are in a file;















Output will show the commands which were in the file;