NW engineer tried to aggregate addresses with python netaddr

Premise

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

Preparation

netaddr installation

Launch Anaconda Prompt > pip install netaddr

netaddr installation (local material)

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

How to use

1. Address aggregation

> 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)

output

[IPNetwork('10.0.0.0/8'), IPNetwork('192.168.0.0/23')]

2. Address sorting (ascending order)

> 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)

output

[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)

output

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)

output

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

reference

https://netaddr.readthedocs.io/en/latest/index.html

Recommended Posts

NW engineer tried to aggregate addresses with python netaddr
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I tried to automate sushi making with python
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I tried to draw a route map with Python
I tried to solve the soma cube with python
I tried to get started with blender python script_Part 02
I tried to implement an artificial perceptron with python
I tried to automatically generate a password with Python3
I tried to solve the problem with Python Vol.1
I tried to analyze J League data with Python
I tried to solve AOJ's number theory with Python
I tried to aggregate & compare unit price data by language with Real Gachi by Python
I tried fp-growth with python
I tried scraping with Python
Connect to BigQuery with Python
Connect to Wikipedia with Python
Post to slack with Python 3
Switch python to 2.7 with alternatives
Write to csv with Python
I tried gRPC with Python
I tried scraping with python
I tried to find the entropy of the image with python
I tried to simulate how the infection spreads with Python
I tried to make various "dummy data" with Python faker
I tried various methods to send Japanese mail with Python
[Ipdb] Web development beginners tried to summarize debugging with Python
[Python] I tried to visualize tweets about Corona with WordCloud
Mayungo's Python Learning Episode 3: I tried to print numbers with print
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried to divide the file into folders with Python
[5th] I tried to make a certain authenticator-like tool with python
I tried to solve the ant book beginner's edition with python
Python: How to use async with
Link to get started with python
Rubyist tried to make a simple API with Python + bottle + MySQL
I tried to touch Python (installation)
[Python] Write to csv file with Python
[2nd] I tried to make a certain authenticator-like tool with python
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
Output to csv file with Python
I tried web scraping with python.
[3rd] I tried to make a certain authenticator-like tool with python
[Python] A memo that I tried to get started with asyncio
I tried to create a list of prime numbers with python
Convert list to DataFrame with python
MP3 to WAV conversion with Python
To do tail recursion with Python2
How to get started with Python
[Pandas] I tried to analyze sales data with Python [For beginners]
I tried to fix "I tried stochastic simulation of bingo game with Python"
I tried to make a periodical process with Selenium and Python
I tried to find out if ReDoS is possible with Python
What to do with PYTHON release?
I tried to make a 2channel post notification application with Python
Unable to install Python with pyenv
How to use FTP with Python