forked from vyos/vyos-1x
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request vyos#4106 from dmbaturin/T6743-native-range-valida…
…tors validators: T6743: use native ipaddrcheck validator options for ranges
- Loading branch information
Showing
2 changed files
with
14 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,10 @@ | ||
#!/bin/bash | ||
#!/bin/sh | ||
|
||
# snippet from https://stackoverflow.com/questions/10768160/ip-address-converter | ||
ip2dec () { | ||
local a b c d ip=$@ | ||
IFS=. read -r a b c d <<< "$ip" | ||
printf '%d\n' "$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d))" | ||
} | ||
ipaddrcheck --verbose --is-ipv4-range "$1" | ||
|
||
error_exit() { | ||
echo "Error: $1 is not a valid IPv4 address range" | ||
exit 1 | ||
} | ||
|
||
# Only run this if there is a hypen present in $1 | ||
if [[ "$1" =~ "-" ]]; then | ||
# This only works with real bash (<<<) - split IP addresses into array with | ||
# hyphen as delimiter | ||
readarray -d - -t strarr <<< $1 | ||
|
||
ipaddrcheck --is-ipv4-single ${strarr[0]} | ||
if [ $? -gt 0 ]; then | ||
error_exit $1 | ||
fi | ||
|
||
ipaddrcheck --is-ipv4-single ${strarr[1]} | ||
if [ $? -gt 0 ]; then | ||
error_exit $1 | ||
fi | ||
|
||
start=$(ip2dec ${strarr[0]}) | ||
stop=$(ip2dec ${strarr[1]}) | ||
if [ $start -ge $stop ]; then | ||
error_exit $1 | ||
fi | ||
|
||
exit 0 | ||
if [ $? -gt 0 ]; then | ||
echo "Error: $1 is not a valid IPv4 address range" | ||
exit 1 | ||
fi | ||
|
||
error_exit $1 | ||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,10 @@ | ||
#!/usr/bin/env python3 | ||
#!/bin/sh | ||
|
||
from ipaddress import IPv6Address | ||
from sys import argv, exit | ||
ipaddrcheck --verbose --is-ipv6-range "$1" | ||
|
||
if __name__ == '__main__': | ||
if len(argv) > 1: | ||
# try to pass validation and raise an error if failed | ||
try: | ||
ipv6_range = argv[1] | ||
range_left = ipv6_range.split('-')[0] | ||
range_right = ipv6_range.split('-')[1] | ||
if not IPv6Address(range_left) < IPv6Address(range_right): | ||
raise ValueError(f'left element {range_left} must be less than right element {range_right}') | ||
except Exception as err: | ||
print(f'Error: {ipv6_range} is not a valid IPv6 range: {err}') | ||
exit(1) | ||
else: | ||
print('Error: an IPv6 range argument must be provided') | ||
exit(1) | ||
if [ $? -gt 0 ]; then | ||
echo "Error: $1 is not a valid IPv6 address range" | ||
exit 1 | ||
fi | ||
|
||
exit 0 |