I'm new here. I'm working on a script that has to tell me if a giving IP address belongs to a IP range, using bash scripting. For example: starting from a big range like 10.103.240.0/20 I have to code a script in bash linux to discover if the ip 10.102.247.221 belongs to the range? I know that the HostMin is 10.103.240.1 and the HostMax 10.103.255.254 but i don't know how to implement the comparison. Do you have a suggestion please? I don't know where to start.
Thanks
This could work :
function checkInRange() {
hostMin=$1
hostMax=$2
ip=$3
for i in $(seq 4)
do
hMini=$(echo $hostMin | cut -d'.' -f$i)
hMaxi=$(echo $hostMax | cut -d'.' -f$i)
ipi=$(echo $ip | cut -d'.' -f$i)
if [[ $ipi -gt $hMaxi || $ipi -lt $hMini ]]
then
echo "$ip not in $hostMin - $hostMax range"
return 1
fi
done
echo "$ip in $hostMin - $hostMax range"
return 0
}
if [[ $(checkInRange $hostMin $hostMax $ip) -eq 0 ]]
then
# Do your stuff...
fi