Using Python script to implement domain name resolution and attribution information query

Source: Internet
Author: User
Tags get ip

First, preface

Because sometimes in the work to the domain name resolution and obtain the relevant IP attribution information, so write the script, convenient for their own query use.


Ii. contents of the script

#!/usr/bin/env python#coding:utf-8import dns.resolverimport urllibimport chardetimport  sysimport reip_list = []query_domain = raw_input (' Please input a  domain:  ') #  first query whether there is a CNAME, if any, get to a record through CNAME, if not directly get a record, and then return to a list of def get_iplist (domain):     try:        cn = dns.resolver.query ( Domain, ' CNAME ')         for i in cn.response.answer:             for j in i.items:                 cname_domain =  j.to_text ()          print  ' =========================== ============= '         print  '     CNAME   ---> ',  cname_domain        print  ' ======================================== '     except:        pass     Else:        a = dns.resolver.query (Cname_domain, ' A ')          for i in A.response.answer:             for j in i.items:                 try:                     ip_list.append (j.address)                 except:                      get_iplist (str (j)) &NBsp;           try:         a = dns.resolver.query (Domain, ' A ')         for  i in A.response.answer:             for j in i.items:                 ip_list.append (j.address)     except:         pass    return ip_list#  Get IP attribution information by calling Ip138.com def get_ Ipbelong (domain_ip):    url =  ' http://ip138.com/ips138.asp?ip=%s&action=2 '  % domain_ip    content = urllib.urlopen (URL). Read ()      typeencode = sys.getfilesystemencoding ()     infoencode =  Chardet.detect (content). Get (' ENcoding ', ' Utf-8 ')     html = content.decode (infoencode, ' ignore '). Encode ( Typeencode)     ip = re.findall (R ' \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} ', html)      result = re.findall (R ' (<li>.*?</li>) ', HTML)      belong = result[0][14:-5]    print  '%-*s--->%*s '  %  (18,ip[0], 20,belong) if __name__ ==  ' __main__ ':     get_iplist (query_domain)      for ip in ip_list:        get_ Ipbelong (IP)


Third, the effect

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M02/84/54/wKiom1eNn4nRaLF6AAATTGNhCCE573.png "title=" effect "alt = "Wkiom1enn4nralf6aaattgnhcce573.png"/>


Iv. Related Information

Http://www.dnspython.org/examples.html

Http://www.cnblogs.com/CoolRandy/p/3251733.html

This article is from the "Gzguitar" blog, make sure to keep this source http://gzguitar.blog.51cto.com/5406286/1827705

Using Python script to implement domain name resolution and attribution information query

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.