IP Address Analysis Using Python

Find out more about IP addresses (IPv4 and IPv6) using the Python module IPy:

#!/usr/bin/env python

from IPy import IP

ip_s = raw_input('Please enter an IP address or range: ')
try:
    i = IP(ip_s)
except ValueError:
    print('Could not understand your input %s. Exiting.' % ip_s)
    from sys import exit
    exit(1)

print('I understood: %s' % i)
print('This is an IPv%d address.' % i.version())

#if i._prefixlen != 32: # a network
try:
    net = len(i) > 1 # a network
    size = len(i)
except:
    net = True
    size = "large"
if net:
    print('net: %s' % i.net())
    print('netmask: %s' % i.netmask())
    print('broadcast: %s' % i.broadcast())
    print('reverse notation of net address: %s' % i.reverseNames()[0])
    print('size of subnet: %s' % size)
else: # a single IP
    print('reverse notation: %s' % i.reverseNames()[0])

if i.version() == 6:
    print('normal notation: %s' % i.strNormal())
    print('full size notation: %s' % i.strFullsize())
print('hexadecimal notation: %s' % i.strHex())
print('string notation of binary value: %s' % i.strBin())
print('type of ip: %s' % i.iptype())
if i.get_mac(): print('found possible mac address: %s' % i.get_mac())

References

Comments