简体   繁体   English

从IP地址获取子网

[英]Get subnet from IP address

I am trying to get subnet for IP address I have. 我正在尝试为我拥有的IP地址获取子网。

Eg : 例如:

1 1

Subnet mask : 255.255.255.0 子网掩码: 255.255.255.0

Input : 192.178.2.55 输入: 192.178.2.55

Output : 192.178.2.0 输出: 192.178.2.0

2 2

Subnet mask : 255.255.0.0 子网掩码: 255.255.0.0

Input : 192.178.2.55 输入: 192.178.2.55

Output : 192.178.0.0 输出: 192.178.0.0

Currently, the way I do it (for subnet 255.255.255.0) 目前,我的方式(子网255.255.255.0)

ip =  '192.178.2.55'
subnet = '.'.join(ip.split('.')[:2]) + '.0.0'
subnet
'192.178.0.0'

I see python has ipaddress library. 我看到python有ipaddress库。 However I could not find a method that could do the above task. 但是我找不到可以执行上述任务的方法。

Bonus : Since ipaddress supports IPv4 and IPv6, if the same function could be used for both. 奖励:由于ipaddress支持IPv4和IPv6,如果两者都可以使用相同的功能。

The ipaddress.ip_network function can take any of the string formats that IPv4Network and IPv6Network can take, including the address/mask format. ipaddress.ip_network函数可以采用IPv4NetworkIPv6Network可以采用的任何字符串格式,包括address/mask格式。

Since you're not actually passing the network address, but an address within that network with host bits set, you need to use strict=False . 由于您实际上并未传递网络地址,而是设置了主机位的网络中的地址,因此您需要使用strict=False

So: 所以:

>>> net = ipaddress.ip_network('192.178.2.55/255.255.255.0', strict=False)
>>> net
IPv4Network('192.178.2.0/24')
>>> net.network_address
IPv4Address('192.178.2.0')
>>> net.netmask
IPv4Address('255.255.255.0')

Alternatively, you can use ip_interface and extract the network from there: 或者,您可以使用ip_interface并从那里提取网络:

>>> iface = ipaddress.ip_interface('192.178.2.55/255.255.255.0')
>>> iface
IPv4Interface('192.178.2.55/24')
>>> iface.network
IPv4Network('192.178.2.0/24')
>>> iface.netmask
IPv4Address('255.255.255.0')
>>> iface.ip
IPv4Address('192.178.2.55')
>>> iface.network.network_address
IPv4Address('192.178.2.0')

Which of the two you want depends on what exactly you're trying to represent. 你想要的两个中的哪一个取决于你想要代表什么。 Notice that the Interface types are subclasses of the Address types, and of course they remember the original address that was used to construct them, while the Network classes remember the network address; 请注意,接口类型是Address类型的子类,当然它们还记住用于构造它们的原始地址,而Network类则记住网络地址; one of those two is usually the deciding factor. 其中一个通常是决定性因素。

Both of them will, of course, work just as well with IPv6: 当然,它们都可以与IPv6一起工作:

>>> ipaddress.ip_interface('2001:db8::1000/32')
IPv6Interface('2001:db8::1000/32')
>>> ipaddress.ip_interface('2001:db8::1000/32').network.network_address
IPv6Address('2001:db8::')

subnet is simply a binary mask that must be and 'ed bitwise with the IP address. subnet只是一个二进制掩码,必须and IP地址一致。 You can apply the mask yourself: 你可以自己申请面具:

".".join(map(str, [i & m # Apply the mask
          for i,m in zip(map(int, ip.split(".")),
                         map(int, subnet.split(".")))]))
#'192.178.2.0'

This solution works only for IPv4 addresses. 此解决方案仅适用于IPv4地址。

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

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