[英]Using a wildcard IP Address to get a Domain Name
我正在嘗試編寫一個類,該類將從/ 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
我編寫了一段簡單的代碼,它將查找主機文件並搜索相應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地址?
你可以遍歷它嗎?
for (int i=0;i<=255;i++) {
String destination = "74.125.24." + i;
InetAddress address = InetAddress.getByName(destination);
if (address == null) // Or any exception.
continue;
String resolvedHost = address.getHostName();
System.out.println("Translated " + destination + " to host name " + resolvedHost);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.