last time, late modifications...
This commit is contained in:
parent
929a3523ba
commit
5ae26df1a1
25
test.py
25
test.py
@ -7,10 +7,11 @@ from sys import hash_info
|
|||||||
import pprint
|
import pprint
|
||||||
|
|
||||||
def run_traceroute(host):
|
def run_traceroute(host):
|
||||||
|
timestamp = datetime.now().timestamp()
|
||||||
result = subprocess.run(['traceroute', host], stdout=subprocess.PIPE)
|
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')
|
lines = output.strip().split('\n')
|
||||||
hops = []
|
hops = []
|
||||||
@ -20,7 +21,7 @@ def parse_traceroute_output(output):
|
|||||||
target = output.strip().split('\n')[0].split()[2]
|
target = output.strip().split('\n')[0].split()[2]
|
||||||
|
|
||||||
for line in lines[1:]:
|
for line in lines[1:]:
|
||||||
hop = []
|
hop = {}
|
||||||
hop_info = line.split()
|
hop_info = line.split()
|
||||||
hop_number = int(hop_info[0])
|
hop_number = int(hop_info[0])
|
||||||
hop_name = None
|
hop_name = None
|
||||||
@ -43,17 +44,14 @@ def parse_traceroute_output(output):
|
|||||||
|
|
||||||
# If first colum is either name or ip-address
|
# If first colum is either name or ip-address
|
||||||
if count == 1:
|
if count == 1:
|
||||||
print(part)
|
|
||||||
match = re.search(ip_regex, part)
|
match = re.search(ip_regex, part)
|
||||||
if match:
|
if match:
|
||||||
hop_ip = part.strip('()')
|
hop_ip = part.strip('()')
|
||||||
else:
|
else:
|
||||||
print('do ever here?')
|
|
||||||
hop_name = part
|
hop_name = part
|
||||||
|
|
||||||
# Second colum is ip-address first latency reading
|
# Second colum is ip-address first latency reading
|
||||||
if count == 2:
|
if count == 2:
|
||||||
print(part)
|
|
||||||
if re.search(ip_regex, part):
|
if re.search(ip_regex, part):
|
||||||
try:
|
try:
|
||||||
_ip = ipaddress.ip_address(part.strip('()'))
|
_ip = ipaddress.ip_address(part.strip('()'))
|
||||||
@ -65,7 +63,6 @@ def parse_traceroute_output(output):
|
|||||||
# reruns of the hop_name and hop_ip...
|
# reruns of the hop_name and hop_ip...
|
||||||
# We only need the latency floats anymore.
|
# We only need the latency floats anymore.
|
||||||
else:
|
else:
|
||||||
print(part)
|
|
||||||
try:
|
try:
|
||||||
latency = float(part)
|
latency = float(part)
|
||||||
latencies.append(latency)
|
latencies.append(latency)
|
||||||
@ -74,12 +71,12 @@ def parse_traceroute_output(output):
|
|||||||
|
|
||||||
hop_latency = sum(latencies) / len(latencies) if latencies else None
|
hop_latency = sum(latencies) / len(latencies) if latencies else None
|
||||||
|
|
||||||
|
hop['timestamp'] = timestamp
|
||||||
hop.append(hop_number)
|
hop['hop_number'] = hop_number
|
||||||
if not hop_name == None:
|
if not hop_name == None:
|
||||||
hop.append(hop_name)
|
hop['hop_name'] = hop_name
|
||||||
hop.append(hop_ip)
|
hop['hop_ip'] = hop_ip
|
||||||
hop.append(hop_latency)
|
hop['hop_latency'] = hop_latency
|
||||||
|
|
||||||
|
|
||||||
hops.append(hop)
|
hops.append(hop)
|
||||||
@ -89,8 +86,8 @@ def parse_traceroute_output(output):
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
target='8.8.8.8'
|
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)
|
print('>> '+target)
|
||||||
pprint.pprint(hops)
|
pprint.pprint(hops)
|
||||||
|
BIN
traceroute.db
Normal file
BIN
traceroute.db
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user