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:07:18 +0100 |
commit | f69a8bbf9cdaac6b3acb0b9da2d748ae37323e3e (patch) | |
tree | 26eaff8a9d552171b6f3abfdfa559066258d3d7e /src/system | |
parent | 7713fb958f606672804789ff431aa1f691ef4a46 (diff) | |
download | vyos-1x-f69a8bbf9cdaac6b3acb0b9da2d748ae37323e3e.tar.gz vyos-1x-f69a8bbf9cdaac6b3acb0b9da2d748ae37323e3e.zip |
T288: add a script for normalizing IP(v6) addresses so that they can be safely passed to iproute2.
Diffstat (limited to 'src/system')
-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) + |