Skip to content

Instantly share code, notes, and snippets.

@joswr1ght

joswr1ght/countips.py

Last active Feb 18, 2021
Embed
What would you like to do?
Read a file of network + CIDR masks, one per line; count the number of IP addresses it represents
#!/usr/bin/env python
import sys
def countips(netblock):
cidr = int(netblock.split('/')[1])
return 2**(32 - cidr)
if (len(sys.argv) != 2):
print(f"Usage: {sys.argv[0]} <file with CIDR masks>")
sys.exit(0)
ipcount=0
with open(sys.argv[1]) as infile:
for netblock in infile:
ipcount += countips(netblock)
print(ipcount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment