basile10 / Plotcodes

0 stars 0 forks source link

Code #1

Open basile10 opened 3 months ago

basile10 commented 3 months ago

import ipaddress import networkx as nx import matplotlib.pyplot as plt import pandas as pd

Read data from CSV

df = pd.read_csv('network_locations.csv')

Create a graph

G = nx.DiGraph()

Parse CIDR blocks and add to the graph with location names

for _, row in df.iterrows(): location = row['Location'] cidr = row['CIDR'] G.add_node(location, cidr=ipaddress.ip_network(cidr))

Identify relationships (subset/superset)

for loc1 in G.nodes: for loc2 in G.nodes: if loc1 != loc2: network1 = G.nodes[loc1]['cidr'] network2 = G.nodes[loc2]['cidr'] if network1.supernet_of(network2): G.add_edge(loc1, loc2, relation='superset') elif network1.subnet_of(network2): G.add_edge(loc2, loc1, relation='subset')

Draw the graph

pos = nx.spring_layout(G) # Layout for visualization plt.figure(figsize=(12, 10)) nx.draw(G, pos, with_labels=True, node_size=3000, node_color="skyblue", font_size=10, font_weight="bold") edge_labels = nx.get_edge_attributes(G, 'relation') nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels) plt.title("CIDR Blocks Overlap and Superset/Subset Relationships") plt.show()

basile10 commented 2 weeks ago

`

`