146 lines
3.8 KiB
Bash
146 lines
3.8 KiB
Bash
#!/bin/bash
|
||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||
#
|
||
# Rx3 IP Library
|
||
#
|
||
# Copyright (C) 2025-2026 Arnaud G. GIBERT
|
||
# mailto:arnaud@rx3.net
|
||
#
|
||
# This is free software: you can redistribute it and/or modify it
|
||
# under the terms of the GNU Lesser General Public License as published
|
||
# by the Free Software Foundation, either version 3 of the License, or
|
||
# (at your option) any later version.
|
||
#
|
||
# 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 Lesser General Public License for more details.
|
||
#
|
||
# You should have received a copy of the GNU Lesser General Public
|
||
# License along with this program; If not, see
|
||
# <https://www.gnu.org/licenses/>.
|
||
#
|
||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||
|
||
if [[ "${RX3_IP_LIB}" != "" ]]
|
||
then
|
||
return
|
||
else
|
||
declare -g RX3_IP_LIB=1
|
||
fi
|
||
|
||
|
||
|
||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||
# Includes
|
||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||
|
||
: "${RX3_LIB_DIR:=/usr/lib/rx3}"
|
||
. "${RX3_LIB_DIR}/base.bash"
|
||
|
||
|
||
|
||
|
||
|
||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||
# Global Variable
|
||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||
|
||
|
||
|
||
|
||
|
||
#--------------------------------------------------------------------------------------------------------------------------
|
||
# Is Valid Ip
|
||
#--------------------------------------------------------------------------------------------------------------------------
|
||
|
||
ip_is_valid_ip()
|
||
{
|
||
local ip=$1
|
||
local regex='^([0-9]{1,3}\.){3}[0-9]{1,3}$'
|
||
|
||
|
||
if [[ $ip =~ $regex ]]
|
||
then
|
||
IFS='.' read -r o1 o2 o3 o4 <<< "$ip"
|
||
|
||
for octet in $o1 $o2 $o3 $o4
|
||
do
|
||
if (( octet < 0 || octet > 255 ))
|
||
then
|
||
return 1
|
||
fi
|
||
done
|
||
|
||
return 0
|
||
else
|
||
return 1
|
||
fi
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
#--------------------------------------------------------------------------------------------------------------------------
|
||
# Ip To Num
|
||
#--------------------------------------------------------------------------------------------------------------------------
|
||
|
||
ip_ip_to_num()
|
||
{
|
||
local ip="$1"
|
||
local a
|
||
local b
|
||
local c
|
||
local d
|
||
|
||
IFS=. read -r a b c d <<< "${ip}"
|
||
|
||
echo $(( (a << 24) + (b << 16) + (c << 8) + d ))
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
#--------------------------------------------------------------------------------------------------------------------------
|
||
# Num To Ip
|
||
#--------------------------------------------------------------------------------------------------------------------------
|
||
|
||
ip_num_to_ip()
|
||
{
|
||
local num="$1"
|
||
|
||
|
||
# Optional safety check
|
||
if (( num < 0 || num > 4294967295 ))
|
||
then
|
||
echo_error "num_to_ip: value out of range (0‑4294967295)"
|
||
return 1
|
||
fi
|
||
|
||
# Extract each byte by shifting and masking
|
||
local a=$(( (num >> 24) & 255 ))
|
||
local b=$(( (num >> 16) & 255 ))
|
||
local c=$(( (num >> 8) & 255 ))
|
||
local d=$(( num & 255 ))
|
||
|
||
printf '%d.%d.%d.%d\n' "$a" "$b" "$c" "$d"
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||
# IP Filter
|
||
#-----------------------------------------------------------------------------------------------------------------------------------
|
||
|
||
ip_ip_filter()
|
||
{
|
||
grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'
|
||
}
|
||
|
||
|
||
|