Skip to main content
  1. Posts/

Merge subnet lists

·80 words·1 min·
netdevops blog python subnet cidr merge
Maximilian Thoma
Author
Maximilian Thoma
network engineer

This is a small python snippet to merge multiple subnet lists mixed with single ip addresses and CIDRs.

netaddr module required, install it with “pip install netaddr”.

Usage: python merge.py merge1.txt merge2.txt etc.

from netaddr import cidr_merge
import sys

if __name__ == "__main__":

    if len(sys.argv) == 1:
        print("No merge files defined. merge.py merge1.txt merge2.txt ...")
        sys.exit(1)

    MERGE_FILES = sys.argv[1:]

    buffer = []

    for MERGE_FILE in MERGE_FILES:
        with open(MERGE_FILE) as mf:
            for line in mf.readlines():
                buffer.append(line.strip())

    for element in cidr_merge(buffer):
        print(element.cidr)

Related

APIFlask Webhook Listener for Netbox
·258 words·2 mins
netdevops blog netbox python api apiflask
This little code snippet is the base of my Netbox Webhook Listener written in APIFlask.
Automation: Netbox sync to CheckMK
·1055 words·5 mins
blog netbox python api check_mk
Script to sync Netbox assets to CheckMK
Ansible: Parse Cisco commando ouputs with TEXTFSM
·390 words·2 mins
netdevops blog cisco textfsm ansible
To parse outputs of commands in Ansible you can use TEXTFSM.