Find Out If an Ip Is Within a Range of Ips

Find out if an IP is within a range of IPs

>> require "ipaddr"
=> true
>> low = IPAddr.new("62.0.0.0").to_i
=> 1040187392
>> high = IPAddr.new("62.255.255.255").to_i
=> 1056964607
>> ip = IPAddr.new("62.156.244.13").to_i
=> 1050473485
>> (low..high)===ip
=> true

If you are given the network instead of the start and end addresses, it is even simpler

>> net = IPAddr.new("62.0.0.0/8")
=> #<IPAddr: IPv4:62.0.0.0/255.0.0.0>
>> net===IPAddr.new("62.156.244.13")
=> true

IPAddr will also work with IPv6 addresses

>> low = IPAddr.new('1::')
=> #<IPAddr: IPv6:0001:0000:0000:0000:0000:0000:0000:0000/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
>> high = IPAddr.new('2::')
=> #<IPAddr: IPv6:0002:0000:0000:0000:0000:0000:0000:0000/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
>> (low..high)===IPAddr.new('1::1')
=> true

How to check an IP address is within a range of two IPs in Python?

For changing the ip format so that we can compare

def convert_ipv4(ip):
return tuple(int(n) for n in ip.split('.'))

This function will return True or False if the ip is within range or not

def check_ipv4_in(addr, start, end):
return convert_ipv4(start) < convert_ipv4(addr) < convert_ipv4(end)

ip_range = ('1.1.0.0', '1.1.255.255')

print(check_ipv4_in('1.1.99.99', *ip_range))

It will print 'true' in this case as the function returns True in this case

How to check an IP address is within a range of two IPs in PHP?

With ip2long() it's easy to convert your addresses to numbers. After this, you just have to check if the number is in range:

if ($ip <= $high_ip && $low_ip <= $ip) {
echo "in range";
}

Checking if IP address lies within a given range

You are comparing strings values (which compare lexicographically) and not the int values for every IP, make them into ints with comprehensions:

min_ip = [int(i) for i in mask.split(' - ')[0].split('.')]
max_ip = [int(i) for i in mask.split(' - ')[1].split('.')]
ip = [int(i) for i in IP.split('.')]

It's also best if you don't perform the split twice for every ip in mask; split before hand:

s = mask.split(' - ')
min_ip, max_ip = [[int(i) for i in j.split('.')] for j in s]

Your for loop could arguably be better if you used enumerate with ip:

for ind, val in enumerate(ip):
if val < min_ip[ind] or val > max_ip[ind]:
return False

Checking if IP is in a range in MYSQL

You need to use INET_ATON() to convert IPv4 address format to the unsigned integer equivalent.

Start by calculating the starting address and the ending address based on the CIDR notation.

mysql> SELECT INET_ATON(SUBSTRING_INDEX(range, '/', 1)) AS start_address, 
POWER(2, 32-SUBSTRING_INDEX(range, '/', -1))-1 AS num_addresses
FROM ipranges;
+---------------+---------------+
| start_address | num_addresses |
+---------------+---------------+
| 16777216 | 255 |
+---------------+---------------+

That result is when I tested this query against '1.0.0.0/24'.

Then you can check if the INET_ATON() of the IP address you're interested in falls in the range, because it's simply an integer range.

SELECT range FROM (
SELECT range,
INET_ATON(SUBSTRING_INDEX(range, '/', 1)) AS start_address,
POWER(2, 32-SUBSTRING_INDEX(range, '/', -1))-1 AS num_addresses
FROM ipranges) AS t
WHERE INET_ATON('195.124.199.201') BETWEEN start_address AND start_address+num_addresses;

I'm afraid with all these expressions, it's not possible for the SQL query to be optimized with indexes. If you need this to run with high performance against a large data set of IP ranges, you need to think of a way to store the data differently so you can index it.

But if you only have a small number of IP ranges, even an unindexable query may nevertheless be fast enough.

DB | Postgres | How to check if IP is in a list of IPs or a range

I think this does what you want:

select t.*
from t
where '1.1.1.1'::inet <<= any(regexp_split_to_array(t.ips, '@##@')::inet[])

