JDK1.6 获取操作系统IP地址及对应物理mac地址
2012-03-30 23:21
756 查看
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class OSInfo {
public static void listIPMac() throws SocketException{
//首先获取到机器的所有网络接口
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
NetworkInterface ni = null;
//网卡的物理mac地址二进制格式
byte[] macbyte = null;
//网卡的物理mac地址
String mac = null;
//遍历所有的网络接口
while(nis.hasMoreElements()){
ni = nis.nextElement();
//当不是回环网卡,并且启动,并且不是虚拟网卡
if(!ni.isLoopback() && ni.isUp() && !ni.isVirtual()){
macbyte = ni.getHardwareAddress();
//把二进制格式化成十六进制
mac = bytesToHexString(macbyte, macbyte.length);
Enumeration<InetAddress> ias = ni.getInetAddresses();
InetAddress ia = null;
while(ias.hasMoreElements()){
ia = ias.nextElement();
//不是 127.0.0.1 回环IP
if(!ia.isLoopbackAddress()){
System.out.println(ia.getHostAddress()+" = " + mac.toUpperCase());
}
}
}
}
}
/*
* 二进制转十六进制
*/
public static String bytesToHexString(byte[] src, int length) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
public static void main(String[] args) {
try {
listIPMac();
} catch (SocketException e) {
e.printStackTrace();
}
}
}
运行结果:
2001:0:4137:9e76:3804:5134:2249:d190 = 00000000000000e0
fe80:0:0:0:3804:5134:2249:d190%11 = 00000000000000e0
fe80:0:0:0:b8c8:25a3:8cb6:a0b7%13 = 0015af5*****
192.168.1.101 = 0015af5*****
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class OSInfo {
public static void listIPMac() throws SocketException{
//首先获取到机器的所有网络接口
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
NetworkInterface ni = null;
//网卡的物理mac地址二进制格式
byte[] macbyte = null;
//网卡的物理mac地址
String mac = null;
//遍历所有的网络接口
while(nis.hasMoreElements()){
ni = nis.nextElement();
//当不是回环网卡,并且启动,并且不是虚拟网卡
if(!ni.isLoopback() && ni.isUp() && !ni.isVirtual()){
macbyte = ni.getHardwareAddress();
//把二进制格式化成十六进制
mac = bytesToHexString(macbyte, macbyte.length);
Enumeration<InetAddress> ias = ni.getInetAddresses();
InetAddress ia = null;
while(ias.hasMoreElements()){
ia = ias.nextElement();
//不是 127.0.0.1 回环IP
if(!ia.isLoopbackAddress()){
System.out.println(ia.getHostAddress()+" = " + mac.toUpperCase());
}
}
}
}
}
/*
* 二进制转十六进制
*/
public static String bytesToHexString(byte[] src, int length) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
public static void main(String[] args) {
try {
listIPMac();
} catch (SocketException e) {
e.printStackTrace();
}
}
}
运行结果:
2001:0:4137:9e76:3804:5134:2249:d190 = 00000000000000e0
fe80:0:0:0:3804:5134:2249:d190%11 = 00000000000000e0
fe80:0:0:0:b8c8:25a3:8cb6:a0b7%13 = 0015af5*****
192.168.1.101 = 0015af5*****
相关文章推荐
- 获取硬件信息的delphi源码(CPUID、操作系统、Mac物理地址、计算机名称、IP地址、用户名)
- android获取本机的IP地址和mac物理地址
- 根据IP地址获取对应的MAC地址
- android获取本机的IP地址和mac物理地址
- 在局域网内中 通过IP地址得到对应的主机名及MAC物理地址
- 获取本地PC的IP地址和物理Mac地址(C++代码实现)
- java(jdk1.4、jdk1.6)获取mac地址的方法(winxp、win2003、win7、linux)
- android 获取本机的IP地址和mac物理地址的实现方法
- javascript如何通过访问者ip地址获取ip地址对应的物理地址API接口
- Linux C 网络编程 - 获取本地 ip 地址,mac,通过域名获取对应的 ip
- VC 物理MAC地址的获取方法
- 获取指定IP地址对应的物理位置
- Android获取本机Mac地址及IP地址方法
- Android开发中获取本机Mac地址和IP地址的方法
- Android获取本机Mac地址及IP地址方法
- 枚举和获取网卡物理MAC地址
- Linux下如何在进程中获取虚拟地址对应的物理地址【转】
- 通过IP地址获取MAC地址
- 用纯Java代码根据IP获取windows和linux的MAC物理地址
- PHP获取客户端的网卡mac物理地址