Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python DNS查找器_Python_Dns - Fatal编程技术网

Python DNS查找器

Python DNS查找器,python,dns,Python,Dns,您好,我已经在github上获得了这段代码的要点,并添加了我自己的一些调整,但是速度很慢,有什么方法可以加快速度吗?我已经尝试了线程,但它只会在写入文件时产生更多的麻烦,因此如何加快速度 # dnsfind.py <startip> <endip> import sys import socket import struct import threading import os import time # basic DNS header for 1 query de

您好,我已经在github上获得了这段代码的要点,并添加了我自己的一些调整,但是速度很慢,有什么方法可以加快速度吗?我已经尝试了线程,但它只会在写入文件时产生更多的麻烦,因此如何加快速度

# dnsfind.py <startip> <endip>

import sys
import socket
import struct
import threading
import os
import time

# basic DNS header for 1 query
def buildDNSQuery(host):
    packet=struct.pack("!HHHHHH", 0x0001, 0x0100, 1, 0, 0, 0)

    for name in host:
         query=struct.pack("!b"+str(len(name))+"s", len(name), name)
         packet=packet+query

    packet=packet+struct.pack("!bHH",0,1,1)

    return packet

    # just ask for www.google.com
    TEST_QUERY=buildDNSQuery(["www","google","com"])
    DNS_PORT=53
    TIMEOUT=2
  # scan a server for DNS
    def ScanDNS(addr, timeout):
          s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
          s.settimeout(TIMEOUT)

  # send DNS question to server
          sendcount=s.sendto(TEST_QUERY, 0, (addr,DNS_PORT))
          if sendcount <= 0:
               return False

    # wait for response
         try:
             recvdata=s.recvfrom(1024)
         except socket.error, e:
               return False

        return True

   # extract an ip address into a tuple of integers
  def ExtractIP(ip):
        partip=ip.split(".")
        if len(partip) != 4:
            print "Invalid ip address: "+ip
        try:
             iptuple=(int(partip[0]),int(partip[1]),int(partip[2]),int(partip[3]))
        except ValueError:
             print "Invalid ip address: "+ip

        return iptuple

if len(sys.argv) < 2:
       print "Not enough parameters supplied!"

# convert ip address to integer tuple
STARTs_IP=ExtractIP(sys.argv[1])
ENDs_IP=ExtractIP(sys.argv[2])
File = open("file.txt","wb")
def main(START_IP,END_IP):
    # store found DNS servers
    foundDNS=[]

    # scan all the ip addresses in the range
    for i0 in range(START_IP[0], END_IP[0]+1):
        for i1 in range(START_IP[1], END_IP[1]+1):
            for i2 in range(START_IP[2], END_IP[2]+1):
                for i3 in range(START_IP[3], END_IP[3]+1):
                   # build ip addres
                   ipaddr=str(i0)+"."+str(i1)+"."+str(i2)+"."+str(i3)

                   print "Scanning "+ipaddr+"...",
                   # scan address
                   ret=ScanDNS(ipaddr, 10)

                   if ret==True:
                       foundDNS.append(ipaddr)
                       print "Found!"
                       File.write(ipaddr)
                       File.write("\n")
                   else:
                        print 

                   # print out all found servers

 if __name__ == "__main__":
     main(STARTs_IP,ENDs_IP)
#dnsfind.py
导入系统
导入套接字
导入结构
导入线程
导入操作系统
导入时间
#1查询的基本DNS标头
def buildDNSQuery(主机):
数据包=结构包(“!hhhhh”,0x0001,0x0100,1,0,0)
对于主机中的名称:
query=struct.pack(“!b”+str(len(name))+“s”,len(name),name)
数据包=数据包+查询
数据包=数据包+结构包(“!bHH”,0,1,1)
返回包
#请访问www.google.com
TEST_QUERY=buildDNSQuery([“www”、“google”、“com]”)
DNS_端口=53
超时=2
#扫描服务器上的DNS
def SCANNS(地址,超时):
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
s、 设置超时(超时)
#将DNS问题发送到服务器
sendcount=s.sendto(测试查询,0,(地址,DNS\u端口))

如果sendcount套接字库具有此功能

 import socket
 print socket.gethostbyaddr('8.8.8.8')

确实是这样。这应该比较快。您还可以使用队列和线程进行并行请求,如图所示