#!/usr/bin/env python
"""
Example script how to use the subnet splitting classes.
"""

import sys,os,logging

from systematic.shell import Script,ScriptError
from seine.address import IPv4Address,IPv6Address,SubnetPrefixIterator

USAGE = """
Split the given CIDR or IPv6 network to subnets by given bitmask, which must be
valid for the given split. You can split multiple networks with same command.
"""
script = Script(description=USAGE)
script.add_argument('address',help='Address range to split')
script.add_argument('bitmask',type=int,help='Netmask of split networks')
args = script.parse_args()

try:
    network = SubnetPrefixIterator(args.address,args.bitmask)
except ValueError,e:
    script.exit(1,e)

for splitnet in network:
    if type(splitnet) == IPv4Address:
        script.message('%s/%s' % (splitnet.network,splitnet.bitmask))
    elif type(splitnet) == IPv6Address:
        script.message(splitnet.network)

