方法如下——
public static String getHostByAddr(byte[] addr) throws UnknownHostException {
Name name = ReverseMap.fromAddress(InetAddress.getByAddress(addr));
final String[] servers = new String[] {"208.67.220.220", "208.67.222.222"};
final Resolver res = new ExtendedResolver(servers);
final Lookup lookUp = new Lookup(name, Type.PTR);
lookUp.setResolver(res);
Record[] records = lookUp.run();
if (records == null) {
throw new UnknownHostException();
}
return ((PTRRecord) records[0]).getTarget().toString();
}
这是对上述方法的调用-
final InetAddress ip = InetAddress.getByName("198.154.218.168");
final byte[] bytes = ip.getAddress();
final String host = getHostByAddr(bytes);
System.out.println("Host - " + host);
在大多数情况下都可以正常工作,但是当 IP 映射到多个域时会失败 (???)
这是示例-
- 从这里获取securonix.com的 IP ,它是 198.154.218.168
- 如果我将此 IP 传递给上述方法,则会出现错误
- 但如果我在这里尝试相同的 IP ,它会列出 4 个域
是否可以使用DNSJava做到这一点?