使用mysql根据IP和子网掩码广播地址

发布于 2024-07-13 12:38:39 字数 1345 浏览 7 评论 0原文

我有以下两列:

SELECT  b.ip_address AS IP ,b.mask AS MASK FROM interfaces b WHERE b.ip_address = 167804290;
+-----------+------------+
| IP        | MASK       |
+-----------+------------+
| 167804290 | 4294967168 | 
+-----------+------------+
Where is an actual IP address an its subnet mask
SELECT INET_NTOA(b.ip_address) AS IP,INET_NTOA(b.mask) AS MASK FROM interfaces b WHERE b.ip_address = 167804290;
+--------------+-----------------+ | IP | MASK | +--------------+-----------------+ | 10.0.125.130 | 255.255.255.128 | +--------------+-----------------+ 1 row in set (0.00 sec)
I am trying to find a way with mysql to grab the actual broadcast range which in this case is 10.0.125.255 or 167804415, but I can't see to find it. closest I gotten is

SELECT INET_NTOA(b.ip_address+(POWER(2,32)- b.mask - 1)) FROM interfaces b WHERE b.ip_address = 167804290; 
+---------------------------------------------------+
| INET_NTOA(b.ip_address+(POWER(2,32)- b.mask - 1)) |
+---------------------------------------------------+
| 10.0.126.1                                        | 
+---------------------------------------------------+

唯一的问题是,这是假设列 ip_address 是子网 10.0.125.128 的开头。

任何帮助将不胜感激。

I have the following two columns:


SELECT b.ip_address AS IP ,b.mask AS MASK FROM interfaces b WHERE b.ip_address = 167804290;
+-----------+------------+
| IP | MASK |
+-----------+------------+
| 167804290 | 4294967168 |
+-----------+------------+

Where is an actual IP address an its subnet mask

SELECT INET_NTOA(b.ip_address) AS IP,INET_NTOA(b.mask) AS MASK FROM interfaces b WHERE b.ip_address = 167804290;
+--------------+-----------------+
| IP | MASK |
+--------------+-----------------+
| 10.0.125.130 | 255.255.255.128 |
+--------------+-----------------+
1 row in set (0.00 sec)

I am trying to find a way with mysql to grab the actual broadcast range which in this case is 10.0.125.255 or 167804415, but I can't see to find it.
closest I gotten is


SELECT INET_NTOA(b.ip_address+(POWER(2,32)- b.mask - 1)) FROM interfaces b WHERE b.ip_address = 167804290;
+---------------------------------------------------+
| INET_NTOA(b.ip_address+(POWER(2,32)- b.mask - 1)) |
+---------------------------------------------------+
| 10.0.126.1 |
+---------------------------------------------------+

Only problem with that, is that this is assuming the column ip_address is the start of the subnet 10.0.125.128

Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2024-07-20 12:38:39

广播地址是子网掩码的补码,与 IP 地址进行或运算。

SELECT INET_NTOA( ~b.mask & 0xffffffff | b.ip_address) 
FROM interfaces b 
WHERE b.ip_address = 167804290; 

(您必须使用“& 0xffffffff”进行掩码,因为在 MySQL 中,按位补运算符返回 64 位值。)

The broadcast address is the complement of the subnet mask, OR'ed with the IP address.

SELECT INET_NTOA( ~b.mask & 0xffffffff | b.ip_address) 
FROM interfaces b 
WHERE b.ip_address = 167804290; 

(You have to mask with "& 0xffffffff" because in MySQL, the bitwise complement operator returns a 64-bit value.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文