java 获取ip地址和网络接口

时间:2023-03-08 22:44:54

*/

.hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
color: #333;
background: #f8f8f8;
}

.hljs-comment,
.hljs-template_comment,
.diff .hljs-header,
.hljs-javadoc {
color: #998;
font-style: italic;
}

.hljs-keyword,
.css .rule .hljs-keyword,
.hljs-winutils,
.javascript .hljs-title,
.nginx .hljs-title,
.hljs-subst,
.hljs-request,
.hljs-status {
color: #333;
font-weight: bold;
}

.hljs-number,
.hljs-hexcolor,
.ruby .hljs-constant {
color: #099;
}

.hljs-string,
.hljs-tag .hljs-value,
.hljs-phpdoc,
.tex .hljs-formula {
color: #d14;
}

.hljs-title,
.hljs-id,
.coffeescript .hljs-params,
.scss .hljs-preprocessor {
color: #900;
font-weight: bold;
}

.javascript .hljs-title,
.lisp .hljs-title,
.clojure .hljs-title,
.hljs-subst {
font-weight: normal;
}

.hljs-class .hljs-title,
.haskell .hljs-type,
.vhdl .hljs-literal,
.tex .hljs-command {
color: #458;
font-weight: bold;
}

.hljs-tag,
.hljs-tag .hljs-title,
.hljs-rules .hljs-property,
.django .hljs-tag .hljs-keyword {
color: #000080;
font-weight: normal;
}

.hljs-attribute,
.hljs-variable,
.lisp .hljs-body {
color: #008080;
}

.hljs-regexp {
color: #009926;
}

.hljs-symbol,
.ruby .hljs-symbol .hljs-string,
.lisp .hljs-keyword,
.tex .hljs-special,
.hljs-prompt {
color: #990073;
}

.hljs-built_in,
.lisp .hljs-title,
.clojure .hljs-built_in {
color: #0086b3;
}

.hljs-preprocessor,
.hljs-pragma,
.hljs-pi,
.hljs-doctype,
.hljs-shebang,
.hljs-cdata {
color: #999;
font-weight: bold;
}

.hljs-deletion {
background: #fdd;
}

.hljs-addition {
background: #dfd;
}

.diff .hljs-change {
background: #0086b3;
}

.hljs-chunk {
color: #aaa;
}

#container {
padding: 15px;
}
pre {
border: 1px solid #ccc;
border-radius: 4px;
display: block;
background-color: #f8f8f8;
}
pre code {
white-space: pre-wrap;
}
.hljs,
code {
font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;
}
:not(pre) > code {
padding: 2px 4px;
font-size: 90%;
color: #c7254e;
background-color: #f9f2f4;
white-space: nowrap;
border-radius: 4px;
}
-->

网络相关对象在java.net包中。

1.获取主机对象InetAddress

//获取本地主机对象
InetAddress host = InetAddress.getLocalHost(); //根据ip地址或主机名获取主机对象,以主机名获取主机时需要DNS解析
InetAddress host = InetAddress.getByName("192.168.100.124");
InetAddress host = InetAddress.getByName("www.baidu.com");

2.获取主机对象的ip地址和主机名(需要dns解析主机名)

host.getHostAddress();
host.getHostName();

3.获取本机所有接口NetworkInterface并遍历

//返回数据类型为Enumeration
Enumeration<NetworkInterface> enu = NetworkInterface.getNetworkInterfaces();
while(enu.hasMoreElements){
NetworkInterface inet = enu.nextElement();
String intName = inet.getName();
}

由于一个接口上可能有多个子接口(辅助ip,如eth0:1),因此根据某个接口,可以得到该接口的所有ip地址枚举集合(同时包括Ipv4和ipv6接口)。

Enumeration<InetAddress> net_list = inet.getInetAddresses();
while(net_list.hasMoreElements){
InetAddress net = net_list.nextElement();
String ip = net.getHostAddress();
}

可以使用Collections.list()方法将Enumeration类型转换为ArrayList集合的数据结构,然后使用Itreator遍历器遍历。

以下是获取本机所有接口名称和这些接口上的ipv4地址的方法(适用于Windows和Linux)。

import java.net.*;
import java.util.*; public class EnumDemo {
public static void main(String[] args) {
try {
//获取所有接口,并放进枚举集合中,然后使用Collections.list()将枚举集合转换为ArrayList集合
Enumeration<NetworkInterface> enu = NetworkInterface.getNetworkInterfaces();
ArrayList<NetworkInterface> arr = Collections.list(enu);
for(Iterator<NetworkInterface> it = arr.iterator();it.hasNext();) {
NetworkInterface ni = it.next();
String intName = ni.getName(); //获取接口名
//获取每个接口中的所有ip网络接口集合,因为可能有子接口
ArrayList<InetAddress> inets = Collections.list(ni.getInetAddresses());
for(Iterator<InetAddress> it1 = inets.iterator();it1.hasNext();) {
InetAddress inet = it1.next();
//只筛选ipv4地址,否则会同时得到Ipv6地址
if(inet instanceof Inet4Address) {
String ip = inet.getHostAddress();
System.out.printf("%-10s %-5s %-6s %-15s\n", "InetfaceName:",intName,"| IPv4:",ip);
}
}
}
} catch (SocketException s) {
s.printStackTrace();
}
}
}

注:若您觉得这篇文章还不错请点击右下角推荐,您的支持能激发作者更大的写作热情,非常感谢!