java中獲取dns結果

java中獲取dns結果

提供兩種方法獲取域名解析的結果java

1.使用Process啓動一個進程
private List<String> getDigResults(String host,String server){

    InputStream is = null;
    BufferedReader br = null;
    Process p=null;
    StringBuffer sbf = new StringBuffer();

    try {
        String cmd= MessageFormat.format("dig @{0} {1} +short",server,host);
        p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
        is = p.getInputStream();
        br = new BufferedReader(new InputStreamReader(is,"utf-8"));
        String tmp = "";
        while((tmp = br.readLine())!=null){
            sbf.append(tmp+",");
        }

    } catch (IOException e) {
        e.printStackTrace();

    }finally{
        IOUtils.closeQuietly(br);
        IOUtils.closeQuietly(is);
        p.destroy();
        return Arrays.asList(sbf.toString().split(","));
    }
}

實質就是運行了dig命令獲取執行結果app

2.使用dnsjava

添加maven依賴maven

<dependency>
    <groupId>dnsjava</groupId>
    <artifactId>dnsjava</artifactId>
    <version>2.1.8</version>
 </dependency>

dnsjava是徹底線程化的,推薦使用工具

Resolver resolver = new SimpleResolver("114.114.114.114");
    Lookup lookup = new Lookup("www.baidu.com",Type.A);
    lookup.setResolver(resolver);
    Cache cache=new Cache();
    lookup.setCache(cache);
    lookup.run();
    if(lookup.getResult()==Lookup.SUCCESSFUL){
       Record[] records=lookup.getAnswers();
       for (Record record:records){
           System.out.println(record.rdataToString());
       }
    }

以上代碼能夠獲取域名解析的A記錄,==值得注意的是==
工具支持直接查詢CNAME,只需聲明ui

Lookup lookup = new Lookup("www.baidu.com",Type.CNAME);

可是這種方法只能獲取第一次解析的CNAME.若是像獲取相似於dig的解析結果:線程

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6 <<>> @114.114.114.114 www.baidu.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 18548
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.baidu.com.                 IN      A

;; ANSWER SECTION:
www.baidu.com.          112     IN      CNAME   www.a.shifen.com.
www.a.shifen.com.       181     IN      A       180.97.33.108
www.a.shifen.com.       181     IN      A       180.97.33.107

;; Query time: 23 msec
;; SERVER: 114.114.114.114#53(114.114.114.114)
;; WHEN: Thu Feb 28 18:25:35 2019
;; MSG SIZE  rcvd: 90

沒法經過方法直接得到,可是咱們能夠思考既然他能夠直接獲取A記錄,那必然存在一個完成的解析過程,最終獲取解析的A記錄,通過查看源碼發現其將其解析的每一步都存在一個名爲cache的Map結構裏,也就是說cache就是咱們要的結果,比較坑的是做者對cache變量申明成了私有變量也不提供get支持,慶幸的是toString()被覆蓋了,否則只能用反射的方法獲取了。下面的代碼就是獲取解析整個過程的代碼:code

Resolver resolver = new SimpleResolver("114.114.114.114");
    Lookup lookup = new Lookup("www.baidu.com",Type.A);
    lookup.setResolver(resolver);
    Cache cache=new Cache();
    lookup.setCache(cache);
    lookup.run();
    if(lookup.getResult()==Lookup.SUCCESSFUL){
        String[] results=cache.toString().split("\\n");
        for(String result:results){
            System.out.println(result);
        }
    }

結果以下:orm

{ www.baidu.com. 285 IN CNAME [www.a.shifen.com.] } cl = 3
{ www.a.shifen.com. 189 IN A [115.239.210.27] [115.239.211.112] } cl = 3
相關文章
相關標籤/搜索