#!/usr/bin/env python3 # # Copyright (C) 2018 VyOS maintainers and contributors # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 or later as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # # Normalizes IPv6 addresses so that they can be passed to iproute2, # since iproute2 will not take an address with leading zeroes for an argument import re import sys import ipaddress if __name__ == '__main__': if len(sys.argv) < 2: print("Argument required") sys.exit(1) address_string, prefix_length = re.match(r'(.+)/(.+)', sys.argv[1]).groups() try: address = ipaddress.IPv6Address(address_string) normalized_address = address.compressed except ipaddress.AddressValueError: # It's likely an IPv4 address, do nothing normalized_address = address_string print("{0}/{1}".format(normalized_address, prefix_length)) sys.exit(0)