7 Using basic Python

In this chapter, we will look at using Python 3.7 with Netaddr module to calculate  IP network information:

  1. Please make sure Python 3.7 is installed on the computer.
    1. If you do not have Python installed, please go to www.python.org and download and install
    2. Once installed, please open a python console or IDLE and install netaddr module using pip
  2. Now your ready to start using the basic Python command to do some math and subnetting
    IDLE Screenshot
    Python Idle

    Now, convert decimal subnetmask to cidr format:

    Given the subnet mask 255.255.0.0, we note that 255=8bits flag, so two of the octets are all ‘1’ which gives in binary 16.

 

Now, using Python can we verify:

from netaddr import * = This imports netaddr module.

IPAddress is a function within the  module netaddr, now we can all the function IPAddress and adding value ‘255.255.0.0″ and asking Python to give the value in bits.  The function is just counting ‘1’.

For example = 255.255.0.0  in binary is 11111111.11111111.0.0 = 2^8. 2^8. 2^0.2^0.

So, to write a program which to take the value 255.255.255.0 and convert 255 into binary then count the ‘1’s, and split the value based on ‘.’ and sum the total. For example, take 255 = 8+8+8+0 = 24.

In Python, it would be netmask=’255.255.255.0′ then sum(bin(int(x))

 

 

 

 

License

Icon for the Public Domain license

This work (Introduction to intrusion detection system by Sung Lee) is free of known copyright restrictions.

Share This Book