Sunday, November 6, 2016

Get DNS Records of multiple websites using Java

Getting DNS Records with Java:

How to run:

1) Download the code from https://github.com/csanuragjain/dnsrecordlister

2) Enter the required domains for which you would like to extract the DNS records in subdomain.txt

3) Run DNSLookup.java

4) dnsinformation.txt gets generated with DNS information for each domain

How it works:

Main function:
 public static void main(String args[]) throws IOException  
      {  
           FileReader r=new FileReader("subdomain.txt");  
           BufferedReader br=new BufferedReader(r);  
           fw =new FileWriter("dnsinformation.txt");  
           bw=new BufferedWriter(fw);  
           String line=br.readLine();  
           while(line!=null)  
           {  
                System.out.println("DNS information for "+line);  
                bw.write("DNS information for "+line);  
                bw.newLine();  
                getDNSRecord(line);  
                System.out.println("**************************************************\n");  
                bw.write("**************************************************");  
                bw.newLine();  
                line=br.readLine();  
           }  
           br.close();  
           r.close();  
           bw.close();  
           fw.close();  
      }  

Here below happens:

1) FileReader points to subdomain.txt so that it can read the domain for which dns records need to be fetched.
2) FileWriter points to dnsinformation.txt and would write the resulting dns information into this file
3) It calls function getDNSRecord which actually extracts the DNS information.

getDNSRecord function:

   public static void getDNSRecord(String domain) throws IOException  
   {  
     try  
     {  
       InitialDirContext iDirC = new InitialDirContext();  
       // Use below if you want to extract only certain record like A, CNAME, MX  
       //Attributes attributes = iDirC.getAttributes("dns:/" + domain,new String[]{"A","CNAME","MX"});  
       Attributes attributes = iDirC.getAttributes("dns:/" + domain);  
       NamingEnumeration attributeEnumeration = attributes.getAll();  
       while (attributeEnumeration.hasMore())  
       {  
            Object info=attributeEnumeration.next();  
         System.out.println("" + info);  
         bw.write("" + info);  
         bw.newLine();  
       }  
       attributeEnumeration.close();  
     }  
     catch (NamingException exception)  
     {  
       System.err.println("No DNS record for '" + domain + "'");  
     }  
   }  

Here below happens:

1) JNDI is used to extract the all available DNS attributes
2) All attributes name & value are printed and written to file
3) It is also possible to extract only certain DNS record if you are not interested in other records. For example if you only need A, CNAME & MX record then pass the extra parameters in getAttributes like below:

 Attributes attributes = iDirC.getAttributes("dns:/" + domain,new String[]{"A","CNAME","MX"});   

I named the input file as subdomain.txt since I was using the technique to see if the subdomains gathered for a particular domain(http://cooltrickshome.blogspot.in/2016/11/creating-subdomain-crawler-using-java.html) are correctly owned.
Let me know if you have any doubts :)

No comments:

Post a Comment