diff --git a/misc/config-file.func b/misc/config-file.func index e6b2f62..33d8c5a 100644 --- a/misc/config-file.func +++ b/misc/config-file.func @@ -265,7 +265,7 @@ config_file() { local ip_cidr_regex='^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/([0-9]{1,2})$' local ip_regex='^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$' - ip_range_regex="^([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]{1,2}-([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]{1,2}$" + local ip_cidr_range_regex="^([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]{1,2}$" if [[ -n ${NET-} ]]; then if [ "$NET" == "dhcp" ]; then @@ -285,34 +285,49 @@ config_file() { msg_error "Gateway IP Address cannot be empty" exit fi - elif [[ "$NET" =~ ^([0-9]+\.[0-9]+\.[0-9]+)\.([0-9]+)/([0-9]+)-([0-9]+)/([0-9]+)$ ]]; then - base="${BASH_REMATCH[1]}" - start="${BASH_REMATCH[2]}" - end="${BASH_REMATCH[4]}" - cidr_start="${BASH_REMATCH[3]}" - cidr_end="${BASH_REMATCH[5]}" + elif [[ "$NET" == *-* ]]; then + IFS="-" read -r ip_start ip_end <<< "$NET" - # Optional check: CIDR must match (can be customized) - if [[ "$cidr_start" != "$cidr_end" ]]; then - msg_error "Mismatched CIDR: /$cidr_start and /$cidr_end" + if [[ ! "$ip_start" =~ $ip_cidr_regex ]] || [[ ! "$ip_end" =~ $ip_cidr_regex ]]; then + echo "Invalid IP range format: $NET" exit 1 fi - msg_info "Checking IPs from $base.$start/$cidr_start to $base.$end/$cidr_start" + # Extract IPs and ignore CIDR for scanning + ip1="${ip_start%%/*}" + ip2="${ip_end%%/*}" + cidr="${ip_start##*/}" - for ((i=start; i<=end; i++)); do - ip="$base.$i" - if ! ping -c 1 -W 1 "$ip" > /dev/null 2>&1; then - NET="$ip/$cidr_start" - msg_ok "Selected unused IP: $NET" + # Convert IP to integer + ip_to_int() { + local IFS=. + read -r i1 i2 i3 i4 <<< "$1" + echo $(( (i1 << 24) + (i2 << 16) + (i3 << 8) + i4 )) + } + + # Convert integer back to IP + int_to_ip() { + local ip=$1 + echo "$(( (ip >> 24) & 0xFF )).$(( (ip >> 16) & 0xFF )).$(( (ip >> 8) & 0xFF )).$(( ip & 0xFF ))" + } + + start_int=$(ip_to_int "$ip1") + end_int=$(ip_to_int "$ip2") + + for ((ip_int=start_int; ip_int<=end_int; ip_int++)); do + ip=$(int_to_ip $ip_int) + if ! ping -c 1 -W 1 "$ip" >/dev/null 2>&1; then + NET="$ip/$cidr" + echo "Selected unused IP: $NET" break fi done - if [[ ! "$NET" =~ / ]]; then - msg_error "No free IP found in range" + if [[ "$NET" == *-* ]]; then + echo "No free IP found in range" exit 1 fi + if [ -n "$GATE" ]; then if [[ "$GATE" =~ $ip_regex ]]; then echo -e "${GATEWAY}${BOLD}${DGN}Gateway IP Address: ${BGN}$GATE${CL}"