The algorithm for dividing the range of subnet

2024/11/20 20:36:08

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.

Answer

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'}]
https://en.xdnf.cn/q/118373.html

Related Q&A

Look if a string starts with the ending characters of another string?

I want to see if ending of one string is similar to starting of another stringif i have a string a="12345678" and b="56789" i want to update a as 123456789these two strings are in …

Dict and List Manipulation Python

I have two files one has key and other has both key and value. I have to match the key of file one and pull the corresponding value from file two. When all the key and value are in plain column format …

Python - input of file path

this code works fine when I put the path of the file myself. but when I want to get it from users raw_input() it doesnt work. what can I do?import string import randomprint "enter number between …

libmproxy and mitmproxy documentation

I am new to the mitmproxy world. I need to write a python script that would log all the requests made from a certain app on Genymotion emulator. Now, I learned that mitmproxy can be helpful for my requ…

Printing mutiple HTML tables using tabulate in python

I want to produce two HTML tables using tabulate package, but I am only able to produce one table and send mail.Is it possible to put more than one html table into a message sent with smtplib and email…

Why am I getting presentation error in python list?

My code is following: d = [int(d) for d in input().split()]l = [] c = 1 for i in range(len(d)):if d[i] not in l:l.append(d[i])c += 1for i in range(len(l)):print(l[i], end=" ")Evaluation: Inpu…

Calculating BLEU score between two sumamries in python

prediction = "I am ABC. I have completed my bachelors degree in computer application at XYZ University and I am currently pursuing my masters degree in computer application through distance educat…

Pivot a dataframe with duplicate values in Index

I have a pandas dataframe like thissnapDate instance waitEvent AvgWaitInMs 0 2015-Jul-03 XX gc cr block 3-way 1 1 2015-Jun-29 YY gc current b…

Concatenate .txt files with same names in different folders with python

I have two folders containing many text files with matching file names. So I am concatenating folder1/file1.txt with folder2.file1.txt. My current code appends data from folder2/file1 to folder2/file1 …

Importing module via another module

In module A, I import module B. Then, in module C, I import module A. In module C, will I be able to use the content of module B implicitly via the import of module A, or will I have to explicitly impo…