2013-08-02 49 views
0

你好,我已經得到github上的這個代碼的要點,並添加了我自己的小調整,但它很慢有什麼辦法可以加快它?我已經嘗試過線程,但是它只是給寫入文件造成更多麻煩,所以我怎麼能加快速度?Python的DNS查找器

# 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) 

回答

0

插槽庫中有一個功能,這個

import socket 
print socket.gethostbyaddr('8.8.8.8') 
+0

的Y你沒有閱讀的代碼是嗎? –

+0

我確實。這應該是相對較快的。你也可以使用隊列和線程並行請求,如圖所示http://www.lonelycode.com/2011/02/04/python-threading-and-queues-and-why-its-awesome/ – djinn