版权声明:本文为博主原创文章,未经博主允许不得转载。
本文主要介绍使用java实现nslookup小程序,nslookup主要用于查询IP和使用IP反查域名:
- 主要就是使用java.net.*这个大类(具体的类就不细讲)
- 输入域名输出IP
- 输入IP输出域名
- 第一个程序实现功能较为简单,第二个程序实现了更多功能
- 最后附录一个查询域名具体信息的网址
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();
}
}