1. pip install clientsubnetoption (works for both Python2 & Python3)
  2. Import clientsubnetoption and dependancies you\’ll need:

    import dns
    import clientsubnetoption
  3. Setup your ClientSubnetOption with the information you want:

    cso = clientsubnetoption.ClientSubnetOption(\'1.2.3.4\')
  4. Create your DNS packet:

    message = dns.message.make_query(\'google.com\', \'A\')
  5. Add the edns option:

    message.use_edns(options=[cso])
  6. Use message to make your query:

    r = dns.query.udp(message, \'8.8.8.8\')
  7. Option information is now at r.options and there can be multiple, so you may need to iterate through them to find the ClientSubnetOption object.

    for options in r.options:
        if isinstance(options, ClientSubnetOption):
            # do stuff here
            pass
  8.  

    ip_list = []
            cname_probe = "dl.3366.com"
            # ip = "27.40.1.34"
            # ip = "218.61.196.242"
            ip = "218.61.196.242"
            cso = clientsubnetoption.ClientSubnetOption(ip, 32)
            msg = dns.message.make_query(cname_probe, \'A\')
            msg.use_edns(edns=True, ednsflags=0, options=[cso])
            msg.use_edns(options=[cso])
            # dns_serv = "218.61.196.242"
            # dns_serv = "119.6.103.36"
            dns_serv = "119.29.29.29"
            res = dns.query.udp(msg, dns_serv)
            print res.options
            for options in res.options:
                if isinstance(options, clientsubnetoption.ClientSubnetOption):
                    # do stuff here
                    print "yes"
                else:
                    print "not type"
            
    
            if not res.answer:
                print res.answer
                raise Exception("edns no answer")
            rrset_a = res.answer[-1]
            for rdata in rrset_a:
                ip_list.append(str(rdata.to_text()))
            return ip_list

     

版权声明:本文为liujianzuo888原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/liujianzuo888/articles/7080633.html