Python – check if ip is public or private

 

netaddr is a Python library for representing and manipulating network addresses.

Installing netaddr library:

Use any of following command to install netaddr library

$ sudo pip install netaddr
or
$ easy_install netaddr

Observe following example for more details:

>>> from netaddr import *
>>> IPAddress('74.125.236.194').is_private()
False
>>> IPAddress('192.168.1.10').is_private()
True
>>> IPAddress('127.0.0.1').is_loopback()
True

IPAddress(‘input ip’).is_private() will return true if the input ip address private, else it will return false.

Reference: Netaddr

Ping all IP’s in a subnet

 

We can use nmap command to ping all IP’s in subnet.

If you don’t have nmap installed in your machine install nmap with following command:

$ apt-get install nmap

Use following command to ping all IP’s in a subnet:

$ nmap -n -sP 192.168.2.0/26

Output:

Starting Nmap 5.21 ( http://nmap.org ) at 2014-06-30 17:04 PDT
Nmap scan report for 192.168.2.1
Host is up (0.00073s latency).
Nmap scan report for 192.168.2.2
Host is up (0.0011s latency).
Nmap scan report for 192.168.2.4
Host is up (0.0010s latency).
Nmap done: 64 IP addresses (3 hosts up) scanned in 0.62 seconds

In above output we can see all reachable IP’s.