Check if ipv6 is inside range(检查 ipv6 是否在范围内)
问题描述
我有 maxmind 的 ipv6 数据.这是我当前的表格(带有示例数据):
I have the maxmind's ipv6 data. Here's my current table (with sample data):
+---------------+------------+
| network | geoname_id |
+---------------+------------+
| 2001:208::/32 | 123 |
| 2001:218::/32 | 4312 |
+---------------+------------+
使用 他们的转换器,我可以创建一个 network_start_ip
和 network_last_ip
列:
Using their converter, I can create a network_start_ip
and network_last_ip
columns:
+------------------+----------------------------------------+------------+
| network_start_ip | network_last_ip | geoname_id |
+------------------+----------------------------------------+------------+
| 2001:200:: | 2001:200:ffff:ffff:ffff:ffff:ffff:ffff | 123 |
| 2001:208:: | 2001:208:ffff:ffff:ffff:ffff:ffff:ffff | 4312 |
+------------------+----------------------------------------+------------+
我期待这样的事情会起作用(即使它可能会比其他方法慢):
I was expecting that something like this would work (even though it would probably be slower than other methods):
SELECT b.geoname_id FROM blocks b
WHERE HEX(INET6_ATON('2001:201:ffff:ffff:ffff:ffff:ffff:ffff')) BETWEEN HEX(b.network_start_ip) AND HEX(b.network_last_ip)
那么,我错过了什么?另外,存储 ipv6 地址(范围)的最佳方式是什么
So, what am I missing? Also, what is the best way to store ipv6 addresses (ranges)
谢谢
推荐答案
这是我的工作方式:
- 创建了另一个具有相同列的表,但
network_start_ip
和network_last_ip
是VARBINARY(16)
- 使用以下语句填充该表:
INSERT INTO 块SELECT INET6_ATON(b2.network_start_ip), INET6_ATON(b2.network_last_ip), b2.geoname_id FROM blocks_copy b2;
- 然后,要检查 IPv6 地址是否在范围内,我只需要运行以下查询:
SELECT geoname_id FROM blocks bWHERE INET6_ATON('2a01:4ff:ffff:ffff::ffff') 在 b.network_start_ip 和 b.network_last_ip 之间
- Created another table with the same columns, but
network_start_ip
andnetwork_last_ip
areVARBINARY(16)
- Populated that table with this statement:
INSERT INTO blocks SELECT INET6_ATON(b2.network_start_ip), INET6_ATON(b2.network_last_ip), b2.geoname_id FROM blocks_copy b2;
- Then, to check if the IPv6 address is in the range, I just need to run this query:
SELECT geoname_id FROM blocks b WHERE INET6_ATON('2a01:4ff:ffff:ffff::ffff') BETWEEN b.network_start_ip AND b.network_last_ip
这篇关于检查 ipv6 是否在范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检查 ipv6 是否在范围内


基础教程推荐
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01