java获取dns记录,Java DNS查找SRV记录

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.

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

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

}

}

}

解决方案

Your code is fine, except that Lookup.run() returns null (not an empty array) if no records are found.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值