Python ‐ IP expander - dishplate/blog GitHub Wiki

Needs to be tested

import ipaddress
import pandas as pd
import csv

def expand_ip_ranges(ip_ranges):
    expanded_ips = []
    for ip_range in ip_ranges:
        if isinstance(ip_range, tuple):
            start, end = ip_range
            start_ip = ipaddress.ip_address(start.strip())
            end_ip = ipaddress.ip_address(end.strip())
            for ip_int in range(int(start_ip), int(end_ip) + 1):
                expanded_ips.append(str(ipaddress.ip_address(ip_int)))
        else:
            expanded_ips.append(ip_range.strip())
    return expanded_ips

def read_ip_ranges_from_csv(file_path):
    with open(file_path, 'r') as file:
        ip_ranges = file.read().split(', ')
    return ip_ranges

def read_ip_ranges_from_excel(file_path):
    df = pd.read_excel(file_path)
    ip_ranges = []
    for index, row in df.iterrows():
        ip_ranges.append((row['Range_Begin'], row['Range_End']))
    return ip_ranges

def expand_and_flatten(ip_ranges):
    expanded_ips = []
    for ip_range in ip_ranges:
        if isinstance(ip_range, tuple):
            start, end = ip_range
            start_ip = ipaddress.ip_address(start.strip())
            end_ip = ipaddress.ip_address(end.strip())
            for ip_int in range(int(start_ip), int(end_ip) + 1):
                expanded_ips.append(str(ipaddress.ip_address(ip_int)))
        else:
            expanded_ips.append(ip_range.strip())
    return expanded_ips

csv_file_path = 'ip_ranges.csv'
excel_file_path = 'ip_ranges.xlsx'

csv_ip_ranges = read_ip_ranges_from_csv(csv_file_path)
excel_ip_ranges = read_ip_ranges_from_excel(excel_file_path)

expanded_csv_ips = expand_ip_ranges(csv_ip_ranges)
expanded_excel_ips = expand_ip_ranges(excel_ip_ranges)

csv_set = set(expanded_csv_ips)
excel_set = set(expanded_excel_ips)

missing_in_excel = list(csv_set - excel_set)
missing_in_csv = list(excel_set - csv_set)

# Expand IP ranges for differences
expanded_missing_in_excel = expand_ip_ranges(missing_in_excel)
expanded_missing_in_csv = expand_ip_ranges(missing_in_csv)

# Write differences to a CSV file
with open('differences.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(["IP addresses missing in Excel", "IP addresses missing in CSV"])
    for excel_ip, csv_ip in zip(expanded_missing_in_excel, expanded_missing_in_csv):
        writer.writerow([excel_ip, csv_ip])

print("Differences written to differences.csv file.")