Here is a db<>fiddle.

How to check if a given ip falls between a given ip range using node js

This is quite easy if we convert the ips to simple numbers:

function IPtoNum(ip){
return Number(
ip.split(".")
.map(d => ("000"+d).substr(-3) )
.join("")
);
}

Then we can check a certain range as:

 if( IPtoNum(min) < IPtoNum(val) &&    IPtoNum(max) > IPtoNum(val) ) alert("in range");

That can also be applied to a table:

const ranges = [
["..41", "192.168.45"],
["123.124.125"," 126.124.123"]
];

const ip = "125.12.125";
const inRange = ranges.some(
([min,max]) => IPtoNum(min) < IPtoNum(ip) && IPtoNum(max) > IPtoNum(ip)
);

Check if user's IP address is in a range of IP's

The idea is to split the IP and check every component separately.

mask = "26.83.152.12-192"
IP = "26.83.152.19"
def match(mask, IP):
splitted_IP = IP.split('.')
for index, current_range in enumerate(mask.split('.')):
if '-' in current_range:
mini, maxi = map(int,current_range.split('-'))
else:
mini = maxi = int(current_range)
if not (mini <= int(splitted_IP[index]) <= maxi):
return False
return True

How to see if an IP address belongs inside of a range of IPs using CIDR notation?

Decided to answer my own question so people can benefit. If it can be improved, please do!

I used the IPNetwork library and it worked out fantastically!

nuget install IPNetwork2

Below is the code I used:

using System.Net;

public static class RedirectHelpers
{
public static bool IpIsWithinBoliviaRange(string ip)
{
IPAddress incomingIp = IPAddress.Parse(ip);
foreach (var subnet in Bolivia_Ip_Range)
{
IPNetwork network = IPNetwork.Parse(subnet);

if (IPNetwork.Contains(network, incomingIp))
return true;
}
return false;
}

private static List<string> Bolivia_Ip_Range = new List<string>()
{
"12.144.86.0/23",
"31.201.1.176/30",
"46.36.198.101/32",
"46.36.198.102/31",
"46.36.198.104/31",
"46.136.172.0/24",
"63.65.11.0/24",
"63.65.12.0/25",
"63.65.12.128/26",
"63.65.12.192/27",
"63.65.12.224/28",
"63.65.12.240/29",
"63.65.12.248/30",
"63.65.12.252/31",
"63.65.12.254/32",
"65.173.56.0/21",
"67.23.241.179/32",
"67.23.241.180/30",
"67.23.241.184/29",
"67.23.241.192/30",
"67.23.241.196/31",
"67.23.241.198/32",
"72.32.164.56/29",
"72.46.244.32/28",
"74.91.16.48/29",
"74.91.16.208/29",
"74.91.20.48/28",
"74.91.20.64/29",
"74.112.134.120/29",
"74.112.135.104/29",
"74.205.37.16/29",
"78.24.205.32/28",
"98.129.27.88/29",
"98.129.91.40/29",
"166.114.0.0/16",
"167.157.0.0/16",
"174.143.165.80/29",
"186.0.156.0/22",
"186.2.0.0/17",
"186.27.0.0/17",
"190.0.248.0/21",
"190.3.184.0/21",
"166.114.0.0/16",
"167.157.0.0/16",
"186.2.0.0/18",
"190.11.64.0/20",
"190.11.80.0/20",
"190.103.64.0/20",
"190.104.0.0/19",
"190.107.32.0/20",
"190.129.0.0/17",
"190.181.0.0/18",
"190.186.0.0/18",
"190.186.64.0/18",
"190.186.128.0/18",
"200.7.160.0/20",
"200.58.64.0/20",
"200.58.80.0/20",
"200.58.160.0/20",
"200.58.176.0/20",
"200.75.160.0/20",
"200.85.128.0/20",
"200.87.0.0/17",
"200.87.128.0/17",
"200.105.128.0/19",
"200.105.160.0/19",
"200.105.192.0/19",
"200.112.192.0/20",
"200.119.192.0/20",
"200.119.208.0/20",
"201.222.64.0/19",
"201.222.96.0/19"
};
}


Related Topics



Leave a reply



Submit