improve ip validation

This commit is contained in:
buildplan
2025-11-17 11:42:32 +00:00
parent fca2be09b5
commit 6e2b9f7254

View File

@@ -2559,7 +2559,6 @@ validate_ip_or_cidr() {
local input="$1"
# IPv4 address (simple check)
if [[ "$input" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
# Validate octets are <= 255
local -a octets
IFS='.' read -ra octets <<< "$input"
for octet in "${octets[@]}"; do
@@ -2573,7 +2572,6 @@ validate_ip_or_cidr() {
if [[ "$input" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]{1,2}$ ]]; then
local ip="${input%/*}"
local cidr="${input##*/}"
# Validate IP part
local -a octets
IFS='.' read -ra octets <<< "$ip"
for octet in "${octets[@]}"; do
@@ -2581,18 +2579,17 @@ validate_ip_or_cidr() {
return 1
fi
done
# Validate CIDR is 0-32
if [[ "$cidr" -ge 0 && "$cidr" -le 32 ]]; then
return 0
fi
return 1
fi
# IPv6 address (basic check)
if [[ "$input" =~ ^([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}$ ]]; then
if [[ "$input" =~ ^[0-9a-fA-F:]+$ && "$input" == *":"* && "$input" != *"/"* ]]; then
return 0
fi
# IPv6 CIDR (e.g., 2001:db8::/32)
if [[ "$input" =~ ^([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}/[0-9]{1,3}$ ]]; then
# IPv6 CIDR (permissive check, allows compressed ::)
if [[ "$input" =~ ^[0-9a-fA-F:]+/[0-9]{1,3}$ && "$input" == *":"* ]]; then
local cidr="${input##*/}"
if [[ "$cidr" -ge 0 && "$cidr" -le 128 ]]; then
return 0