diff options
author | Daniil Baturin <daniil@baturin.org> | 2018-11-05 03:05:54 +0100 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2018-11-05 03:05:54 +0100 |
commit | 67c9d917d4e63e65746e277d7e516a4d7f0826c0 (patch) | |
tree | 0de2f6faa3b4c03759f35149d3476cc2799936ba /src | |
parent | f0edf379f6b34ee49865ea030a6356dbb3ea1ce3 (diff) | |
download | vyos-1x-67c9d917d4e63e65746e277d7e516a4d7f0826c0.tar.gz vyos-1x-67c9d917d4e63e65746e277d7e516a4d7f0826c0.zip |
T288: add a script for normalizing IP(v6) addresses so that they can be safely passed to iproute2.
Diffstat (limited to 'src')
-rwxr-xr-x | src/system/normalize-ip | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/system/normalize-ip b/src/system/normalize-ip new file mode 100755 index 000000000..08f922a8e --- /dev/null +++ b/src/system/normalize-ip @@ -0,0 +1,43 @@ +#!/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 <http://www.gnu.org/licenses/>. +# +# + +# 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) + |