java将域名解析为IP地址,获取网卡的配置信息
DNS域名解析系统
系统简介:用户通过输入需要查询的域名,便可输出域名对应的一个IP地址或多个IP地址。
系统功能:1.通过输入域名可以查询域名所对应的IP地址。
2.可以获取当地的主机IP地址和本地机器名。
3.获取本地主机所有网卡配置相关信息(包括网卡名称、MAC地址、MTU、IP地址等)。
4.对文本记录框清屏。
InetAddress类主要是用来得到所指定的网络地址
InetAddress有三个静态方法可以用来创建 InetAddress的实例
1. static InetAddress getLocalHost()throws UnknownHostException
2. static InetAddress getBy Name(String hostName)throws UnknownHostException
3 static InetAddressl I getAllByName(String hostName) throws UnknownHostException
netAddress类的非静态方法:
boolean equals(Object other)
如果对象具有和othe相同的 Internet址则返回true
byte[] getAddress()
返回与 InetAddress对象相关的主机名的宇符串
int hashCode()
返回调用对象散列码
boolean isMulticastAddress()
如果Internet地址是一个多播地址则返回true;否则返回false
String toString()
返回主机名字字符串和IP地址
下面附上完整代码:
DNSAnalyze.java
package 域名解析;
import java.awt.EventQueue;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectInputStream.GetField;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.xml.bind.DatatypeConverter;
import javax.xml.ws.handler.PortInfo;public class DNSAnalyze {private JFrame frmDnsip;private JTextField textField;private JScrollPane scrollPane;private JTextArea textArea;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {DNSAnalyze window = new DNSAnalyze ();window.frmDnsip.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}// 获取本机主机IP地址对象 public void getLocalIP() { try { InetAddress addr = InetAddress.getLocalHost();String hostAddr = addr.getHostAddress();// 获取IP地址 String hostName = addr.getHostName();// 获取本机机器名 System.out.println("本地IP地址:" + hostAddr); System.out.println("本地机器名:" + hostName); print("本地IP地址:" + hostAddr);print("本地机器名:" + hostName);} catch (UnknownHostException e) {// 捕获未知主机异常 System.out.println("不能获得主机IP地址:" + e.getMessage());print("不能获得主机IP地址:" + e.getMessage());System.exit(1); } } //根据域名获得主机的IP地址public void getIPByName(String hostName){ try { InetAddress addr=InetAddress.getByName(hostName);//根据域名创建主机地址对象 String hostAddr=addr.getHostAddress();//获取主机IP地址 System.out.println("域名为:"+hostName+"的主机IP地址:"+hostAddr);print("域名为:"+hostName+"的主机IP地址:"+hostAddr);} catch (UnknownHostException e) { System.out.println("不能根据域名获得主机IP地址:" + e.getMessage()); print("不能根据域名获得主机IP地址:" + e.getMessage());System.exit(1); } } //根据域名获得主机所有的IP地址public void getAllIPByName(String hostName){ try { InetAddress[] addrs=InetAddress.getAllByName(hostName);//根据域名创建主机地址对象 String[] ips=new String[addrs.length]; System.out.println("域名为:"+hostName+"的所有的主机IP地址:");print("域名为:"+hostName+"的所有的主机IP地址:");for (int i = 0; i < addrs.length; i++) { ips[i]=addrs[i].getHostAddress();//获取主机IP地址 System.out.println(ips[i]); print(ips[i]);} } catch (UnknownHostException e) { System.out.println("不能根据域名获得 主机所有IP地址:" + e.getMessage()); System.exit(1); print("不能根据域名获得 主机所有IP地址:" + e.getMessage());} } //获取网络配置信息,返回字符串public String getNetworkInfo(){StringBuilder sb=new StringBuilder();try{//获取主机名String hostname=InetAddress.getLocalHost().getHostName();sb.append("本机主机名:"+hostname+"\n");sb.append("===================================================\n");//获取网卡列表Enumeration<NetworkInterface>interfaces=NetworkInterface.getNetworkInterfaces();while(interfaces.hasMoreElements()){NetworkInterface nic=interfaces.nextElement();//获取每个网卡的信息sb.append("网络接口"+nic.getIndex()+"的配置信息----------------");sb.append("接口名称:"+nic.getName()+"\n");sb.append("显示名称:"+nic.getDisplayName()+"\n");//接口状态sb.append("接口状态:");if(nic.isUp())sb.append("已经激活\n");elsesb.append("已经关闭!\n");//是否环回接口sb.append("是否环回接口:");if(nic.isLoopback())sb.append("环回接口\n");elsesb.append("非环回接口\n");//是否虚拟接口sb.append("是否虚拟接口:");if(nic.isLoopback())sb.append("虚拟接口\n");elsesb.append("物理接口\n");//接口MTUsb.append("接口MTU:"+nic.getMTU()+"\n");//获取显示硬件地址byte[] macAddr=nic.getHardwareAddress();if(macAddr!=null)sb.append("接口硬件地址(MAC地址):"+DatatypeConverter.printHexBinary(macAddr)+"\n");elsesb.append("无法获取该网卡硬件地址\n");//获取IP地址列表sb.append("IP地址列表:");Enumeration<InetAddress>ipaddresses=nic.getInetAddresses();while(ipaddresses.hasMoreElements()){sb.append("\t"+ipaddresses.nextElement().getHostAddress()+"\n");}sb.append("\n");}}catch(Exception e){e.printStackTrace();return "程序运行异常,无法获取网络信息,请检查安全软件配置。";}return sb.toString();}// public void getYuming(){// }//判断是否是一个IP static class test{public String 除去空格(String IP){//去掉IP字符串前后所有的空格 while(IP.startsWith(" ")){ IP= IP.substring(1,IP.length()).trim(); } while(IP.endsWith(" ")){ IP= IP.substring(0,IP.length()-1).trim(); } return IP; } public boolean isIp(String IP){boolean b = false; IP = 除去空格(IP); if(IP.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")){ String s[] = IP.split("\\."); if(Integer.parseInt(s[0])<255) if(Integer.parseInt(s[1])<255) if(Integer.parseInt(s[2])<255) if(Integer.parseInt(s[3])<255) b = true; } return b; } }/*** 从源码内容中获取运营商* @param content* @return operator*//*public String getOperatorFromContent(String hostName){//找到以 '来' 开始 出现的位置int beginIndex = hostName.indexOf("来自:");//找到以 '</center>' 结束 出现的位置int endIndex = hostName.indexOf("</center>");//因为我们只需要截取运营商的名字,所以beginIndex需要加3String operator = hostName.substring(beginIndex + 3, endIndex);//返回运营商名称return operator;}*//*** Create the application.*/public void print(String a) {textArea.append(a+"\n");textArea.setCaretPosition(textArea.getText().length());}public DNSAnalyze () {initialize();}/*** Initialize the contents of the frame.*/private void initialize() {frmDnsip = new JFrame();frmDnsip.setTitle("\u4F55\u540C\u5B66\u7684\u8BFE\u7A0B\u8BBE\u8BA1 DNS\u57DF\u540D\u89E3\u6790");frmDnsip.setBounds(100, 100, 670, 453);frmDnsip.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frmDnsip.getContentPane().setLayout(null);//输入文本框textField = new JTextField();textField.setBounds(10, 49, 631, 76);frmDnsip.getContentPane().add(textField);textField.setColumns(10);//查询按钮JButton btnNewButton = new JButton("\u67E5\u8BE2");btnNewButton.setBounds(420, 150, 93, 38);frmDnsip.getContentPane().add(btnNewButton);btnNewButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//当输入查询为空的时候弹出提示窗口(没成功)//String input=textField.getText();//if(textField.getText()==null){// JOptionPane.showMessageDialog(frmDnsip, "请输入需要解析的域名!");// return;//}// else{String s =textField.getText(); //new test().isIp(s); if(new test().isIp(s)==true){//getLocalIP();//调用方法获得本机的IP地址 //String hostName=textField.getText();//获取xx域名 System.out.println("ip");try { //通过IP反查域名的方法// String ip=textField.getText(); //IP地址 String[] ipStr=s.split("[.]"); //IP地址转换为字符串数组 byte[] ipBytes=new byte[4]; //声明存储转换后IP地址的字节数组 for (int i = 0; i < 4; i++) { int m=Integer.parseInt(ipStr[i]); //转换为整数 byte b=(byte)(m&0xff); //转换为字节 ipBytes[i]=b; } InetAddress inetAddr=InetAddress.getByAddress(ipBytes); //创建InetAddress对象 String canonical=inetAddr.getCanonicalHostName(); //获取域名 String host=inetAddr.getHostName(); //获取主机名 textArea.setText(canonical); //在文本框中显示域名 textArea.setText(host); //在文本框中显示主机名 } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } }else{ getLocalIP();//调用方法获得本机的IP地址 String hostName=textField.getText();//获取xx域名 getIPByName(hostName);//获取xx域名的主机IP地址 System.out.println("一个域名对应多个ip地址,列举出"+hostName+"域名下的所有ip");getAllIPByName(hostName);//获取xx域名主机所有的IP地址 //System.out.println( "营运商名称:"+getOperatorFromContent(hostName));;//获取域名的营运商}//}/}});//输出提示标签JLabel lblNewLabel = new JLabel("\u8F93\u51FAIP\u5730\u5740\u548C\u672C\u5730\u673A\u5668\u540D\uFF1A");lblNewLabel.setBounds(10, 152, 155, 34);frmDnsip.getContentPane().add(lblNewLabel);scrollPane = new JScrollPane();scrollPane.setBounds(10, 196, 631, 204);frmDnsip.getContentPane().add(scrollPane);//输出文本框textArea = new JTextArea();scrollPane.setViewportView(textArea);//输入提示标签JLabel lblNewLabel_1 = new JLabel("\u8BF7\u8F93\u5165\u9700\u8981\u67E5\u8BE2\u7684\u57DF\u540D\uFF1A");lblNewLabel_1.setBounds(10, 10, 155, 26);frmDnsip.getContentPane().add(lblNewLabel_1);//清屏按钮JButton btnNewButton_1 = new JButton("\u6E05\u5C4F");btnNewButton_1.setBounds(551, 150, 93, 38);btnNewButton_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {textArea.setText("");textField.setText("");}});frmDnsip.getContentPane().add(btnNewButton_1);JButton btnNewButton_2 = new JButton("\u83B7\u53D6\u4E3B\u673A\u7F51\u7EDC\u63A5\u53E3\u4FE1\u606F");btnNewButton_2.setBounds(214, 148, 179, 40);btnNewButton_2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {textArea.setText("");String NicInfo=getNetworkInfo();textArea.append(NicInfo);}});frmDnsip.getContentPane().add(btnNewButton_2);}
}
java将域名解析为IP地址,获取网卡的配置信息相关推荐
- 引用Geoip实现由IP地址获取国家城市等信息
需求 当我们知道用户ip的情况下,需要统计用户所属的国家/省份/城市等信息. 这时可以用开源的Geoip2 避免重复造轮子. github地址:https://github.com/maxmind/G ...
- android根据ip获取查询省份,通过IP地址获取省份城市位置信息
private String mResult = null; private String mProvinceName; //省份 private String mChCityName; //城市 p ...
- 利用IP地址获取用户的地理位置信息
最近酷乐在项目中遇到一个需要获取用户IP信息并查询出用户地理位置信息的需求,代码如下: 1.首先获取用户具体的IP信息. <?php function getClientIp(){if (get ...
- java 根据IP地址获取地理位置
来源:http://www.ipplus360.com/tech/api/ 来源:http://blog.csdn.net/Cryhelyxx/article/details/40862101 精确查 ...
- 【转载】 java根据ip地址获取详细地域信息
互联网有很多接口可以实现通过ip查询到具体的位置,如下: 通过淘宝IP地址库获取IP位置 1. 请求接口(GET):http://ip.taobao.com/service/getIpInfo.php ...
- JAVA程序 通过IP地址 获取MAC地址
转载自:https://blog.csdn.net/chwshuang/article/details/46958719 叙述 我们都知道通过在电脑上敲 cmd 后,输入ipconfig -all 来 ...
- java根据ip地址获取城市地域信息
java根据ip地址获取城市地域信息 这里提供两个公开的接口,一个是阿里的,一个是新浪的 http://ip.taobao.com/service/getIpInfo.php?ip=123.139.9 ...
- Java 根据IP地址获取城市(ip2region)
根据IP地址获取城市(ip2region) Ip2region是什么? Ip2region特性 99.9%准确率 标准化的数据格式 体积小 查询速度快 多查询客户端的支持 maven集成 小坑 Ip2 ...
- 【Java】根据IP地址获取省市
最近要做一个埋点的功能,需求里要求记录用户登录的IP和地点,打算利用ip2region.db实现 首先下载ip2region.db 地址:下载地址 ip2region是一套准确率99.9%的ip地址定 ...
最新文章
- 【我的Android进阶之旅】推荐一款能提升数十倍效率的Android应用开发助手
- 【CTF】实验吧 疑惑的汉字
- 初识react-native
- 升级Firefox8后watir-webdriver出现错误“unable to obtain stable firefox connection in 60 seconds”...
- jetty 通过配置文件嵌入式启动web服务
- iframe js 加载失败_如何提高Vue项目首页的加载速度
- java运行时异常的特点是什么_Java运行时异常和非运行时异常
- idea 配置jdk版本_JDK 11 安装过程(同时已安装了JDK 8)以及Intellij IDEA 配置
- mysql的on和in用法_数据库中in、on、with的用法及示例。
- C#.NET编程----Spring.NET NHibernate整合(一)
- 1.1.0-简介-P8-选举、多数派和租约
- c语言代码中调用系统命令行.sh shell脚本,linux shell system传参
- Bailian3258 兔子产子【递推+打表】
- 读书笔记 - 《资本幕后》
- 简历编辑导出工具(类似wps简历助手)
- sql语句练习题(mysql版)
- 使用微软TTS语音引擎实现文本朗读
- 360手机刷机·LSPosed安装和使用教程
- 屏录录制工具,Gif录制工具
- 第1部分 基础算法(提高篇)--第1章 贪心算法1425:【例题4】加工生产调度
热门文章
- 远程连接电脑以及服务器
- vscode@局部替换文本/正则匹配分组替换(复杂删除修改替换)/目录检索_模糊匹配工作空间的目录
- 『Python』Excel文件的读取以及DataFrame的相关操作 (2)
- 微服务实施笔记(三)——建立待发现服务
- IIS 设备未就绪。
- OpenCV4学习笔记(57)——基于GrabCut图像分割算法实现背景替换与背景虚化效果
- 鲁大师电动车智能化测评报告第十七期:电动车产品的同质化困局
- 谈用户裂变,社群裂变的重要性
- 6、Hello World官网教程(TX2)第一部分
- 毕业设计-基于微信小程序的校园一卡通应用系统