Java - Récupère les serveurs de noms d’un site web

Java - Obtenez les serveurs de noms d'un site Web

DNS

Dans ce didacticiel, nous allons vous montrer comment obtenir les serveurs de noms d'un site Web à l'aide de la commande Java + dig.

1. Utilisation de Dig Command

1.1 On Linux, we can use dig command to query a DNS lookup of a website, for example :

$ dig any example.com

//...
example.com.             299     IN      A       162.159.x.x
example.com.             299     IN      A       198.41.x.x
example.com.             299     IN      MX      10 aspmx2.googlemail.com.
example.com.             299     IN      MX      10 aspmx3.googlemail.com.
example.com.             299     IN      MX      1 aspmx.l.google.com.
example.com.             299     IN      MX      5 alt1.aspmx.l.google.com.
example.com.             299     IN      MX      5 alt2.aspmx.l.google.com.
example.com.             21599   IN      NS      max.ns.cloudflare.com.
example.com.             21599   IN      NS      erin.ns.cloudflare.com.

;; Query time: 246 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Tue Mar 18 08:35:24 Malay Peninsula Standard Time 2014
;; MSG SIZE  rcvd: 387

P.S The dig is a built-in command on *nix, no need to install.

1.2 On Windows, you need to install the BIND package (a zip file) from isc.org, and the dig command is inside the zip file. Après avoir extrait le fichier zip téléchargé, définissez le dossier «bind» sur la variable d'environnement, afin de pouvoir utiliser la commandedig partout.

C:\> dig +short NS example.com
max.ns.cloudflare.com.
erin.ns.cloudflare.com.

2. Exemple DNS Java

En Java, nous pouvons appeler la commande externedig pour obtenir facilement les serveurs de noms.

ShellCommand.java

package com.example.shell;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ShellCommand {

    public static void main(String args[]) {

        ShellCommand shell = new ShellCommand();

        String result = shell.run("dig +short NS example.com");
        System.out.println(result);

    }

    public String run(String command) {

        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;
        Process p;

        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            reader = new BufferedReader(
                new InputStreamReader(p.getInputStream()));

            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return sb.toString();

    }

}

Sortie

max.ns.cloudflare.com.
erin.ns.cloudflare.com.

3. Obtenez des serveurs de messagerie

De plus, vous pouvez utiliser «MX» pour obtenir des serveurs de messagerie.

C:\> dig +short MX example.com

10 aspmx2.googlemail.com.
5 alt2.aspmx.l.google.com.
5 alt1.aspmx.l.google.com.
1 aspmx.l.google.com.
10 aspmx3.googlemail.com.

Note
Si vous voulez plus de fonctionnalités DNS, essayez la bibliothèquednsjava, une implémentation de DNS en Java.