有谁知道linux系统环境下,怎样在后台才能获取到mac地址?

发布网友 发布时间:2022-04-23 12:26

我来回答

2个回答

热心网友 时间:2022-05-26 12:22

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* 与系统相关的一些常用工具方法.
*
* @author stephen
* @version 1.0.0
*/
public class SystemTool {

/**
* 获取当前操作系统名称.
* return 操作系统名称 例如:windows xp,linux 等.
*/
public static String getOSName() {
return System.getProperty("os.name").toLowerCase();
}

/**
* 获取unix网卡的mac地址.
* 非windows的系统默认调用本方法获取.如果有特殊系统请继续扩充新的取mac地址方法.
* @return mac地址
*/
public static String getUnixMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ifconfig eth0");// linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("hwaddr");// 寻找标示字符串[hwaddr]
if (index >= 0) {// 找到了
mac = line.substring(index +"hwaddr".length()+ 1).trim();// 取出mac地址并去除2边空格
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}

return mac;
}

/**
* 获取widnows网卡的mac地址.
* @return mac地址
*/
public static String getWindowsMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ipconfig /all");// windows下的命令,显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("physical address");// 寻找标示字符串[physical address]
if (index >= 0) {// 找到了
index = line.indexOf(":");// 寻找":"的位置
if (index>=0) {
mac = line.substring(index + 1).trim();// 取出mac地址并去除2边空格
}
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}

return mac;
}

/**
* 测试用的main方法.
*
* @param argc
* 运行参数.
*/
public static void main(String[] argc) {
String os = getOSName();
System.out.println(os);
if(os.startsWith("windows")){
//本地是windows
String mac = getWindowsMACAddress();
System.out.println(mac);
}else{
//本地是非windows系统 一般就是unix
String mac = getUnixMACAddress();
System.out.println(mac);
}
}
}

-------------------------------------------------------------------------

本程序可以正确获得本机IP地址和网卡"eth0"的MAC地址,已经在windowsXP和ubuntu-Linux上测试过
(注意:如果有多块网卡,可能出错)
下面给出代码:
import java.net.*;import java.util.*;
public class Test { public static void main(String[] args) { Test t = new Test(); System.out.println(t.getLocalIP()); System.out.println(t.getMacAddr()); }
public String getMacAddr() { String MacAddr = ""; String str = ""; try { NetworkInterface NIC = NetworkInterface.getByName("eth0"); byte[] buf = NIC.getHardwareAddress(); for (int i = 0; i < buf.length; i++) { str = str + byteHEX(buf[i]); } MacAddr = str.toUpperCase(); } catch (SocketException e) { e.printStackTrace(); System.exit(-1); } return MacAddr; }
public String getLocalIP() { String ip = ""; try { Enumeration<?> e1 = (Enumeration<?>) NetworkInterface .getNetworkInterfaces(); while (e1.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) e1.nextElement(); if (!ni.getName().equals("eth0")) { continue; } else { Enumeration<?> e2 = ni.getInetAddresses(); while (e2.hasMoreElements()) { InetAddress ia = (InetAddress) e2.nextElement(); if (ia instanceof Inet6Address) continue; ip = ia.getHostAddress(); } break; } } } catch (SocketException e) { e.printStackTrace(); System.exit(-1); } return ip; }
/* 一个将字节转化为十六进制ASSIC码的函数 */ public static String byteHEX(byte ib) { char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; char[] ob = new char[2]; ob[0] = Digit[(ib >>> 4) & 0X0F]; ob[1] = Digit[ib & 0X0F]; String s = new String(ob); return s; }}

热心网友 时间:2022-05-26 13:40

你是要啥MAC地址?本机的?如果是本机的话可以用命令ipconfig得到包含mac地址的网络参数,在提取出啦就是追问我问的是具体代码怎么写!

追答找到一个更简单的方法来实现:cat /sys/class/net/eth0/address 注意是否需要更换网卡名称。Ubuntu 下通过

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com