简体   繁体   English

在数据库中的IP地址比较

[英]Ip address compare in database

I am trying to solve a problem that could compare 2 columns in a table. 我试图解决一个可以比较表中的2列的问题。 the table is as follows 表格如下

------------------------------------------
|  from    |    to           |  Country  |
------------------------------------------
| 25.0.0.1 | 25.255.255.255  |  denmark  |
------------------------------------------
| 68.0.0.1 | 68.255.255.255  |  USA      |

My problem is i have a ip of 25.195.32.0 and i want to compare this to the from and to column and return the country name . 我的问题是我有一个25.195.32.0的IP,我想将它与fromto列进行比较并返回country name

You can use INET_ATON to get a numeric value of the IPs to compare. 您可以使用INET_ATON获取要比较的IP的数值。

SELECT country FROM table
WHERE INET_ATON('25.195.32.0') BETWEEN INET_ATON(from) AND INET_ATON(to)

http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet-aton http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet-aton

根据您使用ANDOR您可以找到使用IN()尝试条件

WHERE from IN('your_ip') OR to IN('your_ip')

Transform IP into integer, then compare integer is simple. 将IP转换为整数,然后比较整数很简单。 MySQL offers the inet_aton function to do so. MySQL提供inet_aton函数来执行此操作。

select Country from table_name where inet_aton($ip) between inet_aton(`from`) and inet_aton(`ip`);

For performance, you should transform column from , to to integer manully. 为了提高性能,您应该将列fromto为整数。 eg. 例如。 add to columns from_n , to_n . 添加到from_nto_n列。 then your SQL will be like this: 然后你的SQL将是这样的:

select Country from table_name where inet_aton($ip) between `from_n` and `to_n`

If you are not using MySQL, you should transform $ip into integer $ip_n first (using something like Python's socket.inet_aton ), then replace inet_aton($ip) with $ip_n . 如果你不使用MySQL,你应该首先将$ip转换为整数$ ip_n(使用类似Python的socket.inet_aton ),然后用$ip_n替换inet_aton($ip)

select * from db.table

Then in php (assuming you've run the quest and stored results): 然后在php中(假设您运行了任务并存储了结果):

$lookupIP;

for($i=0;$i<$db->getRowCount();$i++) {
    $partsFROM = explode(".", $FROM);
    $partsTO = explode(".", $TO);
    $partsLOOKUP = explode(".",$lookupIP);
    if(
        $partsLOOKUP[0] >= $partsFROM[0] && $partsLOOKUP[0] <= $partsTO[0]  
     && $partsLOOKUP[1] >= $partsFROM[1] && $partsLOOKUP[1] <= $partsTO[1]  
     && etc..
    ) return $Country
}

Not particularly efficient or elegant but you get the idea. 不是特别有效或优雅,但你明白了。

As i interpret from the question, you can use some mask and get the base ip address and then compare it. 正如我从问题中解释的那样,您可以使用一些掩码并获取基本IP地址然后进行比较。 To use the mask and get base ip, you need to learn something about different classes (A, B, C, D) used from addressing. 要使用掩码并获取基本ip,您需要了解有关寻址中使用的不同类(A,B,C,D)的信息。

Refer this link http://www.subnet-calculator.com/ 请参阅此链接http://www.subnet-calculator.com/

INET_ATON gives desired solution on IPV4 addresses. INET_ATONIPV4地址提供所需的解决方案。

Apart from that, you can also try HEX version of the IP to compare in between. 除此之外,您还可以尝试使用HEX版本的IP进行比较。

SELECT  HEX( input_ip_value ) 
BETWEEN HEX( from_ip_column ) 
    AND HEX( to_ip_column )

Example : 示例

mysql> select @i:=hex('25.195.32.0'),
    -> @f:=hex('25.0.0.1'),
    -> @t:=hex('25.255.255.255'),
    -> @i between @f and @t is_between;
+------------------------+---------------------+------------------------------+------------+
| @i:=hex('25.195.32.0') | @f:=hex('25.0.0.1') | @t:=hex('25.255.255.255')    | is_between |
+------------------------+---------------------+------------------------------+------------+
| 32352E3139352E33322E30 | 32352E302E302E31    | 32352E3235352E3235352E323535 |          1 |
+------------------------+---------------------+------------------------------+------------+

Select a range: 选择范围:

SELECT Country FROM table_name WHERE ip_address >= from AND to <= ip_address

If you can use a script (eg php) you could select the entire table and make a mask to the IP. 如果你可以使用脚本(例如php),你可以选择整个表并为IP制作一个掩码。 Something like: 就像是:

We can know what belongs to the network address 192.168.129.3/18 ip as follows: 我们可以知道什么属于网络地址192.168.129.3/18 ip如下:

 ip_en_binario = decbin (ip2long ("192.168.129.3")); mascara_en_binario = decbin (ip2long ("255.255.192.0")); resultado_en_binario $ = $ & $ mascara_en_binario ip_en_binario; miss long2ip (bindec ($ resultado_en_binario)); 

The result returned us this script is 192.168.128.0, which is the network address to which the IP belongs 192.168.129.3/18. 结果返回给我们这个脚本是192.168.128.0,这是IP所属的网络地址192.168.129.3/18。

Then, you can do a 'SELECT' of your network with "from" and "to" fields. 然后,您可以使用“从”和“到”字段对网络进行“选择”。

使用此查询

select Country from table_name where from=to;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM