Skip to content

Instantly share code, notes, and snippets.

@heyalexej
Created June 20, 2014 06:18
Show Gist options
  • Save heyalexej/7326e28585a54c92f8bf to your computer and use it in GitHub Desktop.
Save heyalexej/7326e28585a54c92f8bf to your computer and use it in GitHub Desktop.
Quick and dirty solution to translate IPv4 and IPv6 CIDR blocks into a list of IPs for further digestion.
#!/usr/bin/env python3
# Translates IPv4/6 CIDR blocks into a list of IPs and writes
# them into a text file. Requires netaddr (pip install netaddr).
import netaddr
ip_list = netaddr.IPNetwork('192.168.1.0/21')
with open('ips-from-cidr.txt', 'w') as f:
for ip in ip_list:
f.write(str(ip) + '\n')
#print(str(ip)) # additionally print to stdout if you like
f.close()
print('\nGenerated "{0:s}" with {1:d} IPs.\n'.format((f.name),
len(ip_list)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment