-
Notifications
You must be signed in to change notification settings - Fork 10
/
bitroute.py
79 lines (59 loc) · 1.89 KB
/
bitroute.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import requests
import shutil
import subprocess
import sys
__all__ = ["bitroute"]
def is_compatible():
""" Checks whether the current machine is capable of running bitroute
Returns:
bool: True if the machine is compatible false if not.
"""
# check for Windows
if hasattr(sys, 'getwindowsversion'):
print('error: Windows is currently not supported.')
return False
# check for command presence
if not shutil.which('traceroute'):
print('error: Missing `traceroute` binary.')
return False
return True
def get_server_info():
"""Gets network metadata for the machine calling the function.
see http://ipinfo.io for more info.
Returns:
dict: A dictionary with keys ip, hostname, city, region, country, loc, org, postal
"""
uri = 'http://ipinfo.io'
raw = requests.get(uri)
data = raw.json()
return data
def bitroute(url):
""" runs traceroute against the url.
Args:
url (str): A url to run traceroute against.
Raises:
ValueError: if the url is malformed or traceroute cannot be performed on it.
Returns:
dict: A dictionary containing traceroute information.
"""
if not is_compatible():
return
uri = url.replace('https://', '').replace('http://', '')
try:
out = subprocess.check_output(['traceroute', str(uri)]).decode('unicode_escape')
except subprocess.CalledProcessError:
raise ValueError("traceroute cannot be performed on url={}".format(uri))
res = [line for line in out.split('\n') if line != '']
info = {
'traceroute': res,
'server': get_server_info()
}
return info
if __name__ == '__main__':
url = sys.argv[1]
data = bitroute(url)
formatted_data = json.dumps(data, indent=4, sort_keys=True)
print(formatted_data)