Compare commits

...

1 Commits

Author SHA1 Message Date
kalzu rekku
5ae26df1a1 last time, late modifications... 2024-06-02 19:16:26 +03:00
2 changed files with 11 additions and 14 deletions

25
test.py
View File

@ -7,10 +7,11 @@ from sys import hash_info
import pprint
def run_traceroute(host):
timestamp = datetime.now().timestamp()
result = subprocess.run(['traceroute', host], stdout=subprocess.PIPE)
return result.stdout.decode()
return result.stdout.decode(), timestamp
def parse_traceroute_output(output):
def parse_traceroute_output(output, timestamp):
lines = output.strip().split('\n')
hops = []
@ -20,7 +21,7 @@ def parse_traceroute_output(output):
target = output.strip().split('\n')[0].split()[2]
for line in lines[1:]:
hop = []
hop = {}
hop_info = line.split()
hop_number = int(hop_info[0])
hop_name = None
@ -43,17 +44,14 @@ def parse_traceroute_output(output):
# If first colum is either name or ip-address
if count == 1:
print(part)
match = re.search(ip_regex, part)
if match:
hop_ip = part.strip('()')
else:
print('do ever here?')
hop_name = part
# Second colum is ip-address first latency reading
if count == 2:
print(part)
if re.search(ip_regex, part):
try:
_ip = ipaddress.ip_address(part.strip('()'))
@ -65,7 +63,6 @@ def parse_traceroute_output(output):
# reruns of the hop_name and hop_ip...
# We only need the latency floats anymore.
else:
print(part)
try:
latency = float(part)
latencies.append(latency)
@ -74,12 +71,12 @@ def parse_traceroute_output(output):
hop_latency = sum(latencies) / len(latencies) if latencies else None
hop.append(hop_number)
hop['timestamp'] = timestamp
hop['hop_number'] = hop_number
if not hop_name == None:
hop.append(hop_name)
hop.append(hop_ip)
hop.append(hop_latency)
hop['hop_name'] = hop_name
hop['hop_ip'] = hop_ip
hop['hop_latency'] = hop_latency
hops.append(hop)
@ -89,8 +86,8 @@ def parse_traceroute_output(output):
if __name__ == '__main__':
target='8.8.8.8'
traceroute_output = run_traceroute(target)
traceroute_output, timestamp = run_traceroute(target)
target, hops = parse_traceroute_output(traceroute_output)
target, hops = parse_traceroute_output(traceroute_output, timestamp)
print('>> '+target)
pprint.pprint(hops)

BIN
traceroute.db Normal file

Binary file not shown.