Skip to content

Instantly share code, notes, and snippets.

@phwelo
Created October 20, 2021 18:44
Show Gist options
  • Save phwelo/57697986e0b766348da7c0ebf9a00abf to your computer and use it in GitHub Desktop.
Save phwelo/57697986e0b766348da7c0ebf9a00abf to your computer and use it in GitHub Desktop.
Simple tool to prevent me needing to remember bit to range conversions. accepts single param of cidr in 10.0.0.0/32 format
#!/usr/bin/env python3
import sys
import ipaddress
CYAN = '\033[96m'
END = '\033[0m'
BOLD = '\033[1m'
if len(sys.argv) < 2:
raise ValueError('No CIDR was provided on the command line.')
elif len(sys.argv) > 2:
raise ValueError('Only one CIDR supported')
network = ipaddress.IPv4Network(sys.argv[1])
def head(str_heading):
return emph(str_heading) + " " + END
def text(str_text):
return BOLD + emph(str_text)
def emph(str_emph):
return CYAN + str(str_emph) + END
cidr = head("Cidr: ") + text(network.network_address) + emph("/") + text(network.prefixlen)
private = head("Private:") + text(network.is_private)
range = head("Range:") + text(network[0]) + emph("-") + text(network[-1])
lines = [
cidr + " " + private,
range
]
for line in lines:
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment