繁体   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