From 38e96acc583be2d6ac1787af703eea5b72d67b56 Mon Sep 17 00:00:00 2001 From: ayeowch Date: Sat, 13 Jul 2013 16:42:40 +1000 Subject: [PATCH] sources.json --- LICENSE | 20 ++++++++ README.md | 133 ++++++++++++++++++++++++++++++-------------------- sources.json | 26 ++++++++++ traceroute.py | 71 +++++++-------------------- 4 files changed, 143 insertions(+), 107 deletions(-) create mode 100644 LICENSE create mode 100644 sources.json diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f900e1e --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2013 Addy Yeow Chin Heng + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index caac410..56bf389 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,10 @@ Save traceroute.py into a directory with its path stored in your PYTHONPATH envi Try the following from your Python interpreter: >>> from traceroute import Traceroute - >>> traceroute = Traceroute(ip_address='8.8.8.8') + >>> traceroute = Traceroute("8.8.8.8") >>> hops = traceroute.traceroute() >>> hops - [{'hostname': 'gigagate1', 'longitude': -74.6597, 'rtt': '0.700 ms', 'hop_num': 1, 'latitude': 40.3756, 'ip_address': '128.112.128.114'}, {'hostname': 'vgate1', 'longitude': -74.6597, 'rtt': '0.375 ms', 'hop_num': 2, 'latitude': 40.3756, 'ip_address': '128.112.12.22'}, {'hostname': 'te-1-4-3998-pe01.philadelphia.pa.ibone.comcast.net', 'longitude': -75.3697, 'rtt': '18.296 ms', 'hop_num': 3, 'latitude': 40.3265, 'ip_address': '24.104.128.89'}, {'hostname': 'te-0-1-0-12-cr01.ashburn.va.ibone.comcast.net', 'longitude': -97.0, 'rtt': '28.446 ms', 'hop_num': 4, 'latitude': 38.0, 'ip_address': '68.86.84.177'}, {'hostname': 'pos-0-2-0-0-pe01.ashburn.va.ibone.comcast.net', 'longitude': -97.0, 'rtt': '21.968 ms', 'hop_num': 5, 'latitude': 38.0, 'ip_address': '68.86.86.70'}, {'hostname': '75.149.231.62', 'longitude': -97.0, 'rtt': '20.685 ms', 'hop_num': 6, 'latitude': 38.0, 'ip_address': '75.149.231.62'}, {'hostname': '209.85.252.80', 'longitude': -122.0574, 'rtt': '18.383 ms', 'hop_num': 7, 'latitude': 37.4192, 'ip_address': '209.85.252.80'}, {'hostname': '209.85.252.46', 'longitude': -122.0574, 'rtt': '18.541 ms', 'hop_num': 7, 'latitude': 37.4192, 'ip_address': '209.85.252.46'}, {'hostname': '209.85.252.80', 'longitude': -122.0574, 'rtt': '19.723 ms', 'hop_num': 7, 'latitude': 37.4192, 'ip_address': '209.85.252.80'}, {'hostname': '72.14.238.82', 'longitude': -122.0574, 'rtt': '15.124 ms', 'hop_num': 9, 'latitude': 37.4192, 'ip_address': '72.14.238.82'}, {'hostname': '72.14.238.16', 'longitude': -122.0574, 'rtt': '15.564 ms', 'hop_num': 9, 'latitude': 37.4192, 'ip_address': '72.14.238.16'}, {'hostname': '216.239.49.149', 'longitude': -122.0574, 'rtt': '23.253 ms', 'hop_num': 10, 'latitude': 37.4192, 'ip_address': '216.239.49.149'}, {'hostname': 'google-public-dns-a.google.com', 'longitude': -122.0574, 'rtt': '13.020 ms', 'hop_num': 11, 'latitude': 37.4192, 'ip_address': '8.8.8.8'}] + [{'hostname': 'gigagate1', 'longitude': -74.6597, 'rtt': '0.913 ms', 'hop_num': 1, 'latitude': 40.3756, 'ip_address': '128.112.128.114'}, {'hostname': 'vgate1', 'longitude': -74.6597, 'rtt': '0.498 ms', 'hop_num': 2, 'latitude': 40.3756, 'ip_address': '128.112.12.22'}, {'hostname': '38.122.150.1', 'longitude': -74.006, 'rtt': '4.087 ms', 'hop_num': 3, 'latitude': 40.7143, 'ip_address': '38.122.150.1'}, {'hostname': 'te4-3.ccr01.phl01.atlas.cogentco.com', 'longitude': -97.0, 'rtt': '3.469 ms', 'hop_num': 4, 'latitude': 38.0, 'ip_address': '66.28.4.233'}, {'hostname': 'te7-3.ccr01.phl01.atlas.cogentco.com', 'longitude': -97.0, 'rtt': '3.586 ms', 'hop_num': 4, 'latitude': 38.0, 'ip_address': '154.54.0.189'}, {'hostname': 'te4-1.ccr01.bwi01.atlas.cogentco.com', 'longitude': -97.0, 'rtt': '6.470 ms', 'hop_num': 5, 'latitude': 38.0, 'ip_address': '154.54.2.173'}, {'hostname': 'te7-2.ccr01.bwi01.atlas.cogentco.com', 'longitude': -97.0, 'rtt': '6.821 ms', 'hop_num': 5, 'latitude': 38.0, 'ip_address': '154.54.83.222'}, {'hostname': 'te4-1.ccr01.bwi01.atlas.cogentco.com', 'longitude': -97.0, 'rtt': '20.586 ms', 'hop_num': 5, 'latitude': 38.0, 'ip_address': '154.54.2.173'}, {'hostname': '38.122.63.14', 'longitude': -83.1763, 'rtt': '34.924 ms', 'hop_num': 8, 'latitude': 42.3223, 'ip_address': '38.122.63.14'}, {'hostname': '38.122.62.74', 'longitude': -95.3877, 'rtt': '8.072 ms', 'hop_num': 8, 'latitude': 29.6144, 'ip_address': '38.122.62.74'}, {'hostname': '38.122.63.14', 'longitude': -83.1763, 'rtt': '8.950 ms', 'hop_num': 8, 'latitude': 42.3223, 'ip_address': '38.122.63.14'}, {'hostname': '209.85.252.80', 'longitude': -122.0574, 'rtt': '20.619 ms', 'hop_num': 9, 'latitude': 37.4192, 'ip_address': '209.85.252.80'}, {'hostname': '209.85.252.46', 'longitude': -122.0574, 'rtt': '13.415 ms', 'hop_num': 9, 'latitude': 37.4192, 'ip_address': '209.85.252.46'}, {'hostname': '72.14.234.65', 'longitude': -74.006, 'rtt': '15.676 ms', 'hop_num': 12, 'latitude': 40.7143, 'ip_address': '72.14.234.65'}, {'hostname': '72.14.234.53', 'longitude': -122.0574, 'rtt': '16.844 ms', 'hop_num': 12, 'latitude': 37.4192, 'ip_address': '72.14.234.53'}, {'hostname': 'google-public-dns-a.google.com', 'longitude': -97.0, 'rtt': '16.960 ms', 'hop_num': 14, 'latitude': 38.0, 'ip_address': '8.8.8.8'}] >>> You can also run the script directly by passing in the --ip_address option: @@ -24,10 +24,11 @@ You can also run the script directly by passing in the --ip_address option: Usage: traceroute.py --ip_address=IP_ADDRESS Options: - --version show program's version number and exit -h, --help show this help message and exit -i IP_ADDRESS, --ip_address=IP_ADDRESS IP address of destination host (default: 8.8.8.8) + -j JSON_FILE, --json_file=JSON_FILE + List of sources in JSON file (default: sources.json) -c COUNTRY, --country=COUNTRY Traceroute will be initiated from this country; choose 'LO' for localhost to run traceroute locally, 'BY' for @@ -47,7 +48,7 @@ You can also run the script directly by passing in the --ip_address option: { "hostname": "gigagate1", "longitude": -74.6597, - "rtt": "0.700 ms", + "rtt": "0.913 ms", "hop_num": 1, "latitude": 40.3756, "ip_address": "128.112.128.114" @@ -55,97 +56,121 @@ You can also run the script directly by passing in the --ip_address option: { "hostname": "vgate1", "longitude": -74.6597, - "rtt": "0.375 ms", + "rtt": "0.498 ms", "hop_num": 2, "latitude": 40.3756, "ip_address": "128.112.12.22" }, { - "hostname": "te-1-4-3998-pe01.philadelphia.pa.ibone.comcast.net", - "longitude": -75.3697, - "rtt": "18.296 ms", + "hostname": "38.122.150.1", + "longitude": -74.006, + "rtt": "4.087 ms", "hop_num": 3, - "latitude": 40.3265, - "ip_address": "24.104.128.89" + "latitude": 40.7143, + "ip_address": "38.122.150.1" }, { - "hostname": "te-0-1-0-12-cr01.ashburn.va.ibone.comcast.net", + "hostname": "te4-3.ccr01.phl01.atlas.cogentco.com", "longitude": -97.0, - "rtt": "28.446 ms", + "rtt": "3.469 ms", "hop_num": 4, "latitude": 38.0, - "ip_address": "68.86.84.177" + "ip_address": "66.28.4.233" }, { - "hostname": "pos-0-2-0-0-pe01.ashburn.va.ibone.comcast.net", + "hostname": "te7-3.ccr01.phl01.atlas.cogentco.com", "longitude": -97.0, - "rtt": "21.968 ms", + "rtt": "3.586 ms", + "hop_num": 4, + "latitude": 38.0, + "ip_address": "154.54.0.189" + }, + { + "hostname": "te4-1.ccr01.bwi01.atlas.cogentco.com", + "longitude": -97.0, + "rtt": "6.470 ms", "hop_num": 5, "latitude": 38.0, - "ip_address": "68.86.86.70" + "ip_address": "154.54.2.173" }, { - "hostname": "75.149.231.62", + "hostname": "te7-2.ccr01.bwi01.atlas.cogentco.com", "longitude": -97.0, - "rtt": "20.685 ms", - "hop_num": 6, + "rtt": "6.821 ms", + "hop_num": 5, "latitude": 38.0, - "ip_address": "75.149.231.62" + "ip_address": "154.54.83.222" + }, + { + "hostname": "te4-1.ccr01.bwi01.atlas.cogentco.com", + "longitude": -97.0, + "rtt": "20.586 ms", + "hop_num": 5, + "latitude": 38.0, + "ip_address": "154.54.2.173" + }, + { + "hostname": "38.122.63.14", + "longitude": -83.1763, + "rtt": "34.924 ms", + "hop_num": 8, + "latitude": 42.3223, + "ip_address": "38.122.63.14" + }, + { + "hostname": "38.122.62.74", + "longitude": -95.3877, + "rtt": "8.072 ms", + "hop_num": 8, + "latitude": 29.6144, + "ip_address": "38.122.62.74" + }, + { + "hostname": "38.122.63.14", + "longitude": -83.1763, + "rtt": "8.950 ms", + "hop_num": 8, + "latitude": 42.3223, + "ip_address": "38.122.63.14" }, { "hostname": "209.85.252.80", "longitude": -122.0574, - "rtt": "18.383 ms", - "hop_num": 7, + "rtt": "20.619 ms", + "hop_num": 9, "latitude": 37.4192, "ip_address": "209.85.252.80" }, { "hostname": "209.85.252.46", "longitude": -122.0574, - "rtt": "18.541 ms", - "hop_num": 7, + "rtt": "13.415 ms", + "hop_num": 9, "latitude": 37.4192, "ip_address": "209.85.252.46" }, { - "hostname": "209.85.252.80", - "longitude": -122.0574, - "rtt": "19.723 ms", - "hop_num": 7, - "latitude": 37.4192, - "ip_address": "209.85.252.80" + "hostname": "72.14.234.65", + "longitude": -74.006, + "rtt": "15.676 ms", + "hop_num": 12, + "latitude": 40.7143, + "ip_address": "72.14.234.65" }, { - "hostname": "72.14.238.82", + "hostname": "72.14.234.53", "longitude": -122.0574, - "rtt": "15.124 ms", - "hop_num": 9, + "rtt": "16.844 ms", + "hop_num": 12, "latitude": 37.4192, - "ip_address": "72.14.238.82" - }, - { - "hostname": "72.14.238.16", - "longitude": -122.0574, - "rtt": "15.564 ms", - "hop_num": 9, - "latitude": 37.4192, - "ip_address": "72.14.238.16" - }, - { - "hostname": "216.239.49.149", - "longitude": -122.0574, - "rtt": "23.253 ms", - "hop_num": 10, - "latitude": 37.4192, - "ip_address": "216.239.49.149" + "ip_address": "72.14.234.53" }, { "hostname": "google-public-dns-a.google.com", - "longitude": -122.0574, - "rtt": "13.020 ms", - "hop_num": 11, - "latitude": 37.4192, + "longitude": -97.0, + "rtt": "16.960 ms", + "hop_num": 14, + "latitude": 38.0, "ip_address": "8.8.8.8" } ] diff --git a/sources.json b/sources.json new file mode 100644 index 0000000..a0d616c --- /dev/null +++ b/sources.json @@ -0,0 +1,26 @@ +{ + "LO": { + "url": "traceroute _IP_ADDRESS_" + }, + "BY": { + "url": "http://by104.activeby.net/lg/", + "post_data": {"method": "trace", "host": "_IP_ADDRESS_"} + }, + "CH": { + "url": "http://www.switch.ch/cgi-bin/network/nph-traceroute-opencms?destination=_IP_ADDRESS_" + }, + "JP": { + "url": "http://www.harenet.ad.jp/cgi-bin/harenet/traceroute/traceroute.cgi", + "post_data": {"host": "_IP_ADDRESS_"} + }, + "RU": { + "url": "http://traceroute.rusnet.ru/?_IP_ADDRESS_" + }, + "UK": { + "url": "http://ab.newnet.co.uk/cgi-bin/traceroute?_IP_ADDRESS_" + }, + "US": { + "url": "http://www.net.princeton.edu/cgi-bin/traceroute.pl", + "post_data": {"target": "_IP_ADDRESS_"} + } +} diff --git a/traceroute.py b/traceroute.py index 8e2ed8c..6686718 100644 --- a/traceroute.py +++ b/traceroute.py @@ -46,55 +46,17 @@ class Traceroute(object): """ Multi-source traceroute instance. """ - def __init__(self, ip_address="8.8.8.8", country="US", tmp_dir="/tmp", - no_geo=False, timeout=120, debug=False): + def __init__(self, ip_address, source="sources.json", country="US", + tmp_dir="/tmp", no_geo=False, timeout=120, debug=False): super(Traceroute, self).__init__() self.ip_address = ip_address + self.source = source self.country = country self.tmp_dir = tmp_dir self.no_geo = no_geo self.timeout = timeout self.debug = debug - - # Traceroute servers from traceroute.org - sources = { - 'LO': { - 'url': "traceroute {}".format(ip_address), - }, - 'BY': { - 'url': "http://by104.activeby.net/lg/", - 'post_data': { - 'method': "trace", - 'host': ip_address, - }, - }, - 'CH': { - 'url': ("http://www.switch.ch/cgi-bin/network/nph-traceroute" - "-opencms?destination={}".format(ip_address)), - }, - 'JP': { - 'url': ("http://www.harenet.ad.jp/cgi-bin/harenet/traceroute/" - "traceroute.cgi"), - 'post_data': { - 'host': ip_address, - }, - }, - 'RU': { - 'url': "http://traceroute.rusnet.ru/?{}".format(ip_address) - }, - 'UK': { - 'url': ("http://ab.newnet.co.uk/cgi-bin/traceroute?{}".format( - ip_address)), - }, - 'US': { - 'url': "http://www.net.princeton.edu/cgi-bin/traceroute.pl", - 'post_data': { - 'target': ip_address, - }, - }, - } - self.source = sources[self.country] - self.locations = {} # Geo-coded locations + self.locations = {} def traceroute(self): """ @@ -318,6 +280,9 @@ def main(): cmdparser.add_option( "-i", "--ip_address", type="string", default="8.8.8.8", help="IP address of destination host (default: 8.8.8.8)") + cmdparser.add_option( + "-j", "--json_file", type="string", default="sources.json", + help="List of sources in JSON file (default: sources.json)") cmdparser.add_option( "-c", "--country", type="choice", default="US", choices=["LO", "BY", "CH", "JP", "RU", "UK", "US"], @@ -339,17 +304,17 @@ def main(): "-d", "--debug", action="store_true", default=False, help="Show debug output (default: False)") options, _ = cmdparser.parse_args() - if options.ip_address: - traceroute = Traceroute(ip_address=options.ip_address, - country=options.country, - tmp_dir=options.tmp_dir, - no_geo=options.no_geo, - timeout=options.timeout, - debug=options.debug) - hops = traceroute.traceroute() - print(json.dumps(hops, indent=4)) - else: - cmdparser.print_usage() + json_file = open(options.json_file, "r").read() + sources = json.loads(json_file.replace("_IP_ADDRESS_", options.ip_address)) + traceroute = Traceroute(ip_address=options.ip_address, + source=sources[options.country], + country=options.country, + tmp_dir=options.tmp_dir, + no_geo=options.no_geo, + timeout=options.timeout, + debug=options.debug) + hops = traceroute.traceroute() + print(json.dumps(hops, indent=4)) return 0