Java DNS缓存查看器 [英] Java DNS cache viewer
本文介绍了Java DNS缓存查看器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
这是一个脚本,以便您能够查看/转储由java.net api使用的DNS缓存?打印正和负DNS地址缓存。
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class DNSCache {
public static void main(String [] args)throws Exception {
InetAddress.getByName(stackoverflow.com);
InetAddress.getByName(www.google.com);
InetAddress.getByName(www.yahoo.com);
InetAddress.getByName(www.example.com);
尝试{
InetAddress.getByName(nowhere.example.com);
} catch(UnknownHostException e){
}
String addressCache =addressCache;
System.out.println(addressCache);
printDNSCache(addressCache);
String negativeCache =negativeCache;
System.out.println(negativeCache);
printDNSCache(negativeCache);
}
private static void printDNSCache(String cacheName)throws异常{
类< InetAddress> klass = InetAddress.class;
Field acf = klass.getDeclaredField(cacheName);
acf.setAccessible(true);
对象addressCache = acf.get(null);
class cacheKlass = addressCache.getClass();
Field cf = cacheKlass.getDeclaredField(cache);
cf.setAccessible(true);
映射< String,Object> cache =(Map< String,Object>)cf.get(addressCache);
for(Map.Entry< String,Object> hi:cache.entrySet()){
Object cacheEntry = hi.getValue();
类cacheEntryKlass = cacheEntry.getClass();
字段expf = cacheEntryKlass.getDeclaredField(expiration);
expf.setAccessible(true);
long expires =(Long)expf.get(cacheEntry);
字段af = cacheEntryKlass.getDeclaredField(address);
af.setAccessible(true);
InetAddress [] addresses =(InetAddress [])af.get(cacheEntry);
列表< String> ads = new ArrayList< String>(addresses.length);
(InetAddress地址:地址){
ads.add(address.getHostAddress());
}
System.out.println(hi.getKey()++ new Date(expires)++ ads);
}
}
}
Is there a way to view/dump DNS cached used by java.net api?
解决方案
Here is a script to print the positive and negative DNS address cache.
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class DNSCache {
public static void main(String[] args) throws Exception {
InetAddress.getByName("stackoverflow.com");
InetAddress.getByName("www.google.com");
InetAddress.getByName("www.yahoo.com");
InetAddress.getByName("www.example.com");
try {
InetAddress.getByName("nowhere.example.com");
} catch (UnknownHostException e) {
}
String addressCache = "addressCache";
System.out.println(addressCache);
printDNSCache(addressCache);
String negativeCache = "negativeCache";
System.out.println(negativeCache);
printDNSCache(negativeCache);
}
private static void printDNSCache(String cacheName) throws Exception {
Class<InetAddress> klass = InetAddress.class;
Field acf = klass.getDeclaredField(cacheName);
acf.setAccessible(true);
Object addressCache = acf.get(null);
Class cacheKlass = addressCache.getClass();
Field cf = cacheKlass.getDeclaredField("cache");
cf.setAccessible(true);
Map<String, Object> cache = (Map<String, Object>) cf.get(addressCache);
for (Map.Entry<String, Object> hi : cache.entrySet()) {
Object cacheEntry = hi.getValue();
Class cacheEntryKlass = cacheEntry.getClass();
Field expf = cacheEntryKlass.getDeclaredField("expiration");
expf.setAccessible(true);
long expires = (Long) expf.get(cacheEntry);
Field af = cacheEntryKlass.getDeclaredField("address");
af.setAccessible(true);
InetAddress[] addresses = (InetAddress[]) af.get(cacheEntry);
List<String> ads = new ArrayList<String>(addresses.length);
for (InetAddress address : addresses) {
ads.add(address.getHostAddress());
}
System.out.println(hi.getKey() + " "+new Date(expires) +" " +ads);
}
}
}
这篇关于Java DNS缓存查看器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文