java中通过网卡名称获取IP地址
package me.xuzs.sso.test;import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;public class InternetTest {
public static void main(String[] args) {
String netCard = "lo";
try {
Enumeration<NetworkInterface> netInterfaces = NetworkInterface
.getNetworkInterfaces();
if (netInterfaces.hasMoreElements()) {
NetworkInterface netInterface = netInterfaces.nextElement();
if (netCard.equals(netInterface.getName())) {
// 子接口,linux下会取到父接口??
Enumeration<NetworkInterface> subnetInterfaces = netInterface
.getSubInterfaces();
while (subnetInterfaces.hasMoreElements()) {
NetworkInterface subnetInterface = subnetInterfaces
.nextElement();
System.out.println(subnetInterface.getName());
Enumeration<InetAddress> subaddresses = netInterface
.getInetAddresses();
while (subaddresses.hasMoreElements()) {
InetAddress subaddress = subaddresses.nextElement();
System.out.println(subaddress.getHostAddress());
}
}
// 打印接口下所有IP
System.out.println(netInterface.getName());
Enumeration<InetAddress> addresses = netInterface
.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
System.out.println(address.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}
您可能感兴趣的文章:
- java调用IP138获取本机IP以及通过IP地址获取区域名称
- [JAVA100例]042、获取计算机名称和IP地址(java100例)
- C++ 中通过GetAdaptersInfo获取网卡配置和Ip地址信息
- java通过ip获取网卡MAC地址
- JAVA获取本机网卡IP地址
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存
- c#获取主机名称网卡MACIP地址
- java通过Ip地址获取地理位置
- 通过 Java 获取发起访问的客户端的IP地址
- c#获取主机名称网卡MACIP地址
- Java获取访问设备的ip地址,设备名称
- 通过jquery 获取用户当前所在的城市名称和IP地址
- Linux下通过shell获取网卡的ip地址和mac地址
- C++ 中通过GetAdaptersInfo获取网卡配置和Ip地址信息
- JAVA中通过InetAddress类获取主机名与IP地址
- Java——通过反射获取函数参数名称
- C++ 中通过GetAdaptersInfo获取网卡配置和Ip地址信息
- java 通过反射获取方法参数列表名称
- Android 通过resource名称或者layout中定义的id name 去获取R.java中id,得到控件
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存