簡體   English   中英

使用Java JNDI執行DNS“ ANY”查找

[英]Performing DNS “ANY” Lookup using Java JNDI

我正在使用Java JNDI按照以下SSCCE使用以下基本語法執行DNS查找,但是我試圖使用“ ANY”屬性查詢所有記錄:

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資源記錄-以下示例使用“主機”實用程序。

chrixm@puffy(:):~$ 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()方法以查找所有Internet類記錄。 屬性標識符“ * *”代表任何類或類型的記錄。

但是,Java JNDI無法理解“ *”或“ * *”的資源記錄,因為上述代碼沒有返回任何記錄(我可以單獨查詢“ NS”或“ SOA”等)-有任何人有任何使這個工作的經驗。 我當然可以查詢每種單獨的資源類型,但是考慮到根據RFC 1035(類型ID 255)有一個有效的記錄類型“ ANY”,這似乎效率很低?

在檢查了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();

您在這里發明語義。 JNDI的任何地方都不支持"* *"作為屬性集或屬性名稱。 將“所有屬性”設置為要返回的屬性的正確語法是"*" ,枚舉所有Attributes.getAll()的正確方法是通過Attributes.getAll()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM