SRV 记录的 Java DNS 查找 [英] Java DNS Lookup for SRV records
问题描述
在下面的 java 代码中,我正在查找 DNS SRV 记录以解析给定域名的目标域名和关联端口,例如 root@1000000000.blubluzone.com.下面用/HERE/指示的查找函数以某种方式返回 null,我无法获得查询结果(即记录数组为 null).因此,在 for 循环中访问记录数组时会引发空指针异常.
In the below java code, I am making a DNS SRV record lookup to resolve the target domain name and associated port for a given domain name such as root@1000000000.blubluzone.com. The lookup function indicated with /HERE/ below returns null somehow and I cannot get a a query result (i.e. record array is null). As a consequence, a null pointer exception is thrown when records array is accessed in the for loop.
您认为我缺少什么才能使以下代码正常工作.我正在使用 dnsjava,相关的 API jar 文件可在 http://www.dnsjava.org/download/(在页面底部).预先感谢您的建议.
What do you think I am missing to make the following code work. I am using using dnsjava and the related API jar file is available at http://www.dnsjava.org/download/ (at the bottom of the page). Thanks in advance for your suggestion.
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.SRVRecord;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;
public class DNSLookup {
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("Usage: java -jar DNSLookup <hostname>");
System.exit(1);
}
String query = "_ssh._tcp." + args[0];
try {
/*****HERE*********/
Record[] records = new Lookup(query, Type.SRV).run();
for (Record record : records) {
SRVRecord srv = (SRVRecord) record;
String hostname = srv.getTarget().toString().replaceFirst("\.$", "");
int port = srv.getPort();
System.out.println(hostname + ":" + port);
}
} catch (TextParseException e) {
e.printStackTrace();
}
}
}
推荐答案
你的代码很好,除了 Lookup.run()
返回 null
(not 一个空数组)如果没有找到记录.
Your code is fine, except that Lookup.run()
returns null
(not an empty array) if no records are found.
例如,如果您将 _ssh._tcp
替换为 _nicname._tcp
,然后查找 uk
,您将找到一个 SRV 记录.UK whois
服务器.
For example, if you replace _ssh._tcp
with _nicname._tcp
and then look up uk
you'll find an SRV record for the .UK whois
server.
如果有问题,那就是你的输入参数.
If there's a problem it's with your input parameters.
这篇关于SRV 记录的 Java DNS 查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!