Tagged with: trace attribution
Neither Windows nor Linux system Traceroute commands can show attribution, and in actual network maintenance, the attribution addresses of these trace routes are also important information to help us locate where the problem occurs. The following is a script written by Python to show the place of attribution. Also convenient for their own memory and future use.
#!/usr/bin/pythonimport sysimport osimport reimport urllib2import subprocessimport platformdef getlocation (ipaddr): url = "http://www.ip138.com/ ips138.asp?ip=%s&action=2 " % ipaddr u = urllib2.urlopen (URL) s = u.read () #Get IP Address ip = Re.findall (R ' \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} ', s) #Get IP Address Location result = re.findall (R ' (<li>.*?</li>) ', s) location = result[0][16:-5].decode (' GBK ') return locationif len (SYS.ARGV) < 2: print "Usage: %s {hostname|ip}" % sys.argv[0] sys.exit () else: host = sys.argv[1]system_name = platform.dist () [0]if system_name == ' Redhat ': trace_cmd = '/bin/traceroute ' elif system_name == ' Ubuntu ': trace_cmd = '/usr/sbin/traceroute ' try: p = subprocess. Popen ([Trace_cmd,host],stdout=subprocess. PIPE) while True: line = P.stdout.readline () if not line: break if re.match ("^.[ 0-9].* (. *). * ", line): try: ip = line.split ( ' (') [1].split (') ') [0] &Nbsp; print line,getlocation (IP) except IndexError,e: print line, else: print line,except (keyboardinterrupt,systemexit): sys.exit ()
Because of their network environment in the system is not added to the decision to Windows, follow-up can be added by other friends. In addition I have also written a version of Windows. Welcome to communicate with everyone
This article from "Technology for the King" blog, declined reprint!
Python version trace command displays attribution to