博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分解查询结果
阅读量:5861 次
发布时间:2019-06-19

本文共 2081 字,大约阅读时间需要 6 分钟。

#!/usr/bin/env python

# DNS query program - Example 4 - DNSquery.py

import sys, DNS, DNSany, re

def getreverse(query):

    """Given the query, returns an appropriate reverse lookup string
    under IN-ADDR.ARPA if query is an IP address; otherwise, returns None.
    This function is not IPv6-compatible."""
    if re.search('^\d+\.\d+\.\d+\.\d+$', query):
        octets = query.split('.')
        octets.reverse()
        return '.'.join(octets) + '.IN-ADDR.ARPA'
    return None

def formatline(index, typename, descr, data):

    retval = "%-2s %-5s" % (index, typename)
    data = data.replace("\n", "\n         ")
    if descr != None and len(descr):
        retval += " %-12s" % (descr + ":")
    return retval + " " + data

DNS.DiscoverNameServers()

queries = [(sys.argv[1], DNS.Type.ANY)]
donequeries = []
descriptions = {'A': 'IP address',
                'TXT': 'Data',
                'PTR': 'Host name',
                'CNAME': 'Alias for',
                'NS': 'Name server'}
              
while len(queries):
    (query, qtype) = queries.pop(0)
    if query in donequeries:
        # Don't look up the same thing twice
        continue
    donequeries.append(query)
    print "-" * 77
    print "Results for %s (lookup type %s)" % \
          (query, DNS.Type.typestr(qtype))
    print
    rev = getreverse(query)
    if rev:
        print "IP address given; doing reverse lookup using", rev
        query = rev
       
    answers = DNSany.nslookup(query, qtype, verbose = 0)
    if not len(answers):
        print "Not found."

    count = 0

    for answer in answers:
        count += 1
        if answer['typename'] == 'MX':
            print formatline(count, answer['typename'],
                             'Mail server',
                             "%s, priority %d" % (answer['data'][1],
                                                  answer['data'][0]))
            queries.append((answer['data'][1], DNS.Type.A))
        elif answer['typename'] == 'SOA':
            data = "\n" + "\n".join([str(x) for x in answer['data']])
            print formatline(count, 'SOA', 'Start of authority', data)
        elif answer['typename'] in descriptions:
            print formatline(count, answer['typename'],
                             descriptions[answer['typename']], answer['data'])
        else:
            print formatline(count, answer['typename'], None,
                             str(answer['data']))
        if answer['typename'] in ['CNAME', 'PTR']:
            queries.append((answer['data'], DNS.Type.ANY))
        if answer['typename'] == 'NS':
            queries.append((answer['data'], DNS.Type.A))

本文来自CSDN博客,转载请标明出处:

你可能感兴趣的文章
Mac软件下载备忘
查看>>
java 泛型初探
查看>>
在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory
查看>>
就是一个表格
查看>>
找回使用Eclipse删除的文件
查看>>
rabbitmq 消息系统 消息队列
查看>>
集成spring3、hibernate4、junit
查看>>
URL与ASCII
查看>>
java读取properties配置文件
查看>>
UITableview中cell重用引起的内容重复的问题
查看>>
Windows7操作系统安装教程(图文)
查看>>
IOS Core Animation Advanced Techniques的学习笔记(三)
查看>>
除了模拟手术教学,VR在医疗领域如何应用?
查看>>
盘点5款Ubuntu监控工具解决CPU暴增问题
查看>>
java 测试IP
查看>>
用CSS做导航菜单的4个理由
查看>>
NOIP2015 运输计划 二分答案+Tarjan LCA+树上差分
查看>>
构建之法读后感
查看>>
基本信息项目目标文档
查看>>
移动开发Html 5前端性能优化指南
查看>>