我正在尝试编写一个类,它将从我/ etc / hosts文件中的IP地址返回一个域名。例如,我知道Google拥有的IP地址块74.125.0.0 - 74.125.255.255
,并且在我的主机文件中我知道;
74.125.24.155 Google
74.125.24.101 Google
74.125.24.102 Google
74.125.24.132 Google
74.125.24.113 Google
74.125.24.84 Google
我编写了一段简单的代码,它将查找hosts文件并搜索相应IP地址的名称。这是代码;
String destination = "74.125.24.84";
InetAddress address = InetAddress.getByName(destination);
String resolvedHost = address.getHostName();
System.out.println("Translated " + destination + " to host name " + resolvedHost);
这将返回以下Translated 74.125.24.84 to host name Google
。
然后我尝试了;
String destination = "74.125.24.*";
InetAddress address = InetAddress.getByName(destination);
String resolvedHost = address.getHostName();
System.out.println("Translated " + destination + " to host name " + resolvedHost);
但在回复中收到以下错误;
java.net.UnknownHostException: 74.125.24.*
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source)
at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getByName(Unknown Source)
at IPToDomainName.main(IPToDomainName.java:12)
有没有办法可以使用通配符来表示Google的IP地址?
答案 0 :(得分:0)
你能迭代它吗?
for (int i=0;i<=255;i++) {
String destination = "74.125.24." + i;
InetAddress address = InetAddress.getByName(destination);
String resolvedHost = address.getHostName();
if (address == null) // Or any exception.
continue;
System.out.println("Translated " + destination + " to host name " + resolvedHost);
}