- basics working
- still need to post
This commit is contained in:
parent
a468c1138d
commit
7aae5d688d
2 changed files with 44 additions and 22 deletions
|
|
@ -5,7 +5,9 @@ Multi-source traceroute with geolocation information. Demo: [IP Address Lookup](
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
Save traceroute.py into a directory with its path stored in your PYTHONPATH environment variable.
|
1. Install dependencies listed in requirements.txt file. Pip is recommended. You could also use virtualenv to keep things isolated.
|
||||||
|
2. Save traceroute.py into a directory with its path stored in your PYTHONPATH environment variable. (if using virtualenv, copy it here)
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ import urllib2
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
import requests
|
import requests
|
||||||
import netifaces
|
import netifaces
|
||||||
|
import time
|
||||||
|
|
||||||
USER_AGENT = "traceroute/1.0 (+https://github.com/ayeowch/traceroute)"
|
USER_AGENT = "traceroute/1.0 (+https://github.com/ayeowch/traceroute)"
|
||||||
|
|
||||||
|
|
@ -35,25 +36,29 @@ class Traceroute(object):
|
||||||
json_file = open("sources.json", "r").read()
|
json_file = open("sources.json", "r").read()
|
||||||
sources = json.loads(json_file.replace("_IP_ADDRESS_", ip_address))
|
sources = json.loads(json_file.replace("_IP_ADDRESS_", ip_address))
|
||||||
self.source = sources[country]
|
self.source = sources[country]
|
||||||
|
self.tmp_dir = tmp_dir
|
||||||
|
|
||||||
|
self.no_geo = no_geo
|
||||||
|
self.timeout = timeout
|
||||||
|
self.debug = debug
|
||||||
|
self.locations = {}
|
||||||
|
self.hops = []
|
||||||
self.country = country
|
self.country = country
|
||||||
|
|
||||||
if self.country == 'LO':
|
if self.country == 'LO':
|
||||||
self.local_mode = True
|
self.local_mode = True
|
||||||
self.pub_ip = self._lookup_public_ip()
|
self.pub_ip = self._lookup_public_ip()
|
||||||
self.ifaces = self._get_network_interface_info()
|
self.ifaces = self._get_network_interface_info()
|
||||||
|
self.routes = self._get_network_routes()
|
||||||
else:
|
else:
|
||||||
self.local_mode = False
|
self.local_mode = False
|
||||||
|
|
||||||
self.tmp_dir = tmp_dir
|
|
||||||
self.no_geo = no_geo
|
|
||||||
self.timeout = timeout
|
|
||||||
self.debug = debug
|
|
||||||
self.locations = {}
|
|
||||||
self.hops = []
|
|
||||||
|
|
||||||
self._initialize()
|
self.probe_start = time.time() * 1000
|
||||||
|
self._run_traceroute()
|
||||||
|
self.probe_end = time.time() * 1000
|
||||||
|
|
||||||
def _initialize(self):
|
def _run_traceroute(self):
|
||||||
"""
|
"""
|
||||||
Instead of running the actual traceroute command, we will fetch
|
Instead of running the actual traceroute command, we will fetch
|
||||||
standard traceroute results from several publicly available webpages
|
standard traceroute results from several publicly available webpages
|
||||||
|
|
@ -88,14 +93,17 @@ class Traceroute(object):
|
||||||
self.hops = map(lambda h: {h.pop("hop_num") : h}, self.hops)
|
self.hops = map(lambda h: {h.pop("hop_num") : h}, self.hops)
|
||||||
|
|
||||||
def get_report(self):
|
def get_report(self):
|
||||||
report_structure = {}
|
report = {}
|
||||||
|
|
||||||
report_structure['hops'] = self.hops
|
report['hops'] = self.hops
|
||||||
|
report['probe_start'] = self.probe_start
|
||||||
|
report['probe_end'] = self.probe_end
|
||||||
if self.local_mode:
|
if self.local_mode:
|
||||||
report_structure['pub_ip'] = self.pub_ip
|
report['pub_ip'] = self.pub_ip
|
||||||
report_structure['ifaces'] = self.ifaces
|
report['ifaces'] = self.ifaces
|
||||||
|
report['routes'] = self.routes
|
||||||
|
|
||||||
return report_structure
|
return report
|
||||||
|
|
||||||
def _lookup_public_ip(self):
|
def _lookup_public_ip(self):
|
||||||
|
|
||||||
|
|
@ -116,17 +124,27 @@ class Traceroute(object):
|
||||||
for i in netifaces.interfaces():
|
for i in netifaces.interfaces():
|
||||||
addr = netifaces.ifaddresses(i)
|
addr = netifaces.ifaddresses(i)
|
||||||
|
|
||||||
# only retrieve interfaces that are active
|
try:
|
||||||
if netifaces.AF_INET in addr.keys():
|
|
||||||
iface_list.append( {i : {
|
iface_list.append( {i : {
|
||||||
'ip_address' : addr[netifaces.AF_INET][0]['addr'],
|
'ip_address' : addr[netifaces.AF_INET][0]['addr'],
|
||||||
'mac' : addr[netifaces.AF_LINK][0]['addr']
|
'mac' : addr[netifaces.AF_LINK][0]['addr']
|
||||||
}})
|
}})
|
||||||
|
except KeyError,e:
|
||||||
|
self.print_debug("Key not found - _get_network_interface_info - {}".format(addr))
|
||||||
|
|
||||||
return iface_list
|
return iface_list
|
||||||
|
|
||||||
|
|
||||||
|
def _get_network_routes(self):
|
||||||
|
routes = []
|
||||||
|
|
||||||
|
gw = netifaces.gateways()
|
||||||
|
if 'default' in gw.keys():
|
||||||
|
routes.append( {
|
||||||
|
'default' : gw['default'][netifaces.AF_INET]
|
||||||
|
})
|
||||||
|
|
||||||
|
return routes
|
||||||
|
|
||||||
|
|
||||||
def get_traceroute_output(self):
|
def get_traceroute_output(self):
|
||||||
|
|
@ -150,6 +168,8 @@ class Traceroute(object):
|
||||||
traceroute = re.findall(pattern, content)[0].strip()
|
traceroute = re.findall(pattern, content)[0].strip()
|
||||||
return (status_code, traceroute)
|
return (status_code, traceroute)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _get_hops(self, traceroute):
|
def _get_hops(self, traceroute):
|
||||||
"""
|
"""
|
||||||
Returns hops from traceroute output in an array of dicts each
|
Returns hops from traceroute output in an array of dicts each
|
||||||
|
|
@ -339,6 +359,7 @@ def main():
|
||||||
cmdparser.add_option(
|
cmdparser.add_option(
|
||||||
"-d", "--debug", action="store_true", default=False,
|
"-d", "--debug", action="store_true", default=False,
|
||||||
help="Show debug output (default: False)")
|
help="Show debug output (default: False)")
|
||||||
|
|
||||||
options, _ = cmdparser.parse_args()
|
options, _ = cmdparser.parse_args()
|
||||||
json_file = open(options.json_file, "r").read()
|
json_file = open(options.json_file, "r").read()
|
||||||
sources = json.loads(json_file.replace("_IP_ADDRESS_", options.ip_address))
|
sources = json.loads(json_file.replace("_IP_ADDRESS_", options.ip_address))
|
||||||
|
|
@ -364,7 +385,6 @@ def main():
|
||||||
|
|
||||||
report = traceroute.get_report()
|
report = traceroute.get_report()
|
||||||
|
|
||||||
|
|
||||||
print(json.dumps(report, indent=4))
|
print(json.dumps(report, indent=4))
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue