This article is written by a network engineer who is not good at programming. A guide for address aggregation and sorting without understanding how to use python. It is built in the following environment, but the command is exactly the same when built on Linux
Windows10 64bit Anaconda3 https://www.python.jp/install/anaconda/windows/install.html
Launch Anaconda Prompt > pip install netaddr
Download the material (wheel) at the following site https://pypi.org/ Material: netaddr-0.7.19-py2.py3-none-any.whl (1.6 MB)
Start Anaconda Prompt and move to the location where the materials are stored > cd downloads Install by specifying the file name > pip install netaddr-0.7.19-py2.py3-none-any.whl
> python >>> from netaddr import * >>> ip_list = [ IPNetwork('192.168.1.0/24'), IPNetwork('192.168.0.0/24'), IPNetwork('10.20.30.0/24'), IPNetwork('10.0.0.0/8') ] >>> cidr_merge(ip_list)
[IPNetwork('10.0.0.0/8'), IPNetwork('192.168.0.0/23')]
> python >>> from netaddr import * >>> import random >>> import pprint >>> ip_list = [ IPNetwork('192.168.1.0/24'), IPNetwork('192.168.0.0/24'), IPNetwork('10.20.30.0/24'), IPNetwork('10.0.0.0/8') ] >>> random.shuffle(ip_list) >>> ip_list.sort() >>> pprint.pprint(ip_list)
[IPNetwork('10.0.0.0/8'), IPNetwork('10.20.30.0/24'), IPNetwork('192.168.0.0/24'), IPNetwork('192.168.1.0/24')]
3.mask → prefix > python >>> from netaddr import * >>> ip_list = [ IPNetwork('192.168.24.0/255.255.255.0'), IPNetwork('192.168.24.0/255.255.255.128'), IPNetwork('192.168.24.0/255.255.255.192'), IPNetwork('192.168.24.252/30') ] >>> for i in ip_list: ... print(i) ... (enter)
192.168.24.0/24 192.168.24.0/25 192.168.24.0/26 192.168.24.252/30
4.prefix → mask > python >>> from netaddr import * >>> ip_list = [ IPNetwork('192.168.24.0/255.255.255.0'), IPNetwork('192.168.24.0/255.255.255.128'), IPNetwork('192.168.24.0/255.255.255.192'), IPNetwork('192.168.24.252/30') ] >>> for i in ip_list: ... print(str(i.ip)+'/'+str(i.netmask)) ... (enter)
192.168.24.0/255.255.255.0 192.168.24.0/255.255.255.128 192.168.24.0/255.255.255.192 192.168.24.252/255.255.255.252
https://netaddr.readthedocs.io/en/latest/index.html
Recommended Posts