使用mysql根据IP和子网掩码广播地址
我有以下两列:
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;I am trying to find a way with mysql to grab the actual broadcast range which in this case is
+--------------+-----------------+ | IP | MASK | +--------------+-----------------+ | 10.0.125.130 | 255.255.255.128 | +--------------+-----------------+ 1 row in set (0.00 sec)
10.0.125.255
or 167804415
, but I can't see to find it. closest I gotten isSELECT 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
广播地址是子网掩码的补码,与 IP 地址进行或运算。
(您必须使用“
& 0xffffffff
”进行掩码,因为在 MySQL 中,按位补运算符返回 64 位值。)The broadcast address is the complement of the subnet mask, OR'ed with the IP address.
(You have to mask with "
& 0xffffffff
" because in MySQL, the bitwise complement operator returns a 64-bit value.)