Check IP is in Range of Whitelist Array
You can use grepcidr to check if an IP address is in a list of CIDR networks.
#! /bin/bash
NETWORKS="108.161.176.0/20 94.46.144.0/20 146.88.128.0/20 198.232.124.0/22
23.111.8.0/22 217.22.28.0/22 64.125.76.64/27 64.125.76.96/27
64.125.78.96/27 64.125.78.192/27 64.125.78.224/27 64.125.102.32/27
64.125.102.64/27 64.125.102.96/27 94.31.27.64/27 94.31.33.128/27
94.31.33.160/27 94.31.33.192/27 94.31.56.160/27 177.54.148.0/24
185.18.207.65/26 50.31.249.224/27 50.31.251.32/28 119.81.42.192/27
119.81.104.96/28 119.81.67.8/29 119.81.0.104/30 119.81.1.144/30
27.50.77.226/32 27.50.79.130/32 119.81.131.130/32 119.81.131.131/32
216.12.211.59/32 216.12.211.60/32 37.58.110.67/32 37.58.110.68/32
158.85.206.228/32 158.85.206.231/32 174.36.204.195/32
174.36.204.196/32"
for IP in 108.161.184.123 108.161.176.123 192.168.0.1 172.16.21.99; do
grepcidr "$NETWORKS" <(echo "$IP") >/dev/null && \
echo "$IP is in MAXCDN range" || \
echo "$IP is not in MAXCDN range"
done
NOTE: grepcidr
expects the IP address(es) it is matching to be in a file, not just an argument on the command line. That's why I had to use <(echo "$IP")
above.
Output:
108.161.184.123 is in MAXCDN range
108.161.176.123 is in MAXCDN range
192.168.0.1 is not in MAXCDN range
172.16.21.99 is not in MAXCDN range
grepcidr
is available pre-packaged for several distros, including Debian:
Package: grepcidr
Version: 2.0-1
Description-en: Filter IP addresses matching IPv4 CIDR/network specification
grepcidr can be used to filter a list of IP addresses against one or
more Classless Inter-Domain Routing (CIDR) specifications, or
arbitrary networks specified by an address range. As with grep, there
are options to invert matching and load patterns from a file.
grepcidr is capable of comparing thousands or even millions of IPs
to networks with little memory usage and in reasonable computation
time.
.
grepcidr has endless uses in network software, including: mail
filtering and processing, network security, log analysis, and many
custom applications.
Homepage: http://www.pc-tools.net/unix/grepcidr/
Otherwise, the source is available at the link above.
Another alternative is to write a perl
or python
script using one of the many libraries/modules for manipulating and checking IPv4 addresses with those languages.
For example, the perl
module Data::Validate::IP
has an is_innet_ipv4($ip, $network)
function; Net::CIDR::Lite
has a very similar $cidr->find($ip);
method; and Net::IPv4Addr
has an ipv4_in_network()
function.
python
has comparable libraries, including ipy
, ipaddr
, and ipcalc
, amongst others.
I realize that this is older and already has an accepted answer, but this solution utilizes a function I wrote for portability across multiple servers in an environment where grepcidr
wasn't a standard package across all servers.
The comments should clarify just what it's doing.
#! /bin/bash
# Set DEBUG=1, in order to see it iterate through the calculations.
#DEBUG=1
MAXCDN_ARRAY="108.161.176.0/20 94.46.144.0/20 146.88.128.0/20 198.232.124.0/22 23.111.8.0/22 217.22.28.0/22 64.125.76.64/27 64.125.76.96/27 64.125.78.96/27 64.125.78.192/27 64.125.78.224/27 64.125.102.32/27 64.125.102.64/27 64.125.102.96/27 94.31.27.64/27 94.31.33.128/27 94.31.33.160/27 94.31.33.192/27 94.31.56.160/27 177.54.148.0/24 185.18.207.65/26 50.31.249.224/27 50.31.251.32/28 119.81.42.192/27 119.81.104.96/28 119.81.67.8/29 119.81.0.104/30 119.81.1.144/30 27.50.77.226/32 27.50.79.130/32 119.81.131.130/32 119.81.131.131/32 216.12.211.59/32 216.12.211.60/32 37.58.110.67/32 37.58.110.68/32 158.85.206.228/32 158.85.206.231/32 174.36.204.195/32 174.36.204.196/32"
IP=108.161.184.123
function in_subnet {
# Determine whether IP address is in the specified subnet.
#
# Args:
# sub: Subnet, in CIDR notation.
# ip: IP address to check.
#
# Returns:
# 1|0
#
local ip ip_a mask netmask sub sub_ip rval start end
# Define bitmask.
local readonly BITMASK=0xFFFFFFFF
# Set DEBUG status if not already defined in the script.
[[ "${DEBUG}" == "" ]] && DEBUG=0
# Read arguments.
IFS=/ read sub mask <<< "${1}"
IFS=. read -a sub_ip <<< "${sub}"
IFS=. read -a ip_a <<< "${2}"
# Calculate netmask.
netmask=$(($BITMASK<<$((32-$mask)) & $BITMASK))
# Determine address range.
start=0
for o in "${sub_ip[@]}"
do
start=$(($start<<8 | $o))
done
start=$(($start & $netmask))
end=$(($start | ~$netmask & $BITMASK))
# Convert IP address to 32-bit number.
ip=0
for o in "${ip_a[@]}"
do
ip=$(($ip<<8 | $o))
done
# Determine if IP in range.
(( $ip >= $start )) && (( $ip <= $end )) && rval=1 || rval=0
(( $DEBUG )) &&
printf "ip=0x%08X; start=0x%08X; end=0x%08X; in_subnet=%u\n" $ip $start $end $rval 1>&2
echo "${rval}"
}
for subnet in $MAXCDN_ARRAY
do
(( $(in_subnet $subnet $IP) )) &&
echo "${IP} is in ${subnet}" && break
done
I wanted to run this on a bunch of hosts without having to install grepcidr
and I tried the script by Doug R. but it didn't work, so here's one I wrote that's confirmed working. Hopefully someone will find this useful:
function in_subnet {
# Determine whether IP address is in the specified subnet.
#
# Args:
# cidr_subnet: Subnet, in CIDR notation.
# ip_addr: IP address to check.
#
# Returns:
# 0|1
#
local readonly cidr_subnet="${1}"
local readonly ip_addr="${2}"
local subnet_ip cidr_mask netmask ip_addr_subnet subnet rval
subnet_ip=$(echo "${cidr_subnet}" | cut -d'/' -f1)
cidr_mask=$(echo "${cidr_subnet}" | cut -d'/' -f2)
netmask=$(( 0xFFFFFFFF << $(( 32 - ${cidr_mask} )) ))
# Apply netmask to both the subnet IP and the given IP address
ip_addr_subnet=$(( netmask & $(ip_to_int ${ip_addr}) ))
subnet=$(( netmask & $(ip_to_int ${subnet_ip}) ))
# Subnet IPs will match if given IP address is in CIDR subnet
[ "${ip_addr_subnet}" == "${subnet}" ] && rval=0 || rval=1
return $rval
}
function ip_to_int {
local readonly ip_addr="${1}"
local ip_1 ip_2 ip_3 ip_4
ip_1=$(echo "${ip_addr}" | cut -d'.' -f1)
ip_2=$(echo "${ip_addr}" | cut -d'.' -f2)
ip_3=$(echo "${ip_addr}" | cut -d'.' -f3)
ip_4=$(echo "${ip_addr}" | cut -d'.' -f4)
echo $(( ip_1 * 256**3 + ip_2 * 256**2 + ip_3 * 256 + ip_4 ))
}