子网范围划分算法 [英] The algorithm for dividing the range of subnet

查看:156
本文介绍了子网范围划分算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个有趣的算法,可以划分子网范围。



我有一个子网,例如192.168.1.0/24或192.168.1.248/22,等等。我们知道'/ 24'或'/ 22'代表网络,'(32-24)'或'(32 -22)'代表主机,所以我想使用192.168.1.0/24划分子网分为两个范围,第一个是192.168.1.1-192.168.1.127,第二个是192.168.1.128-192.168.1.254。



所以我需要一种算法来解析使用python将给定的苹果酒子网划分为两个范围,输入是cidr子网,输出是两个ip范围,下面是一个示例:

 输入:192.168.1.0/24 
输出:[{'start_address':'192.168.0.1','end_address':'192.168.0.127'},
{'start_address': '192.168.0.128','end_address':'192.168.0.245'}]

因此,如果您有

解决方案

您想要一个算法,还是想要一个解决方案? / p>

有关Python 3中的解决方案,请使用 ipaddress 模块。在 PyPI 中提供了对Python 2的支持。



如果需要算法,则可以阅读模块的代码。



使用 ipaddress 模块:

  import ipaddress 
network = ipaddress.ip_network( '192.168.1.0/24')
主机=列表(network.hosts())
子网= [{'start_address':str(list(subnet.hosts())[0]),
'end_address':str(list(subnet.hosts())[-1])}
用于network.subnets()中的子网
打印(子网)

输出

 
[{'start_address':'192.168.1.1','end_address':'192.168.1.126'},{'start_address':'192.168.1.129','end_address':'192.168.1.254'}]

哪个与您想要的东西非常接近。
丢失了第一个子网的广播地址192.168.1.127,第二个子网的网络地址192.168.1.128和第二个子网的广播地址192.168.1.255。



要获得准确的输出,可以拆分主机列表:

  hosts = list(network.hosts ())
子网= [{'start_address':str(子网[0]),'end_address':str(子网[-1])}
for(hosts [:len(hosts )// 2],hosts [len(hosts // 2:])]
打印(子网)

输出

 
[{'start_address':'192.168.1.1','end_address ':'192.168.1.127'},{'start_address':'192.168.1.128','end_address':'192.168.1.254'}]


There is a interesting algorithm, wrt dividing the range of subnet.

I have a subnet,such as 192.168.1.0/24 or 192.168.1.248/22, and so on. And we know that '/24' or '/22' stands for networks and '(32 - 24)' or '(32 -22)' stands for hosts.So Using 192.168.1.0/24, I want to divide the subnet into two ranges, the first is 192.168.1.1 - 192.168.1.127, the second is 192.168.1.128 - 192.168.1.254.

So I need a algorithm to resolve which can divide a given cider subnet into two range by using python.The input is cidr subnet, and the output is two ip ranges.Here is a example:

Input : 192.168.1.0/24
Output: [{'start_address':'192.168.0.1','end_address':'192.168.0.127'},    
{'start_address':'192.168.0.128','end_address':'192.168.0.245'}]

So if you have a wonderful algorithm for this, please show it here.

解决方案

Do you want an algorithm, or do you want a solution?

For a solution, in Python 3, use the ipaddress module. There is a backport to Python 2 available in PyPI.

If you want the algorithm, you can read the code for the module.

Using the ipaddress module:

import ipaddress
network = ipaddress.ip_network('192.168.1.0/24')
hosts = list(network.hosts())
subnets = [{'start_address': str(list(subnet.hosts())[0]),
            'end_address': str(list(subnet.hosts())[-1])}
                   for subnet in network.subnets()]
print(subnets)

Output

[{'start_address': '192.168.1.1', 'end_address': '192.168.1.126'}, {'start_address': '192.168.1.129', 'end_address': '192.168.1.254'}]

Which is pretty close to what you want. Missing is 192.168.1.127 which is the broadcast address for the first subnet, 192.168.1.128 which is the network address for the second subnet, and 192.168.1.255 which is the broadcast address for the second subnet.

To achieve the exact output you can split the hosts list:

hosts = list(network.hosts())
subnets = [{'start_address': str(subnet[0]), 'end_address': str(subnet[-1])} 
               for subnet in (hosts[:len(hosts)//2], hosts[len(hosts)//2:])]
print(subnets)

Output

[{'start_address': '192.168.1.1', 'end_address': '192.168.1.127'}, {'start_address': '192.168.1.128', 'end_address': '192.168.1.254'}]

这篇关于子网范围划分算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