简体   繁体   English

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

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

I am using Java JNDI to perform DNS lookups using the following basic syntax as per the SSCCE below, but I am trying to query all records using the "ANY" attribute: 我正在使用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();
    }
  }
}

My question is around being able to query a resource type of "ANY" which should return all the DNS resource records associated with a specific domain - example below using the "host" utility. 我的问题是能否查询“ 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.

I have read http://docs.oracle.com/javase/7/docs/technotes/guides/jndi/jndi-dns.html , which says: 我已阅读http://docs.oracle.com/javase/7/docs/technotes/guides/jndi/jndi-dns.html ,其中说:

Superclass attribute identifiers are also defined. 还定义了超类属性标识符。 These may be useful when querying records using the DirContext.getAttributes() method. 当使用DirContext.getAttributes()方法查询记录时,这些方法可能很有用。 If an attribute name has "*" in place of a type name (or class name), it represents records of any type (or class). 如果属性名称用“ *”代替类型名称(或类名称),则它表示任何类型(或类)的记录。 For example, the attribute identifier "IN *" may be passed to the getAttributes() method to find all internet class records. 例如,可以将属性标识符“ IN *”传递给getAttributes()方法以查找所有Internet类记录。 The attribute identifier "* *" represents records of any class or type. 属性标识符“ * *”代表任何类或类型的记录。

However, Java JNDI doesn't understand a resource record of "*" or "* *" as the above code doesn't return any records (I am able to query "NS" or "SOA", etc individually) - has anyone had any experience of getting this working. 但是,Java JNDI无法理解“ *”或“ * *”的资源记录,因为上述代码没有返回任何记录(我可以单独查询“ NS”或“ SOA”等)-有任何人有任何使这个工作的经验。 I can of course query each individual resource type, but considering there is a valid record type of "ANY" as per RFC 1035 (Type ID 255) this seems very inefficient? 我当然可以查询每种单独的资源类型,但是考虑到根据RFC 1035(类型ID 255)有一个有效的记录类型“ ANY”,这似乎效率很低?

After examining the methods of the Attributes class I noticed a getAll() method. 在检查了Attributes类的方法之后,我注意到了一个getAll()方法。 After further searching I was able to implement the following which now allows you to search using "*" as the record type and print all the records. 经过进一步的搜索,我能够实现以下内容,现在您可以使用“ *”作为记录类型进行搜索并打印所有记录。

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();

You're inventing semantics here. 您在这里发明语义。 There is no support anywhere in JNDI for "* *" as either an attribute set or an attribute name. JNDI的任何地方都不支持"* *"作为属性集或属性名称。 The correct syntax for 'all attributes' as an attribute set to return is "*" , and the correct way to enumerate them all is via Attributes.getAll() . 将“所有属性”设置为要返回的属性的正确语法是"*" ,枚举所有Attributes.getAll()的正确方法是通过Attributes.getAll()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM