java之nslookup小程序–查询IP地址以及对应域名

本文主要介绍使用java实现nslookup小程序,nslookup主要用于查询IP和使用IP反查域名:

  1. 主要就是使用java.net.*这个大类(具体的类就不细讲)
  2. 输入域名输出IP
  3. 输入IP输出域名
  4. 第一个程序实现功能较为简单,第二个程序实现了更多功能
  5. 最后附录一个查询域名具体信息的网址
import java.net.*;
import java.util.regex.Pattern;
import java.io.*;

public class nslookup {
    public static void main(String[] args) {
        if (args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                lookup(args[i]);
            }
        } else {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter the domain names or IP addresses. Enter \"exit\" to quit.");
            try {
                while (true) {
                    String host = in.readLine();
                    if (host.equalsIgnoreCase("exit")) {
                        break;
                    }
                    lookup(host);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void lookup(String host) {
        if(isDomain(host)) {
            try{
                InetAddress address = InetAddress.getByName(host);
                System.out.print("Address: ");
                System.out.println(address.getHostAddress());
            }catch(UnknownHostException e){
                e.printStackTrace();
            }
            // TODO Auto-generated method stub
        } else {
            try{
                InetAddress address = InetAddress.getByName(host);
                String hostName = address.getHostName();
                if(hostName.equals(host)){
                    System.out.println(host + "'s domain cant find");
                }else{
                    System.out.print("Domain: ");
                    System.out.println(hostName);
                }
            }catch(UnknownHostException e){
                e.printStackTrace();
            }
            // TODO Auto-generated method stub
        }
    }

    private static boolean isDomain(String host) {
        String[] part = host.split("\\.");
        if (part.length == 4) {
            for (String pa : part) {
                if (!isNumeric(pa)) {
                    return true;
                }
            }
            return false;
        } else {
            return true;
        }
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }
}

这里写图片描述

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.regex.Pattern;

public class nslookupAdvanced {
    public static void main(String[] args) {
        if (args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                lookup(args[i]);
            }
        } else {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter the domain names or IP addresses. Enter \"exit\" to quit.");
            try {
                while (true) {
                    String host = in.readLine();
                    if (host.equalsIgnoreCase("exit")) {
                        break;
                    }
                    lookup(host);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void lookup(String host) {
        if(isDomain(host)) {
            try {
                InetAddress[] address = InetAddress.getAllByName(host);
                System.out.println("Address: ");
                for (int i = 0; i < address.length; i++) {
                    System.out.println(address[i].getHostAddress());
                }
                //判断是否本地
                NetworkInterface ni = NetworkInterface.getByInetAddress(address[0]);
                if (ni != null) {
                    System.out.print("This is local address.  ");
                    System.out.println(ni.getName());
                } else {
                    System.out.println("This is not local address.");
                }
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            // TODO Auto-generated method stub
        } else {
            try {
                InetAddress address = InetAddress.getByName(host);
                System.out.println("Domain: ");
                String hostName = address.getHostName();
                if (hostName.equals(host)){
                    System.out.println(host + "'s domain can't be find!");
                }else{
                    System.out.println(hostName);
                }
                //判断是否本地
                NetworkInterface ni = NetworkInterface.getByInetAddress(address);
                if (ni != null) {
                    System.out.print("This is local address.  ");
                    System.out.println(ni.getName());
                } else {
                    System.out.println("This is not local address.");
                }
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            // TODO Auto-generated method stub
        }
    }

    private static boolean isDomain(String host) {
        String[] part = host.split("\\.");
        if (part.length == 4) {
            for (String pa : part) {
                if (!isNumeric(pa)) {
                    return true;
                }
            }
            return false;
        } else {
            return true;
        }
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

}

这里写图片描述

发表评论