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.
- Class A
- Class B
- Class C
- Class D (Reserved for multicast groups)
- 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:
- Your Network
- 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
- Installing sh
- $ 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.
- $ python netip.py
- import sh
-
- print "----------------------"
- print " "
- print " NetIP SCAN "
- print " --by amitsin6h-- "
- print " "
- print " start scanning…."
- print " Ctr+z to stop "
- print "-----------------------"
-
- for num in range(10,40):
- ip = "192.168.0."+str(num)
-
- try:
- sh.ping(ip, "-c 1",_out="/dev/null")
- print "PING ",ip , "OK"
- except sh.ErrorReturnCode_1:
- 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”.
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.