2016-11-08 89 views
0

我使用的Java JNDI使用以下基本語法按以下SSCCE執行DNS查找,但我想查詢使用「ANY」屬性中的所有記錄:執行DNS「ANY」查找使用Java JNDI

import java.util.*; 
import javax.naming.*; 
import javax.naming.directory.*; 

public class SSCCE { 
    public static void main(String[] args) { 
    try { 
     Properties p = new Properties(); 
     p.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); 
     InitialDirContext idc = new InitialDirContext(p); 

     Attributes attrs = idc.getAttributes("netnix.org", new String[] { "* *" }); 
     Attribute attr = attrs.get("* *"); 

     if (attr != null) { 
     for (int i = 0; i < attr.size(); i++) { 
      System.out.println("Found " + (String)attr.get(i)); 
     } 
     } 
     else { 
     System.out.println("Found nothing"); 
     } 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
} 

我的問題是能夠查詢「ANY」的資源類型,它應該返回與特定域關聯的所有DNS資源記錄 - 例如下面使用「主機」實用程序。

[email protected](:):~$ host -t ANY netnix.org 
netnix.org has SPF record "v=spf1 include:_spf.google.com ~all" 
netnix.org mail is handled by 10 aspmx2.googlemail.com. 
netnix.org mail is handled by 5 alt1.aspmx.l.google.com. 
netnix.org mail is handled by 1 aspmx.l.google.com. 
netnix.org mail is handled by 5 alt2.aspmx.l.google.com. 
netnix.org mail is handled by 10 aspmx3.googlemail.com. 
netnix.org name server ns-1154.awsdns-16.org. 
netnix.org name server ns-941.awsdns-53.net. 
netnix.org name server ns-61.awsdns-07.com. 
netnix.org name server ns-1880.awsdns-43.co.uk. 

我已閱讀http://docs.oracle.com/javase/7/docs/technotes/guides/jndi/jndi-dns.html,它說:也被定義

超類屬性標識符。當使用DirContext.getAttributes()方法查詢記錄時,這些可能很有用。如果屬性名稱具有「*」來代替類型名稱(或類名稱),則表示任何類型(或類)的記錄。例如,可以將屬性標識符「IN *」傳遞給getAttributes()方法以查找所有的互聯網類記錄。屬性標識符「* *」表示任何類或類型的記錄。

但是,由於上述代碼沒有返回任何記錄(我能夠查詢「NS」或「SOA」,所以Java JNDI不理解「*」或「* *」的資源記錄,等等) - 有任何人有任何這方面的工作經驗。我當然可以查詢每個單獨的資源類型,但考慮到根據RFC 1035(類型ID 255)存在有效的記錄類型「ANY」,這看起來效率很低?

回答

0

在檢查了Attributes類的方法後,我發現了一個getAll()方法。進一步搜索後,我能夠實現以下功能,現在允許您使用「*」作爲記錄類型進行搜索並打印所有記錄。

Attributes attrs = idc.getAttributes("netnix.org", new String[] { "*" }); 
NamingEnumeration<?> ae = attrs.getAll(); 

while (ae.hasMore()) { 
    Attribute attr = (Attribute)ae.next(); 
    for (int i = 0; i < attr.size(); i++) { 
    Object a = attr.get(i); 
    if (a instanceof String) { 
     System.out.println(attr.getID() + " " + a); 
    } 
    else { 
     System.out.println(attr.getID() + " NOT ASCII"); 
    } 
    } 
} 
ae.close(); 
0

你在這裏發明了語義。對於"* *",JNDI中的任何地方都不支持作爲屬性集或屬性名稱。 「所有屬性」的正確語法設置爲返回的屬性爲"*",並且枚舉它們的正確方法都是通過Attributes.getAll()

+0

因此,上面引用的Oracle文章指出:「屬性標識符'\ * \ *'表示任何類或類型的記錄。」實際上是虛構的,我應該讓甲骨文修復他們錯誤措辭的文檔? – chrixm