traceroute_map/app/netx_path_test.py

51 lines
1.3 KiB
Python

import sqlite3
import networkx as nx
import matplotlib.pyplot as plt
import random
import numpy
seed=1
random.seed(seed)
numpy.random.seed(seed)
conn = sqlite3.connect('traceroute.db')
cursor = conn.cursor()
cursor.execute("SELECT source_ip, destination_ip FROM links")
edges = cursor.fetchall()
conn.close()
G = nx.Graph()
for edge in edges:
source_ip, destination_ip = edge
G.add_edge(source_ip, destination_ip)
# Find all connected components (subgraphs) in the graph
subgraphs = [G.subgraph(c).copy() for c in nx.connected_components(G)]
hscale=5
vscale=100
# Create the figure with a larger size
total_nodes = sum(len(subgraph.nodes) for subgraph in subgraphs)
width = total_nodes * hscale / 10 # Adjust the width as needed
height = total_nodes * vscale / 50 # Adjust the height as needed
plt.figure(figsize=(width, height))
# Set aspect ratio to 'equal'
plt.gca().set_aspect('equal', adjustable='datalim')
# Draw each subgraph separately
for subgraph in subgraphs:
nodes = list(subgraph.nodes)
pos = {node: (i * hscale, -i * vscale) for i, node in enumerate(nodes)}
nx.draw(subgraph, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=500, font_size=10)
# Save the figure
plt.savefig("graph003.svg", format="svg")
plt.show()