java之nslookup小程序--查询IP地址以及对应域名
2015-12-01 21:17
746 查看
本文主要介绍使用java实现nslookup小程序,nslookup主要用于查询IP和使用IP反查域名:
主要就是使用java.net.*这个大类(具体的类就不细讲)
输入域名输出IP
输入IP输出域名
第一个程序实现功能较为简单,第二个程序实现了更多功能
最后附录一个查询域名具体信息的网址
http://www.whois.com/whois/202.108.22.5
主要就是使用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(); } }
http://www.whois.com/whois/202.108.22.5
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统