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 - to


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 - to


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 - to


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


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 is between ( to

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 is between( to

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 is between ( to

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 Run the Python program by command.

  1. $ python   
  2. import sh    
  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 "-----------------------"  
  13. for num in range(10,40):  
  14.     ip = "192.168.0."+str(num)  
  16.     try:  
  17., "-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 to

As the program is based on for loop, it will keep checking IP from to 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”.


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.


Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now