zoukankan      html  css  js  c++  java
  • 【Python Network】权威域名服务器级联查询

    如果PyDNS库,跳过本地名称服务器,直接向权威名称服务器查询。如baidu.com查询.com域名服务器,并从各个域名服务器中查询下一即域名,并输出相关信息。

    #! /usr/bin/env python
    # Expanded DNS library example - Chapter 4 - DNSany.py
    
    import sys, DNS
    
    def hierquery(qstring, qtype):
        """Given a query type qtype, returns answers of that type for lookup
            qstring. If no answers are found, removes the most specific component
            (the part before the leftmost period) and retries the query with the result.
            If the topmost query fails, returns None. """
        reqobj = DNS.Request()
        try:
            answerobj = reqobj.req(name=qstring, qtype=qtype)
            answers = [x['data'] for x in answerobj.answers if x['type']==qtype]
        except DNS.Base.DNSError:
            answers = []
        if len(answers):
            return answers
        else:
            remainder = qstring.split(".", 1);
            if len(remainder) == 1:
                return None
            else:
                return hierquery(remainder[1], qtype)
    
    def findnameservers(hostname):
        """Attempts to determine the authoritative nameservers for a given
            hostname, Returns None on failure. """
        return hierquery(hostname, DNS.Type.NS)
    
    def getrecordsfromnameserver(qstring, qtype, nslist):
        """Given a list of nameservers in nslist, executes the query requested
            by qstring and qtype on each in order, returning the data from the
            first server that returned 1 or more answers. If no server returned
            any answers, return []. """
        for ns in nslist:
            reqobj = DNS.Request(server = ns)
            try:
                answers = reqobj.req(name=qstring, qtype=qtype).answers
                if len(answers):
                    return answers
            except DNS.Base.DNSError:
                pass
            return []
    
    def nslookup(qstring, qtype, verbose = -1):
        nslist = findnameservers(qstring)
        if nslist is None:
            raise RuntimeError, "Could not find nameserver to use."
        if verbose:
            print "Using nameservers:", ", ".join(nslist)
        return getrecordsfromnameserver(qstring, qtype, nslist)
    
    if __name__ == "__main__":
        query = sys.argv[1]
        DNS.DiscoverNameServers()
    
        answers = nslookup(query, DNS.Type.ANY)
        if not len(answers):
            print "Not found."
        for item in answers:
            print "%-5s %s" % (item['typename'], item['data'])

    运行结果如图所示:

  • 相关阅读:
    spring中的切入点
    spring AOP的基本概念
    sql server 批量修改数据表和存储过程的所有者(2000)或架构(2005)
    SQL SERVER 修改欄位(列)屬性或是名稱
    sql server 數據庫備份與還原 登錄名丟失 解決辦法
    开心农场给我们的20条人生启示
    sql建立遠端聯接
    windows无法存取指定的装置、路径或文件案
    Vs.net 2008 最搞笑的問題
    女儿长大了!
  • 原文地址:https://www.cnblogs.com/bombe1013/p/3590743.html
Copyright © 2011-2022 走看看