Calculating all IPs in subnet using Python 2.7 built in modules only

2.8k views Asked by At

I am wondering how to create a script that takes a cidr notation like this:

204.15.20.0/22

And calculates all possible IP addresses in that range.

204.15.20.0
...
...
204.15.23.255

Now I finally know the math behind this, I was wondering how I could make such a module in python.

I am aware of ready-available modules such as netaddr.IPNetwork() but I am wondering how I would do this, using only the built-in modules in Python 2.7

Thanks :)

1

There are 1 answers

0
tdelaney On

You can convert between IPv4 addresses and integers with a combination of the socket and struct modules. After that its a question of masking the integer address and figuring out the range of values in the subnet from the cidr value.

import re
import socket
import struct

def inet_atoi(ipv4_str):
    """Convert dotted ipv4 string to int"""
    # note: use socket for packed binary then struct to unpack
    return struct.unpack("!I", socket.inet_aton(ipv4_str))[0]

def inet_itoa(ipv4_int):
    """Convert int to dotted ipv4 string"""
    # note: use struct to pack then socket to string
    return socket.inet_ntoa(struct.pack("!I", ipv4_int))

def ipv4_range(ipaddr):
    """Return a list of IPv4 address contianed in a cidr address range"""
    # split out for example 192.168.1.1:22/24
    ipv4_str, port_str, cidr_str = re.match(
        r'([\d\.]+)(:\d+)?(/\d+)?', ipaddr).groups()

    # convert as needed
    ipv4_int = inet_atoi(ipv4_str)
    port_str = port_str or ''
    cidr_str = cidr_str or ''
    cidr_int = int(cidr_str[1:]) if cidr_str else 0

    # mask ipv4
    ipv4_base = ipv4_int & (0xffffffff << (32 - cidr_int))

    # generate list
    addrs = [inet_itoa(ipv4_base + val)
        for val in range(1 << (32 - cidr_int) + 2)]
    return addrs

print(ipv4_range('204.15.20.0/22'))