Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
armyers committed Dec 7, 2020
0 parents commit 98a0d58
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions cidr-subnets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
#

import os
import sys
from ipaddress import ip_network

def main(argv):
"""
"""
if not (len(argv) >= 2 and len(argv) <= 3):
print('Usage: %s cidr-block [additional-subnet-split]' % argv[0])
sys.exit(1)

network = argv[1]
ip, netmask = network.split('/')
octets = ip.split('.')
while len(octets) < 4:
# add 0's to fill it out
octets += ['0']
# put the ip back together
ip = '.'.join(octets)
# put the network back together
network = ip + '/' + netmask

net = ip_network(network, strict=False)
subnet_split = 0
if len(argv) == 3:
subnet_split = int(argv[2])
subnets = list(net.subnets(subnet_split))

for subnet in subnets:
print ("net %s : broadcast %s : hosts %s" % (subnet.exploded, subnet.broadcast_address, subnet.num_addresses))
print("%s subnets" % int(len(subnets)))

if __name__ == '__main__':
main (sys.argv)

0 comments on commit 98a0d58

Please sign in to comment.