try {
ip = InetAddressgetLocalHost();
NetworkInterface network = NetworkInterfacegetByInetAddress(ip);
if (network != null) {
byte[] mac = networkgetHardwareAddress();
if(mac != null) {
Systemoutprint("MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < maclength; i++) {
sbappend(Stringformat("%02X%s", mac[i], (i < maclength - 1) "-" : ""));
}
Systemoutprintln(sbtoString());
}
} catch (UnknownHostException e) {
eprintStackTrace();
} catch (SocketException e) {
eprintStackTrace();
}
这个网上很多,主要是机器必须支持ICMP和NETBIOS协议。你参考一下:
public String getIP()
{
InetAddress inet;
try {
inet =
InetAddressgetLocalHost();
InetAddressgetByName("");
return
inetgetHostAddress();
} catch (UnknownHostException e) {
// TODO
Auto-generated catch block
eprintStackTrace();
}
return "";
}
来走一个
public static void main(String[] args) throws UnknownHostException,SocketException{InetAddress inetAddress = InetAddressgetLocalHost();
//获取网卡,获取地址
byte[] mac = NetworkInterfacegetByInetAddress(inetAddress)getHardwareAddress();
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < maclength; i++){
if (i != 0){
sbappend("-");
}
//字节转换为整数
int temp = mac[i] & 0xff;
String str = IntegertoHexString(temp);
if (strlength() == 1){
sbappend("0" + str);
}else{
sbappend(str);
}
}
Systemoutprintln("本机MAC地址:" + sbtoString()toUpperCase());
}
输出:
本机MAC地址:B8-88-E3-FA-03-39以上就是关于在linux用java根据ip获得mac地址全部的内容,包括:在linux用java根据ip获得mac地址、在Linux系统下用Java语言获取客户端的IP地址,MAC地址,客户端的主机名称、java怎么生成本机mac地址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)