Skip to content

Instantly share code, notes, and snippets.

@alexkross
Last active July 29, 2020 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexkross/7f80accff12649b940fc9779813b9b91 to your computer and use it in GitHub Desktop.
Save alexkross/7f80accff12649b940fc9779813b9b91 to your computer and use it in GitHub Desktop.
Random network IP-address generator, fast IP-network tree builder and long prefix match (LPM) function
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
from numpy import random, iinfo, uint32
import ipaddress as ipa, functools as ft
count = 99
cidr = 16, 24
def rand_net(f, t, c=-1):
while c:
yield ipa.IPv4Address(int(random.randint(0, iinfo(uint32).max>>f, dtype='uint32')).to_bytes(4, byteorder='big'))
c = c - 1
def ipnp(t, i=0):
for n in t:
print(' '*i*4, str(n), sep='')
ipnp(t[n], i+1)
def ipnm(ns, t):
for n in t:
m = ipnm(ns, t[n])
if m:
return m
if ns.subnet_of(n):
return n
ipn = ft.partial(ipa.ip_network, strict=False)
ns = list(sorted(set(map(ipn, ('/'.join(map(str, (ip, random.randint(*cidr)))) for ip in rand_net(*cidr, count))))))
ns.reverse()
ft = []
t = {}
while ns:
nf = ns.pop()
for i in range(len(ft)):
np, st = ft[i]
if nf.subnet_of(np):
assert nf not in st # Debug
st[nf] = {}
ft.insert(i, (nf, st[nf]))
break
else:
t[nf] = {}
ft.append((nf, t[nf]))
ipnp(t)
for a in map(ipa.ip_network, map(int, random.choice(2**cidr[0], count//10))):
print('Address', str(a.network_address), 'matches', ipnm(a, t))
0.0.0.0/16
0.0.0.0/17
0.0.0.0/18
0.0.0.0/19
0.0.0.0/20
0.0.0.0/21
0.0.16.0/20
0.0.24.0/22
0.0.32.0/20
0.0.32.0/21
0.0.32.0/22
0.0.36.0/22
0.0.40.0/21
0.0.56.0/22
0.0.56.0/23
0.0.64.0/18
0.0.64.0/19
0.0.64.0/21
0.0.72.0/21
0.0.72.0/22
0.0.80.0/21
0.0.84.0/22
0.0.96.0/19
0.0.96.0/20
0.0.96.0/21
0.0.96.0/22
0.0.102.0/23
0.0.104.0/21
0.0.106.0/23
0.0.112.0/21
0.0.124.0/22
0.0.128.0/17
0.0.128.0/18
0.0.128.0/19
0.0.128.0/20
0.0.128.0/21
0.0.144.0/22
0.0.148.0/22
0.0.156.0/22
0.0.156.0/23
0.0.160.0/19
0.0.160.0/20
0.0.168.0/21
0.0.174.0/23
0.0.176.0/21
0.0.176.0/23
0.0.182.0/23
0.0.192.0/18
0.0.192.0/19
0.0.192.0/20
0.0.192.0/21
0.0.196.0/22
0.0.200.0/23
0.0.204.0/22
0.0.206.0/23
0.0.208.0/20
0.0.208.0/21
0.0.216.0/22
0.0.224.0/20
0.0.230.0/23
0.0.240.0/20
0.0.248.0/21
Network 0.0.88.13/32 matches 0.0.64.0/19
Network 0.0.242.131/32 matches 0.0.240.0/20
Network 0.0.82.32/32 matches 0.0.80.0/21
Network 0.0.59.1/32 matches 0.0.56.0/22
Network 0.0.198.87/32 matches 0.0.196.0/22
Network 0.0.144.181/32 matches 0.0.144.0/22
Network 0.0.11.226/32 matches 0.0.0.0/20
Network 0.0.250.201/32 matches 0.0.248.0/21
Network 0.0.208.27/32 matches 0.0.208.0/21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment