我有DNS服务器IP地址和主机名。
使用Java,如何使用该IP地址和主机名找到该DNS服务器返回的主机名的IP地址?
Answers:
看看InetAddress和getHostAddress()方法。
InetAddress address = InetAddress.getByName("www.example.com");
System.out.println(address.getHostAddress());
InetAddress应获取DNS解析的地址getHostAddress()。我不确定是否可以强迫它使用特定的DNS服务器。
您可以这样做:
for(InetAddress addr : InetAddress.getAllByName("stackoverflow.com"))
System.out.println(addr.getHostAddress());
您可以为此使用InetAddress。试试下面的代码,
InetAddress address = InetAddress.getByName("www.yahoo.com");
System.out.println(address.getHostAddress());
System.out.println(address.getHostName());
正如以上所有建议一样,您可以使用,
InetAddress.getByName("hostName")但是这可以为您提供缓存的IP,请阅读Java文档。如果要从DNS获取IP,可以使用:
InetAddress[] ipAddress = DNSNameService.lookupAllHostAddr("hostName");
dig <hostname>在命令行中?