LoginSignup
4
4

More than 5 years have passed since last update.

Runtimeでnslookupする

Last updated at Posted at 2016-02-08

はじめに

Runtimeで外部アプリを叩いているだけじゃないか、というご指摘はごもっともです。

事前に準備する外部ライブラリ等はありません。

実装例

サンプルでは、動作確認しやすいようにmainメソッドで実行できるようにしてあります。
結果だけを確認したい場合は、この記事の一番下のリンク先で使えるようにしてありますのでご覧ください。

NsLookup.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 *
 * @author tool-taro.com
 */
public class NsLookup {

    public static void main(String[] args) throws IOException, InterruptedException {

        //検索したいドメイン・IPアドレス
        String source = "tool-taro.com";
        //nslookupのパス
        String nslookupPath = "/usr/bin/nslookup";

        //nslookup処理
        String[] commandArray = new String[2];
        commandArray[0] = nslookupPath;
        commandArray[1] = source;
        Charset charset = StandardCharsets.UTF_8;

        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        StringBuilder logBuilder = new StringBuilder();
        StringBuilder errorBuilder = new StringBuilder();
        int status = 0;

        try {
            process = runtime.exec(commandArray);
            final InputStream in = process.getInputStream();
            final InputStream ein = process.getErrorStream();

            Runnable inputStreamThread = () -> {
                BufferedReader reader = null;
                String line;
                try {
                    reader = new BufferedReader(new InputStreamReader(in, charset));
                    while (true) {
                        line = reader.readLine();
                        if (line == null) {
                            break;
                        }
                        logBuilder.append(line).append("\n");
                    }
                }
                catch (IOException e) {
                }
                finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        }
                        catch (IOException e) {
                        }
                    }
                }
            };
            Runnable errorStreamThread = () -> {
                BufferedReader reader = null;
                String line;
                try {
                    reader = new BufferedReader(new InputStreamReader(ein, charset));
                    while (true) {
                        line = reader.readLine();
                        if (line == null) {
                            break;
                        }
                        errorBuilder.append(line).append("\n");
                    }
                }
                catch (IOException e) {
                }
                finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        }
                        catch (IOException e) {
                        }
                    }
                }
            };

            Thread inThread = new Thread(inputStreamThread);
            Thread errorThread = new Thread(errorStreamThread);

            inThread.start();
            errorThread.start();

            status = process.waitFor();
            inThread.join();
            errorThread.join();
        }
        finally {
            if (process != null) {
                try {
                    process.destroy();
                }
                catch (Exception e) {
                }
            }
        }
        //標準出力
        System.out.format("検索結果=%1$s", logBuilder.toString());
    }
}

動作確認

> javac NsLookup.java
> java NsLookup
> 検索結果=Server:         XXX.XXX.XXX.XXX
Address:        XXX.XXX.XXX.XXX#XX

Non-authoritative answer:
Name:   tool-taro.com
Address: XXX.XXX.XXX.XXX

環境

  • 開発

    • Windows 10 Pro
    • JDK 1.8.0_112
    • NetBeans IDE 8.2
  • 動作検証

    • CentOS Linux release 7.2
    • JDK 1.8.0_112

上記の実装をベースにWebツールも公開しています。
Nslookup(ドメイン/IPアドレスサーチ)|Web便利ツール@ツールタロウ

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4