Jev*_*vie 25 java sockets mac-address
我可以用
ip = InetAddress.getLocalHost();
NetworkInterface.getByInetAddress(ip);
Run Code Online (Sandbox Code Playgroud)
获取mac地址,但是如果我在离线机器中使用此代码则不起作用.
那么,我怎样才能获得Mac地址?
phi*_*hag 30
使用Java 6+,您可以使用NetworkInterface.getHardwareAddress.
请记住,计算机可以没有网卡,特别是如果它是嵌入式或虚拟的.它也可以有多个.您可以获得所有网卡的列表NetworkInterface.getNetworkInterfaces().
Jes*_*res 22
通过我在这里找到的所有可能的解决方案和另一个回复,我将为我的解决方案做出贡献.您需要使用包含"ip"或"mac"的String指定参数,具体取决于您需要检查的内容.如果计算机没有接口,那么它将返回一个包含null的String,否则将返回一个包含你要求的字符串(ip地址或mac).
如何使用它:
System.out.println("Ip: " + GetNetworkAddress.GetAddress("ip"));
System.out.println("Mac: " + GetNetworkAddress.GetAddress("mac"));
Run Code Online (Sandbox Code Playgroud)
结果(如果计算机有网卡):
Ip: 192.168.0.10
Mac: 0D-73-ED-0A-27-44
Run Code Online (Sandbox Code Playgroud)
结果(如果计算机没有网卡):
Ip: null
Mac: null
Run Code Online (Sandbox Code Playgroud)
这是代码:
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class GetNetworkAddress {
public static String GetAddress(String addressType) {
String address = "";
InetAddress lanIp = null;
try {
String ipAddress = null;
Enumeration<NetworkInterface> net = null;
net = NetworkInterface.getNetworkInterfaces();
while (net.hasMoreElements()) {
NetworkInterface element = net.nextElement();
Enumeration<InetAddress> addresses = element.getInetAddresses();
while (addresses.hasMoreElements() && element.getHardwareAddress().length > 0 && !isVMMac(element.getHardwareAddress())) {
InetAddress ip = addresses.nextElement();
if (ip instanceof Inet4Address) {
if (ip.isSiteLocalAddress()) {
ipAddress = ip.getHostAddress();
lanIp = InetAddress.getByName(ipAddress);
}
}
}
}
if (lanIp == null)
return null;
if (addressType.equals("ip")) {
address = lanIp.toString().replaceAll("^/+", "");
} else if (addressType.equals("mac")) {
address = getMacAddress(lanIp);
} else {
throw new Exception("Specify \"ip\" or \"mac\"");
}
} catch (UnknownHostException ex) {
ex.printStackTrace();
} catch (SocketException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
return address;
}
private static String getMacAddress(InetAddress ip) {
String address = null;
try {
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
address = sb.toString();
} catch (SocketException ex) {
ex.printStackTrace();
}
return address;
}
private static boolean isVMMac(byte[] mac) {
if(null == mac) return false;
byte invalidMacs[][] = {
{0x00, 0x05, 0x69}, //VMWare
{0x00, 0x1C, 0x14}, //VMWare
{0x00, 0x0C, 0x29}, //VMWare
{0x00, 0x50, 0x56}, //VMWare
{0x08, 0x00, 0x27}, //Virtualbox
{0x0A, 0x00, 0x27}, //Virtualbox
{0x00, 0x03, (byte)0xFF}, //Virtual-PC
{0x00, 0x15, 0x5D} //Hyper-V
};
for (byte[] invalid: invalidMacs){
if (invalid[0] == mac[0] && invalid[1] == mac[1] && invalid[2] == mac[2]) return true;
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
更新时间02/05/2017:感谢@mateuscb在帖子中如何确定Java中的Internet网络接口,遗憾的是之前没有在该帖子上获得任何upvote,但他为此更新做出了贡献.
该方法已被改进,以跳过虚拟机网卡(最流行的VM软件)