Handle root permission requirement on certain platforms

also few a handful of nasty bugs
This commit is contained in:
Babak Farrokhi 2016-04-12 12:07:24 +04:30
parent 8892984c2d
commit 77df91f18f
Signed by: farrokhi
GPG Key ID: 6B267AD85D632E9A

View File

@ -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: