I'm looking for a way to loop over specific hosts filtered by partial suffix. Since the ipv6 networks are huge, my actual implementation is time-consuming and looking for a best solution to achieve the same result.
This is an example of my actual test to achieve what I need :
import ipaddress
from itertools import islice
for address, suffix in zip(['10.10.0.0/16','2A00:7E40:F020::/64'],['.1',':00FF:FE00:0003']):
print('===============')
network = ipaddress.ip_network(address)
print(str(network.num_addresses) + ' addresses available')
print('suffix ' + suffix)
print('===============')
for ip in islice(filter(lambda x: str(x.exploded).endswith(suffix),network.hosts()),0,2):
print(ip)
Actual output :
===============
65536 addresses available
suffix .1
===============
10.10.0.1
10.10.1.1
===============
18446744073709551616 addresses available
suffix :00FF:FE00:0003
===============
^C
Regards,
Tried to figure out how to speed up the filtering and the slicing, I've come to this solution who seems to do the job pretty fast even if we start in the middle of a huge IPV6 block
Output :