Skip to content

Instantly share code, notes, and snippets.

@ungeskriptet
Created October 9, 2024 10:24
Show Gist options
  • Save ungeskriptet/6b91bbbee854b26f4ae66005a5a8fd36 to your computer and use it in GitHub Desktop.
Save ungeskriptet/6b91bbbee854b26f4ae66005a5a8fd36 to your computer and use it in GitHub Desktop.
Python CIDR to IPv4 address converter
#!/usr/bin/python
import sys
cidr = sys.argv[1:][0]
octetts = ["0"] * 32
ip_dec = []
for i in range(int(cidr)):
octetts[i] = "1"
for a, b, c, d, e, f, g, h in zip(*[iter(octetts)]*8):
octett = a+b+c+d+e+f+g+h
ip_dec.append(str(int(octett, 2)))
print(".".join(ip_dec))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment