Python version trace command displays attribution to

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.