#

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;






Monday, May 5, 2025

Opening & Closing a Text File

Following code is the classic way of opening and closing a text file.

1st line opens a file which is in the current directory and the other line closes the file.


Since the above way always needs the closing statement to close the file, the better Pythonic way of doing this is to use a context manager. A context manager is a construct that properly manages the setup and teardown of resources like files, network connections, or locks using the with statement.

This way, when you exit from the indentation, the file is closed automatically.



Above code opens the file and save it in the memory as "f" and reads it and stores it in the variable called "output" and prints it out. Basically it prints the text file contents to the CLI.

Following is the sample.txt file in the working directory and let's run the above to see the output.







Output will be:





If we want to get the lines into a List,





Output will be:



Writing to a Text File

Following syntax will write a text to a file.

"w" implies that the file is opened to write, default it read only.


But this actually overwrites the file, so following will be the contents inside the file.





Instead we can append the text we want by opening the file with "a" append mode.





Output will be:
By proper spacing, using \n for line breaking, we can form the text much better.



Following example shows how we can write a file with List of data using f.writelines() method.






Output will be like the following..





Following sample code open 2 files and file and copy the contents of the source file to the destination file after manipulating the text. This is the advantage of copying it to memory. This example capitalizes the letters in the text.






Output will be:






Note:- Even though we need a file with the specified name already to work with reading, that is not the case for writing, It creates a new file if it does not exist.

Note:- 
If we want to read or write a file in another directory, we should give the path like the following.





Since forward slash (/) is used in Linux and back slash (\) is used in Windows, writing scripts to match the host OS can be problematic, Because of this, following piece of code helps to build the directory path according to the OS.







Since my one is Linux, output is:

Sunday, May 4, 2025

There are many types of programming languages. As an example, there are Procedural programming languages like Pascal, C, etc while there are Declarative languages like SQL and there are Object Oriented languages like Java. Python is a type of Functional programming language which does support Object Oriented Programming.

Comprehensions

This means to a concise way to create collections like Lists, Dictionaries, or Sets using a single line of code. Following example will explain what it is practically.

Following code captures the Gig interfaces in the traditional way..










Following code will give the same output and it is more Pythonic..







Single line covers it, there are actually 3 sections, let's break it down.








If we read the sections like the way I have numbered above, it is more understandable.

Note:- If we want to get the output in some modified way, as an example in block letters, we can use int.upper() to replace just int which is the expression.

Following example shows how Comprehension works for a Dictionary which outputs the routers which are up.


Lambda Function

A Lambda function in Python is a small, anonymous function defined using the lambda keyword. It's often used for short, simple operations and also called throw away functions after use.

Following example shows a Lambda function which adds 2 given values in single line of code.





Following is another one which compares a given number with 5 to check greater than or lesser.





Map Function

The map() function in Python applies a function to every item of an iterable (like a List) and returns a map object (which you can convert to a list, tuple, etc.).

Syntax is like the following..
map(function, iterable)

Note:- x**2 is the way to get square, double multiply..




Output will be;




Filter Function

The filter() function in Python is used to filter elements of an iterable (like a List) based on a function that returns True or False.

Syntax is like the following..
filter(function, iterable)

Note:- % checks the remainder..




Output will be;



Reduce Function

The reduce() function in Python is used to apply a function cumulatively to the items of a sequence, reducing the sequence to a single value.

Syntax is like the following..
reduce(function, iterable)

What this gives is the multiplication of all the values together. 1x2 = 2 then 2x3 = 6 then 6x4 =24 and finally 24x5 = 120

Note:- reduce should be imported from functools library

Here is another example of reduce() function

Note:- " " in the Lamba function is to make the space and the result will be like the following..