栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java DNS缓存查看器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 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");    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);    }  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438177.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号