Java Whoisの例

Java Whoisの例

whois image

このチュートリアルでは、Javaライブラリ(「Apache Commons Net」)を使用してドメインのWHOISデータを取得する方法を示します。

1. 単純なJava Whoisの例

internic.netで登録されたドメインの場合、whoisデータを直接取得できます。

WhoisTest.java

package com.example.whois.bo;

import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.whois.WhoisClient;

public class WhoisTest {

    public static void main(String[] args) {

        WhoisTest obj = new WhoisTest();
        System.out.println(obj.getWhois("example.com"));
        System.out.println("Done");

    }

    public String getWhois(String domainName) {

        StringBuilder result = new StringBuilder("");

        WhoisClient whois = new WhoisClient();
        try {

            //default is internic.net
            whois.connect(WhoisClient.DEFAULT_HOST);
            String whoisData1 = whois.query("=" + domainName);
            result.append(whoisData1);
            whois.disconnect();

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

        return result.toString();

    }

}

2. 複雑なJava Whoisの例

他のレジストラ(internic.netではない)で登録されたドメインの場合、whoisデータを取得するための追加の手順が必要です。

* nixおよびmacでwhoisコマンドを確認してください。

$ whois google.com
### It will returns many non related subdomain.google.com data

$ whois =google.com
### Add '=' sign to get the exact result.
### refer to the "Whois server" URL

### Sample data
### Domain Name: GOOGLE.COM
### Registrar: MARKMONITOR INC.
### Whois Server: whois.markmonitor.com
### Referral URL: http://www.markmonitor.com

### The google.com whois is available under whois.markmonitor.com

$ whois -h whois.markmonitor.com google.com
### done, finally, get the whois data of google.com

上記の手順をJavaで実装してみましょう。一目瞭然のコメントを参照してください。

WhoisTest.java

package com.example.whois.bo;

import java.io.IOException;
import java.net.SocketException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.net.whois.WhoisClient;

public class WhoisTest {

    private static Pattern pattern;
    private Matcher matcher;

    // regex whois parser
    private static final String WHOIS_SERVER_PATTERN = "Whois Server:\\s(.*)";
    static {
        pattern = Pattern.compile(WHOIS_SERVER_PATTERN);
    }

    public static void main(String[] args) {

        WhoisTest obj = new WhoisTest();
        System.out.println(obj.getWhois("google.com"));

        System.out.println("Done");

    }

    // example google.com
    public String getWhois(String domainName) {

        StringBuilder result = new StringBuilder("");

        WhoisClient whois = new WhoisClient();
        try {

          whois.connect(WhoisClient.DEFAULT_HOST);

          // whois =google.com
          String whoisData1 = whois.query("=" + domainName);

          // append first result
          result.append(whoisData1);
          whois.disconnect();

          // get the google.com whois server - whois.markmonitor.com
          String whoisServerUrl = getWhoisServer(whoisData1);
          if (!whoisServerUrl.equals("")) {

            // whois -h whois.markmonitor.com google.com
            String whoisData2 =
                            queryWithWhoisServer(domainName, whoisServerUrl);

            // append 2nd result
            result.append(whoisData2);
          }

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

        return result.toString();

    }

    private String queryWithWhoisServer(String domainName, String whoisServer) {

        String result = "";
        WhoisClient whois = new WhoisClient();
        try {

            whois.connect(whoisServer);
            result = whois.query(domainName);
            whois.disconnect();

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

        return result;

    }

    private String getWhoisServer(String whois) {

        String result = "";

        matcher = pattern.matcher(whois);

        // get last whois server
        while (matcher.find()) {
            result = matcher.group(1);
        }
        return result;
    }

}

出力

Whois Server Version 2.0

Domain names in the .com and .net domains can now be registered
with many different competing registrars. Go to http://www.internic.net
for detailed information.

//......

Registrant:
        Dns Admin
        Google Inc.
        Please contact [email protected] 1600 Amphitheatre Parkway
         Mountain View CA 94043
        US
        [email protected] +1.6502530000 Fax: +1.6506188571

    Domain Name: google.com

        Registrar Name: Markmonitor.com
        Registrar Whois: whois.markmonitor.com
        Registrar Homepage: http://www.markmonitor.com

    //......

    Created on..............: 1997-09-15.
    Expires on..............: 2020-09-13.
    Record last updated on..: 2013-02-28.

    //......

Whois parser
上記は、正規表現を使用してwhoisサーバーのURLを取得しています。 Java whoisパーサーを知っている場合は、以下のコメントを行ってください、ありがとう。