Python Program To Find All IPs Assigned In A Network

Lets name this program NetIP(Network IP)

Before we start to create a Net IP, a few things that we should know are as follows.

What is IP ?

Internet Protocol (IP)

The Internet Protocol (IP) is the method or protocol by which data is sent from one computer to another over the internet.

IP address classes

So basically, there are five (5) classes of IP available.

  1. Class A
  2. Class B
  3. Class C
  4. Class D (Reserved for multicast groups)
  5. Class E (Reserved for future use.)

We wont be dealing Class D and Class E IP.

Class A

Address Range - 1.0.0.1 to 126.255.255.254

Supports

This Class A IP supports 16 million hosts on each of 127 networks. Class A IPs are used by huge networks, like those deployed by Internet Service Providers (ISPs).

Class B

Address Range - 128.1.0.1 to 191.255.255.254

Supports

This Class B IP supports 65,000 hosts on each of 16,000 networks. Class B IPs are used by medium and large-sized networks in enterprises and organization.

Class C

Address Range - 192.0.1.1 to 223.255.254.254

Supports

This Class C IP supports 254 hosts on each of 2 million networks. Class C IPs are used by small business and home networks.

Now let us understand,

Why IP Address are divided into four parts

part1.part2.part3.part4

These four (4) parts are also divided into two (2) sections that define:

  1. Your Network
  2. Your Computer, or Host

Now, let's understand IP Address of Class A, Class B, Class C

Class A IP

As we know from the above, Class A IP can be anywhere from 1 to 126.

Example of Class A IP

102.168.212.226 is between (1.0.0.1 to 126.255.255.254)

Here in the given IP, "102" identifies the network and "168.212.226" identifies the host on that network.

Class B IP

As we know from the above, Class B IP can be anywhere from 128 to 191.

Example of Class B IP

168.212.226.204 is between(128.1.0.1 to 191.255.255.254)

Here in the given IP, "168.212" identifies the network and "226.204" identifies the host on that network.

Class C IP

As we know from the above, Class C IP can be anywhere from 192 to 223.

Example of Class C IP

200.168.212.226 is between (192.0.1.1 to 223.255.254.254)

Here in the given IP, "200.168.212" identifies the network and "226" identifies the host on that network.

This information is enough to understand our Python program.

Lets start creating NetIP

To create this program we need to install Python module named sh

sh is full-fledged subprocess 

  1. Installing sh  
  2. $ pip install sh   

With the help of sh, we make ping request to IP to check wheather the IP exists or not.

Just copy the below code and paste it and name it as netip.py. Run the Python program by command.

  1. $ python netip.py   
  2. import sh    
  3.   
  4. print "----------------------"  
  5. print "                              "  
  6. print "    NetIP  SCAN    "  
  7. print "   --by amitsin6h-- "  
  8. print "                              "  
  9. print "   start scanning…."  
  10. print "   Ctr+z to stop      "  
  11. print "-----------------------"  
  12.   
  13. for num in range(10,40):  
  14.     ip = "192.168.0."+str(num)  
  15.   
  16.     try:  
  17.         sh.ping(ip, "-c 1",_out="/dev/null")  
  18.         print "PING ",ip , "OK"  
  19.     except sh.ErrorReturnCode_1:  
  20.         print "PING ", ip, "FAILED"   

Understanding the code

As you can see, I have imported the sh module. After this, I am printing the message which I want to show while running the program.

Now, I am checking the host from 10 to 40. You can see that I am checking Class C Network IP from range 192.168.0.10 to 192.168.0.39.

As the program is based on for loop, it will keep checking IP from 192.168.0.10 to 192.168.0.39 and will tell us if the IP Exists or not in the network.

Inside for loop, we wrote try except statement. Basically, this try except statement tries to ping the IP for one time (we have defined the time intervel ‘-c 1’) and if the IP exists, it prints out “PING ipaddress OK” but when it gets exception sh.ErrorReturnCode_1 which means IP address not present in the network, we print out the message “PING ipaddress FAILED”.

python

Accordingly, you can change the IP to find another Class IP addresses.

This is just an overview of how you can create programs to find Network IP. You can do it in a more interesting way by using raw_input to assign IP directly from user instead of writing it. Let me know if you need any help or if you are having problems in understanding.