dnsjava usage

petercao發表於2019-02-02

linux dig 命令使用方法

https://www.imooc.com/article/26971?block_id=tuijian_wz

 

https://jimwayne.blogspot.com/2012/08/java-dnsjava-dns-query.html

http://dnsjava.org

http://www.dnsjava.org/download/

http://www.xbill.org/dnsjava/dnsjava-current/examples.html

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.xbill.DNS.*;

public class NetWork {
    public static void main(String[] args) throws UnknownHostException, TextParseException {
        InetAddress address = InetAddress.getByName("baidu.com");
        System.out.println(address.getHostAddress());

        //查詢域名對應的IP地址
        Lookup lookup = new Lookup("baidu.com", Type.A);

        // Print the DNS server used by dnsjava
        String[] resolvers = ResolverConfig.getCurrentConfig().servers();
        for (int i = 0; i < resolvers.length; i++) {
            System.out.println("Reslover's default: " + resolvers[i]);
        }

        // Send DNS query
        lookup.run();
        if (lookup.getResult() != Lookup.SUCCESSFUL) {
            System.out.println("ERROR: " + lookup.getErrorString());
            return;
        }
        Record[] answers = lookup.getAnswers();
        for(Record rec : answers){
            System.out.println(rec.toString());
        }
        System.out.println("Searching complete.");
    }
}

相關文章