Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Fetching DNS MX and A records

Thread view: 
Roedy Green - 28 May 2007 10:52 GMT
The following code works fine, but it requires hard coding in a DNS
server IP or name.  I vaguely recall there is a new way to do this
where it finds a DNS server on its own, or there is a new method to
dynamically find a DNS server.

 /**
    * Gets all matching dns records as an array of strings.
    *
    * @param domain domain, e.g. oberon.ark.com or oberon.com which
you want
    *               the DNS records.
    * @param types  e.g."MX","A" to describe which types of
    *               record you want.
    *
    * @return ArrayList of Strings
    *
    * @throws NamingException if DNS lookup fails.
    */
   private static ArrayList<String> getDNSRecs( String domain,
                                                String... types )
throws NamingException
       {
       ArrayList<String> results = new ArrayList<String>( 15 );
       DirContext ictx = new InitialDirContext();
       Attributes attrs =
               ictx.getAttributes( "dns://" + DNS_SERVER + "/" +
domain,
                                   types );
       for ( Enumeration e = attrs.getAll(); e.hasMoreElements(); )
           {
           Attribute a = (Attribute) e.nextElement();
           int size = a.size();
           for ( int i = 0; i < size; i++ )
               {
               // MX string has priority (lower better) followed by
associated mailserver
               // A string is just IP
               results.add( (String) a.get( i ) );
               }// end inner for
           }// end  outer for
       if ( DEBUGGING && results.size() == 0 )
           {
           System.err.println( "Failed to find any DNS records for
domain " + domain );
           }
       return results;
       }
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 30 May 2007 11:22 GMT
On Mon, 28 May 2007 09:52:35 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

> I vaguely recall there is a new way to do this
>where it finds a DNS server on its own, or there is a new method to
>dynamically find a DNS server.

I found it. Real Gagnon documented it.  I now have sample code at
http://mindprod.com/jgloss/dns.html

I use it in the bulk email program posted at
http://mindprod.com/products1.html#BULK
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Martin Gregorie - 30 May 2007 16:46 GMT
> On Mon, 28 May 2007 09:52:35 GMT, Roedy Green
> <see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
[quoted text clipped - 3 lines]
>> where it finds a DNS server on its own, or there is a new method to
>> dynamically find a DNS server.

You can look up a domain and its MX records remarkably easily via
DNSJava too. It was your site that put me onto that: thanks.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.