Intial commit. Some stuff I made last night.

This commit is contained in:
kalzu rekku 2024-05-26 13:07:49 +03:00
parent 1984e7e511
commit 93e785c2ae
3 changed files with 243 additions and 0 deletions

50
netx_path_test.py Normal file
View File

@ -0,0 +1,50 @@
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()

126
traceroute_collector.py Executable file
View File

@ -0,0 +1,126 @@
#!/usr/bin/env python3.11
import subprocess
import sqlite3
import re
from datetime import datetime
def run_traceroute(host):
result = subprocess.run(['traceroute', host], stdout=subprocess.PIPE)
return result.stdout.decode()
def parse_traceroute_output(output):
hops = []
lines = output.strip().split('\n')[1:] # Skip the first line (traceroute to ...)
previous_ip = None
for line in lines:
parts = line.split()
hop_number = int(parts[0])
ip_address = parts[1]
latencies = []
for part in parts[2:]:
if re.match(r'^[0-9+\s+ms$]', part):
latency_str = re.sub(r'[^0-9.]', '', part)
if latency_str and not latency_str == '':
try:
print(part)
print(latency_str)
latencies.append(float(latency_str))
except ValueError:
print(f"Could not convert '{latency_str}' to float.")
avg_latency = sum(latencies) / len(latencies) if latencies else None
timestamp = datetime.now().isoformat()
if previous_ip:
hops.append({
'hop_number': hop_number,
'source_ip': previous_ip,
'destination_ip': ip_address,
'latency': avg_latency,
'timestamp': timestamp,
})
previous_ip = ip_address
return hops
def create_tables():
conn = sqlite3.connect('traceroute.db')
cursor = conn.cursor()
# Table to store unique links
cursor.execute('''
CREATE TABLE IF NOT EXISTS links (
id INTEGER PRIMARY KEY,
source_ip TEXT,
destination_ip TEXT,
UNIQUE(source_ip, destination_ip)
)
''')
# Table to store latency and timestamp for each link
cursor.execute('''
CREATE TABLE IF NOT EXISTS link_latency (
id INTEGER PRIMARY KEY,
link_id INTEGER,
latency REAL,
timestamp TEXT,
FOREIGN KEY (link_id) REFERENCES links (id)
)
''')
conn.commit()
conn.close()
def store_traceroute(hops):
conn = sqlite3.connect('traceroute.db')
cursor = conn.cursor()
for hop in hops:
# Insert or ignore link into links table
cursor.execute('''
INSERT OR IGNORE INTO links (source_ip, destination_ip)
VALUES (?, ?)
''', (hop['source_ip'], hop['destination_ip']))
# Retrieve the link_id
cursor.execute('''
SELECT id FROM links WHERE source_ip = ? AND destination_ip = ?
''', (hop['source_ip'], hop['destination_ip']))
link_id = cursor.fetchone()[0]
# Insert latency data into link_latency table
cursor.execute('''
INSERT INTO link_latency (link_id, latency, timestamp)
VALUES (?, ?, ?)
''', (link_id, hop['latency'], hop['timestamp']))
conn.commit()
conn.close()
def retrieve_traceroute():
conn = sqlite3.connect('traceroute.db')
cursor = conn.cursor()
cursor.execute('''
SELECT l.source_ip, l.destination_ip, ll.latency, ll.timestamp
FROM link_latency ll
JOIN links l ON ll.link_id = l.id
ORDER BY ll.timestamp
''')
rows = cursor.fetchall()
conn.close()
return rows
# Usage
if __name__ == '__main__':
create_tables()
traceroute_output = run_traceroute('vi.fi')
hops = parse_traceroute_output(traceroute_output)
# for hop in hops:
# print(hop)
store_traceroute(hops)
stored_hops = retrieve_traceroute()
for hop in stored_hops:
print(f"Link: {hop[0]} -> {hop[1]}, Latency: {hop[2]} ms, Timestamp: {hop[3]}")
exit(0)

67
traceroute_visualization.py Executable file
View File

@ -0,0 +1,67 @@
import sqlite3
import networkx as nx
from pyvis.network import Network
import matplotlib.pyplot as plt
from datetime import datetime
def retrieve_traceroute_data():
conn = sqlite3.connect('traceroute.db')
cursor = conn.cursor()
cursor.execute('''
SELECT l.source_ip, l.destination_ip, ll.latency, ll.timestamp
FROM link_latency ll
JOIN links l ON ll.link_id = l.id
ORDER BY ll.timestamp
''')
rows = cursor.fetchall()
conn.close()
return rows
def build_graph(data):
G = nx.DiGraph() # Create a directed graph
for row in data:
source_ip = row[0]
destination_ip = row[1]
latency = row[2]
timestamp = row[3]
# Add nodes and edges to the graph
G.add_node(source_ip)
G.add_node(destination_ip)
G.add_edge(source_ip, destination_ip, latency=latency, timestamp=timestamp)
return G
#def visualize_graph_pyvis(G, output_file='network.html'):
# net = Network(height='750px', width='100%', directed=True)
# net.from_nx(G)
#
# for edge in G.edges(data=True):
# src, dst, data = edge
# latency = data['latency']
# timestamp = data['timestamp']
# net.add_edge(src, dst, title=f'Latency: {latency} ms<br>Timestamp: {timestamp}', value=latency)
#
# net.show(output_file)
def main():
# Retrieve data from the database
traceroute_data = retrieve_traceroute_data()
# Build the network graph
graph = build_graph(traceroute_data)
return graph
if __name__ == '__main__':
graph = main()
nx.draw_planar(graph, with_labels=True)
plt.savefig("path.png")
# Visualize the graph using pyvis
#visualize_graph_pyvis(graph, 'traceroute_network.html')
exit(0)