From c5b0acd87b4141ffd95055411e711befefdd959e Mon Sep 17 00:00:00 2001 From: Brian Martin Date: Wed, 8 Jul 2015 22:33:50 -0400 Subject: [PATCH] - testing webhook --- README.md | 3 ++- traceroute.py | 24 +++++++++++++----------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 37101cc..946d8c6 100644 --- a/README.md +++ b/README.md @@ -15,11 +15,12 @@ Try the following from your Python interpreter: >>> from traceroute import Traceroute >>> traceroute = Traceroute("8.8.8.8") - >>> hops = traceroute.traceroute() + >>> hops = traceroute.hops >>> hops [{'hostname': 'core-87-router', 'longitude': -74.6597, 'rtt': '2.208 ms', 'hop_num': 1, 'latitude': 40.3756, 'ip_address': '128.112.128.2'}, {'hostname': 'border-87-router', 'longitude': -74.6597, 'rtt': '0.422 ms', 'hop_num': 2, 'latitude': 40.3756, 'ip_address': '128.112.12.142'}, {'hostname': 'te0-0-1-1.204.rcr12.phl03.atlas.cogentco.com', 'longitude': -97.0, 'rtt': '3.775 ms', 'hop_num': 3, 'latitude': 38.0, 'ip_address': '38.122.150.1'}, {'hostname': 'te0-0-1-3.rcr21.phl01.atlas.cogentco.com', 'longitude': -97.0, 'rtt': '3.689 ms', 'hop_num': 4, 'latitude': 38.0, 'ip_address': '154.54.27.117'}, {'hostname': 'te0-0-1-3.rcr22.phl01.atlas.cogentco.com', 'longitude': -97.0, 'rtt': '4.340 ms', 'hop_num': 4, 'latitude': 38.0, 'ip_address': '66.28.4.233'}, {'hostname': 'te0-7-0-10.mpd22.dca01.atlas.cogentco.com', 'longitude': -97.0, 'rtt': '8.082 ms', 'hop_num': 5, 'latitude': 38.0, 'ip_address': '154.54.42.101'}, {'hostname': 'te0-7-0-10.ccr21.dca01.atlas.cogentco.com', 'longitude': -97.0, 'rtt': '7.860 ms', 'hop_num': 5, 'latitude': 38.0, 'ip_address': '154.54.42.89'}, {'hostname': 'be2112.ccr41.iad02.atlas.cogentco.com', 'longitude': -97.0, 'rtt': '8.628 ms', 'hop_num': 6, 'latitude': 38.0, 'ip_address': '154.54.5.233'}, {'hostname': '38.88.214.50', 'longitude': -97.0, 'rtt': '8.197 ms', 'hop_num': 7, 'latitude': 38.0, 'ip_address': '38.88.214.50'}, {'hostname': '209.85.255.1', 'longitude': -122.0574, 'rtt': '9.230 ms', 'hop_num': 8, 'latitude': 37.4192, 'ip_address': '209.85.255.1'}, {'hostname': '209.85.251.101', 'longitude': -122.0574, 'rtt': '9.719 ms', 'hop_num': 8, 'latitude': 37.4192, 'ip_address': '209.85.251.101'}, {'hostname': '216.239.51.11', 'longitude': -122.0574, 'rtt': '9.811 ms', 'hop_num': 9, 'latitude': 37.4192, 'ip_address': '216.239.51.11'}, {'hostname': '72.14.238.115', 'longitude': -122.0574, 'rtt': '10.142 ms', 'hop_num': 9, 'latitude': 37.4192, 'ip_address': '72.14.238.115'}, {'hostname': '216.239.51.101', 'longitude': -122.0574, 'rtt': '9.568 ms', 'hop_num': 9, 'latitude': 37.4192, 'ip_address': '216.239.51.101'}, {'hostname': 'google-public-dns-a.google.com', 'longitude': -122.0838, 'rtt': '9.127 ms', 'hop_num': 10, 'latitude': 37.386, 'ip_address': '8.8.8.8'}] >>> + You can also run the script directly by passing in the --ip_address option: $ python traceroute.py --help diff --git a/traceroute.py b/traceroute.py index caecc22..b117305 100755 --- a/traceroute.py +++ b/traceroute.py @@ -22,7 +22,6 @@ import time USER_AGENT = "traceroute/1.0 (+https://github.com/ayeowch/traceroute)" - class Traceroute(object): """ Multi-source traceroute instance. @@ -331,6 +330,10 @@ class Traceroute(object): print("[DEBUG {}] {}".format(datetime.datetime.now(), msg)) +def post_result(webhook_url, report, timeout=120): + return requests.post(webhook_url, data=json.dumps(report), timeout=timeout) + + def main(): cmdparser = optparse.OptionParser("%prog --ip_address=IP_ADDRESS") cmdparser.add_option( @@ -360,6 +363,10 @@ def main(): "-d", "--debug", action="store_true", default=False, help="Show debug output (default: False)") + cmdparser.add_option( + "-w", "--webhook", type="string", default="", + help="Specify URL to POST report payload rather than stdout") + options, _ = cmdparser.parse_args() json_file = open(options.json_file, "r").read() sources = json.loads(json_file.replace("_IP_ADDRESS_", options.ip_address)) @@ -373,19 +380,14 @@ def main(): no_geo=options.no_geo, timeout=options.timeout, debug=options.debug) - """ - Pseudo-Code - report = traceroute.get_report() - - print(json.dumps(report, indent=4) - - """ - - report = traceroute.get_report() - print(json.dumps(report, indent=4)) + if options.webhook != "": + status_code = post_result(options.webhook, report, options.timeout) + print "Status code {}".format(status_code) + else: + print(json.dumps(report, indent=4)) return 0