Config File
This commit is contained in:
parent
27da0b98fe
commit
77080e6fb5
@ -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_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})$'
|
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 [[ -n ${NET-} ]]; then
|
||||||
if [ "$NET" == "dhcp" ]; then
|
if [ "$NET" == "dhcp" ]; then
|
||||||
@ -285,34 +285,49 @@ config_file() {
|
|||||||
msg_error "Gateway IP Address cannot be empty"
|
msg_error "Gateway IP Address cannot be empty"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
elif [[ "$NET" =~ ^([0-9]+\.[0-9]+\.[0-9]+)\.([0-9]+)/([0-9]+)-([0-9]+)/([0-9]+)$ ]]; then
|
elif [[ "$NET" == *-* ]]; then
|
||||||
base="${BASH_REMATCH[1]}"
|
IFS="-" read -r ip_start ip_end <<< "$NET"
|
||||||
start="${BASH_REMATCH[2]}"
|
|
||||||
end="${BASH_REMATCH[4]}"
|
|
||||||
cidr_start="${BASH_REMATCH[3]}"
|
|
||||||
cidr_end="${BASH_REMATCH[5]}"
|
|
||||||
|
|
||||||
# Optional check: CIDR must match (can be customized)
|
if [[ ! "$ip_start" =~ $ip_cidr_regex ]] || [[ ! "$ip_end" =~ $ip_cidr_regex ]]; then
|
||||||
if [[ "$cidr_start" != "$cidr_end" ]]; then
|
echo "Invalid IP range format: $NET"
|
||||||
msg_error "Mismatched CIDR: /$cidr_start and /$cidr_end"
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
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
|
# Convert IP to integer
|
||||||
ip="$base.$i"
|
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
|
if ! ping -c 1 -W 1 "$ip" >/dev/null 2>&1; then
|
||||||
NET="$ip/$cidr_start"
|
NET="$ip/$cidr"
|
||||||
msg_ok "Selected unused IP: $NET"
|
echo "Selected unused IP: $NET"
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ ! "$NET" =~ / ]]; then
|
if [[ "$NET" == *-* ]]; then
|
||||||
msg_error "No free IP found in range"
|
echo "No free IP found in range"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -n "$GATE" ]; then
|
if [ -n "$GATE" ]; then
|
||||||
if [[ "$GATE" =~ $ip_regex ]]; then
|
if [[ "$GATE" =~ $ip_regex ]]; then
|
||||||
echo -e "${GATEWAY}${BOLD}${DGN}Gateway IP Address: ${BGN}$GATE${CL}"
|
echo -e "${GATEWAY}${BOLD}${DGN}Gateway IP Address: ${BGN}$GATE${CL}"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user