Shell script implementation determines whether the IP address is code shared within an IP segment

Time:2022-1-13

cat ipcalc.sh
#!/bin/bash
IP=$1
if [ -z $IP ]
then
echo -e “\e[31;42m sh ipcalc.sh IP \e[0m"
exit
fi
while read MASK
do
min=`/usr/bin/ipcalc $MASK|awk '/HostMin:/{print $2}'`
max=`/usr/bin/ipcalc $MASK|awk '/HostMax:/{print $2}'`
MIN=`echo $min|awk -F"." '{printf"%.0f\n",$1*256*256*256+$2*256*256+$3*256+$4}'`
MAX=`echo $max|awk -F"." '{printf"%.0f\n",$1*256*256*256+$2*256*256+$3*256+$4}'`
IPvalue=`echo $IP|awk -F"." '{printf"%.0f\n",$1*256*256*256+$2*256*256+$3*256+$4}'`
if [ "$IPvalue" -gt "$MIN" ] && [ "$IPvalue" -lt "$MAX" ]
then
echo -e “\e[31;42m $IP in ipset $MASK \e[0m”
exit
fi
done<ipset.conf

ipset. Conf format


more ipset.conf
58.24.64.0/18
58.24.0.0/18
58.24.128.0/18
58.24.192.0/18
58.25.128.0/18
58.25.192.0/18
64.254.111.0/24

Run sh IPCalc SH) IP address

The efficiency is not high. Hey, try Perl next time

/Usr / bin / IPCalc , this is not the system’s own command , it’s a Perl version