From 77df91f18f9f2338584c3abb03455fc97cbdc1e0 Mon Sep 17 00:00:00 2001 From: Babak Farrokhi Date: Tue, 12 Apr 2016 12:07:24 +0430 Subject: [PATCH] Handle root permission requirement on certain platforms also few a handful of nasty bugs --- dnstraceroute.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/dnstraceroute.py b/dnstraceroute.py index 0cb8965..745dcff 100755 --- a/dnstraceroute.py +++ b/dnstraceroute.py @@ -171,7 +171,17 @@ def main(): if should_stop: break - icmp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, icmp) + # some platforms permit opening a DGRAM socket for ICMP without root permission + # if not availble, we will fall back to RAW which explicitly requires root permission + try: + icmp_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) + except OSError: + try: + icmp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, icmp) + except OSError: + print("Error: Unable to create ICMP socket with unprivileged user. Please run as root.") + exit(1) + icmp_socket.bind(("", dnsport)) icmp_socket.settimeout(timeout) @@ -236,8 +246,13 @@ def main(): if has_whois and as_lookup: ASN = whoisrecord(curr_addr) as_name = '' - if ASN and ASN.asn != "NA": - as_name = "[%s %s] " % (ASN.asn, ASN.owner) + try: + if ASN and ASN.asn != "NA": + as_name = "[%s %s] " % (ASN.asn, ASN.owner) + except AttributeError: + if should_stop: + exit(0) + pass print("%d\t%s (%s) %s%d ms" % (ttl, curr_name, curr_addr, as_name, elapsed)) else: