Java实现Internet地址获取
Java实现Internet地址获取
代码内容
- 输入域名输出IPV4地址
- 输入IP地址输出域名
- 支持命令行输入
- 支持交互式输入
代码实现
/* nslookup.java */
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++) {
System.out.println("\n> " + args[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 {
boolean isEmptyLine = false;
while (true) {
if (isEmptyLine){
isEmptyLine = false;
System.out.print("> ");
} else
System.out.print("\n> ");
String host = in.readLine();
if (host.equalsIgnoreCase("exit")) {
break;
} else if (host.isEmpty()){
isEmptyLine = true;
continue;
}
lookup(host);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void lookup(String host) {
if(isDomain(host)) {
nat(host, true);
} else {
nat(host, false);
}
}
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]*");
//Pattern pattern = Pattern.compile("^[0-9]+(.[0-9]*)?$");
return pattern.matcher(str).matches();
}
private static void nat(String host, boolean isDomain) {
try {
if (host.equals("127.0.0.1")){
System.out.println("Name: localhost");
return;
}
InetAddress[] address = InetAddress.getAllByName(host);
if (isDomain) {
for (int i = 0; i < address.length; i++){
System.out.println("Address: " + address[i].getHostAddress());
}
}
else if (host.equals(address[0].getHostName())){
for (int i = 0; i < address.length; i++){
System.out.println("Address: " + address[i].getHostAddress());
}
}
else {
for (int i = 0; i < address.length; i++){
System.out.println("Name: " + address[i].getHostName());
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
运行截图
输入域名的结果

输入IP地址的结果

输入本机上IP地址的结果

增强版内容
- 在源程序的基础之上在输入域名时输出全部地址
- 如果查询的域名或者IP在本主机上还要输出对应的端口号
- 如果不在本主机上也需要给相应的提示信息
增强版代码实现
/* nslookupAdvanced.java */
import java.net.*;
import java.util.regex.Pattern;
import java.io.*;
public class nslookupAdvanced {
public static void main(String[] args) {
if (args.length > 0) {
for (int i = 0; i < args.length; i++) {
System.out.println("\n> " + args[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 {
boolean isEmptyLine = false;
while (true) {
if (isEmptyLine){
isEmptyLine = false;
System.out.print("> ");
} else
System.out.print("\n> ");
String host = in.readLine();
if (host.equalsIgnoreCase("exit")) {
break;
} else if (host.isEmpty()){
isEmptyLine = true;
continue;
}
lookup(host);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void lookup(String host) {
if(isDomain(host)) {
nat(host, true);
decideNI(host);
} else {
nat(host, false);
decideNI(host);
}
}
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]*");
//Pattern pattern = Pattern.compile("^[0-9]+(.[0-9]*)?$");
return pattern.matcher(str).matches();
}
private static void nat(String host, boolean isDomain) {
try {
if (host.equals("127.0.0.1")){
System.out.println("Name: localhost");
return;
}
InetAddress[] address = InetAddress.getAllByName(host);
if (isDomain || host.equals(address[0].getHostName())) {
for (int i = 0; i < address.length; i++) {
System.out.println("Address: " + address[i].getHostAddress());
}
} else {
System.out.println("Name: " + address[0].getHostName());
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
private static void decideNI(String host) {
try {
InetAddress address = InetAddress.getByName(host);
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
if (ni != null) {
String niName = ni.getName();
String[] niDisplayName = ni.getDisplayName().split(" ");
System.out.println("This is local address " + niName +
niDisplayName[niDisplayName.length - 1] + ".");
} else {
System.out.println("This is not local address.");
}
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
增强版运行结果
输入一个绑定到多个IP地址上的域名的结果

输入IP地址的结果

Java实现Internet地址获取的更多相关文章
- Java根据ip地址获取Mac地址,Java获取Mac地址
Java根据ip地址获取Mac地址,Java获取Mac地址 >>>>>>>>>>>>>>>>>&g ...
- java根据ip地址获取详细地域信息的方法
通过淘宝IP地址库获取IP位置(也可以使用新浪的) 请求接口(GET):http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串] 响应信息:(jso ...
- java通过IP地址获取物理位置
import java.io.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern ...
- Java根据IP地址获取MAC地址
先使用ping -n 2 10.0.0.1 命令,如果返回的结果中含有TTL字符,证明ping 10.0.0.1是能ping通的,即可达的.如果在Linux机器上请使用 ping -c 2 10.0 ...
- java通过传送地址获取坐标
package com.action; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputS ...
- Java网络编程之查找Internet地址
一.概述 连接到Internet上计算机都有一个称为Internet地址或IP地址的唯一的数来标识.由于IP很难记住,人们设计了域名系统(DNS),DNS可以将人们可以记忆的主机名与计算机可以记忆的I ...
- java工具类(一)之服务端java实现根据地址从百度API获取经纬度
服务端java实现根据地址从百度API获取经纬度 代码: package com.pb.baiduapi; import java.io.BufferedReader; import java.io. ...
- java根据地址获取百度API经纬度
java根据地址获取百度API经纬度(详细文档) public void getLarLng(String address) throws Exception { String ak = " ...
- JAVA从本机获取IP地址
JAVA从本机获取IP地址 论述: 此篇博客是在工作的时候,需要获得当前网络下面正确的ip地址,在网上查阅很多博客,网上一个比较普遍的说法是通过InetAddress.getLocalHost().g ...
随机推荐
- Windows phone 8 学习笔记(3) 通信(转)
Windows phone 8 可利用的数据通信方式比较广泛,在硬件支持的前提下,我们可以利用WiFi.蓝牙.临近感应等多种方式.数据交互一般通过套接字来完成,我们将在本文详细的分析. 快速导航:一. ...
- console.log在线调试
前端开发人员工作有时候会用到console.log,PC端直接能打开开发者工具.但是移动端就不太方便了,为此提供一种简单的方法,只需2步: 1.打开http://jsconsole.com/ 输入: ...
- HBase -ROOT-和.META.表结构
在HBase中,大部分的操作都是在RegionServer完成的,Client端想要插入,删除,查询数据都需要先找到相应的RegionServer.什么叫相应的RegionServer?就是管理你要操 ...
- Recover damage pictures to see the crime scene
Few people know that when you take photos there is also a thumbnail embeded inside the file, even so ...
- Acronis 备份使用
1:至Acronis 中国官网,可了解到:Acronis True Image 是PC产品,Acronis Backup是企业产品 2:Acronis Backup 11.5.0.39029 :htt ...
- MyBatis学习系列三——结合Spring
目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring MyBatis在项目中应用一般都要结合Spring,这一章主要把MyBat ...
- shell script 基本语法
几个符号的意义$#:代表后接的参数『个数』,以上表为例这裡显示为『 4 』:$@:代表『 "$1" "$2" "$3" "$4&q ...
- CentOS学习笔记--目录配置
Linux目录配置 类Linux的目录看上去差不多,为什么? 以下内容节选自l 鸟哥的 Linux 私房菜 -- 基础学习篇目录 第六章.Linux 的文件权限与目录配置 3. Linux目录配 ...
- 通过jquery 获取文本框的聚焦和失焦方法
我还是喜欢用jquery来实现,不管页面中多少个输入框需要实现聚焦,失焦,都公有,我常用的方法是: 遍历该页面中的input框,获取输入框中的val值,当该输入框聚焦的时候跟存放的oldValue值进 ...
- 2014年第五届蓝桥杯试题C/C++程序设计B组——李白打酒
题目描述: 标题:李白打酒 话说大诗人李白,一生好饮.幸好他从不开车. 一天,他提着酒壶,从家里出来,酒壶中有酒2斗.他边走边唱: 无事街上走,提壶去打酒. 逢店加一倍,遇花喝一斗. 这一路上,他一共 ...