In this tutorial, we will Using Python script to calculate Network ID.
Let’s look at how to write a simple Python script to calculate the Net-ID given an IP address and a subnet mask.
We will work with 10.10.10.65/26.
This is the script I will use.
Now, let’s go through it step by step to see what all the lines on it do. But first Quick reminder. Net-ID is derived by ANDing the binary equivalents of both the IP Address and the Subnet Mask.
ANDing in python is part of what are known as BITWISE operators. This are operators used on binary values. There are 6 primary bitwise operators.
Let’s examine the effect of the operators on a binary variable A and B
The NOT operator ~ is used to get the binary bit opposite. For any given variable, at any point we have 1 we replace that with 0 and where we have 0 we replace that with 1.
The shift operators are used to shift the binary bit values a number of stated times either to the left << or to the right >>. Here is an example.
>>>print(0b00110010) 50 >>>print(0b00110010 >>2) 12
So what exactly is happening here? The binary value 00110010 translates to decimal 50. If we shift the binary bits 2 times to the right the binary bits we get are 00001100. Hence an output of 12. Try the same with the << left shift
Now, Lets go back to our script.
To carry out the ANDing operation we need to convert our decimal numbers into binary octets.
Python has an inbuilt function bin( ) which we can use to convert an integer into its binary bit equivalent.
IP Address 10.10.10.65
SN Mask 255.255.255.192
I have converted a few below as an example:
>>>bin(10) 0b1010 >>>bin(65) 0b1000001 >>>bin(192) 0b11000000
Once your conversions are done we build a List each with the binary equivalents for the IP address and SN mask. We will use this lists for our next section.
The next part requires that we run a recursive AND on each of the elements of both our list. For this we will need to invoke a loop. In this case we will use a for loop.
Before we do this however, lets create another empty list which we will use to hold the outcomes of our operation.
We will run the loop 4 times as it is set up to run from iteration 0 to 4-1(3).
Within each loop it references iplist[element space] & masklist[element space] . The outcome of this operation is assigned to a variable netid.
Let’s take one example iplist is 0b1010 and masklist is 0b11111111. AND the two and the value received is 0b1010.
Let’s take a closer look at this line.
List are mutable data structures within Python. Meaning that the elements within them can be adjusted, added or removed. The append( ) function is used to add elements to the end of an existing List. We already created an empty list networkidlist[ ]. Now we need to populate it with elements as we receive them from the loop iterations.
Once the iterations have run our list networkidlist will contain the below details
[0b1010, 0b100, 0b1010, 0b1000000]
However when we print the contents of the list what we will see as output is [10, 10, 10, 64].
The final print statement references the elements of the networkidlist one by one using their index numbers. Converts them from integers to string to enable them be output with the statement.
The final statement that will be displayed on the screen will be
We have come to end of this tutorial. I hope you enjoyed it as much as i did.