java 获取本地 mac 地址

主要参考:Java获取本机MAC地址/IP地址/主机名

做的更改:

1.我的windows是中文版,程序中获取mac时是按照physical address 获取的,添加上"物理地址";

2.获取到第一个mac之后继续循环buffer,获取其他网卡的mac(无线网卡、以太网卡、虚拟网卡...),但我的机器获取到5个mac地址,不知道为什么,我知道的有一个虚拟网卡,一个以太,一个无限,另外两个不知道哪里来的。

3.中文系统获取bufferedReader 后的编码不正确,添加编码“GBK"

  1 package cn.com.sinosoft.monitor.vo;
  2 
  3         import java.io.BufferedReader;
  4         import java.io.IOException;
  5         import java.io.InputStreamReader;
  6         import java.net.InetAddress;
  7         import java.net.NetworkInterface;
  8         import java.net.UnknownHostException;
  9         import java.util.ArrayList;
 10         import java.util.List;
 11         import java.util.Map;
 12 
 13 /**
 14  * @className: SystemTool
 15  * @description: 与系统相关的一些常用工具方法. 目前实现的有:获取MAC地址、IP地址、主机名
 16  * @author: 笑遍世界
 17  * @createTime: 2010-11-13 下午08:03:44
 18  */
 19 public class Tool {
 20 
 21     /**
 22      * 获取当前操作系统名称.
 23      * return 操作系统名称 例如:windows xp,linux 等.
 24      */
 25     private static String getOSName() {
 26         return System.getProperty("os.name").toLowerCase();
 27     }
 28 
 29     /**
 30      * 获取unix网卡的mac地址.
 31      * 非windows的系统默认调用本方法获取.如果有特殊系统请继续扩充新的取mac地址方法.
 32      *
 33      * @return mac地址
 34      */
 35     private static String getUnixMACAddress() {
 36         String mac = null;
 37         BufferedReader bufferedReader = null;
 38         Process process;
 39         try {
 40             process = Runtime.getRuntime().exec("ifconfig eth0");// linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
 41             bufferedReader = new BufferedReader(new InputStreamReader(process
 42                     .getInputStream()));
 43             String line;
 44             int index;
 45             while ((line = bufferedReader.readLine()) != null) {
 46                 index = line.toLowerCase().indexOf("hwaddr");// 寻找标示字符串[hwaddr]
 47                 if (index >= 0) {// 找到了
 48                     mac = line.substring(index + "hwaddr".length() + 1).trim();//  取出mac地址并去除2边空格
 49                     break;
 50                 }
 51             }
 52         } catch (IOException e) {
 53             e.printStackTrace();
 54         } finally {
 55             try {
 56                 if (bufferedReader != null) {
 57                     bufferedReader.close();
 58                 }
 59             } catch (IOException e1) {
 60                 e1.printStackTrace();
 61             }
 62         }
 63 
 64         return mac;
 65     }
 66 
 67     /**
 68      * 获取widnows网卡的mac地址.
 69      *
 70      * @return mac地址
 71      */
 72     private static List<String> getWindowsMACAddress() {
 73         List<String> macList = new ArrayList<String>();
 74         String mac = null;
 75         BufferedReader bufferedReader = null;
 76         Process process;
 77         try {
 78             // windows下的命令,显示信息中包含有mac地址信息
 79             process = Runtime.getRuntime().exec("ipconfig /all");
 80             bufferedReader = new BufferedReader(new InputStreamReader(process
 81                     .getInputStream(),"GBK"));
 82             String line;
 83             int index;
 84             while ((line = bufferedReader.readLine()) != null) {
 85                 index = line.toLowerCase().indexOf("physical address");// 寻找标示字符串[physical address]
 86 
 87                 if (index == -1) {
 88                     index = line.indexOf("物理地址");
 89                 }
 90                 if (index >= 0) {// 找到了
 91                     index = line.indexOf(":");// 寻找":"的位置
 92                     if (index >= 0) {
 93                         mac = line.substring(index + 1).trim();//  取出mac地址并去除2边空格
 94                     }
 95                     macList.add(mac);
 96                 }
 97             }
 98 
 99         } catch (IOException e) {
100             e.printStackTrace();
101         } finally {
102             try {
103                 if (bufferedReader != null) {
104                     bufferedReader.close();
105                 }
106             } catch (IOException e1) {
107                 e1.printStackTrace();
108             }
109         }
110 
111         return macList;
112     }
113 
114     /**
115      * @return 本机主机名
116      */
117     private static String getHostName() {
118         InetAddress ia = null;
119         try {
120             ia = InetAddress.getLocalHost();
121         } catch (UnknownHostException e) {
122             e.printStackTrace();
123         }
124         if (ia == null) {
125             return "some error..";
126         } else
127             return ia.getHostName();
128     }
129 
130     /**
131      * @return 本机IP 地址
132      */
133     private static String getIPAddress() {
134         InetAddress ia = null;
135         try {
136             ia = InetAddress.getLocalHost();
137         } catch (UnknownHostException e) {
138             e.printStackTrace();
139         }
140         if (ia == null) {
141             return "some error..";
142         } else
143             return ia.getHostAddress();
144     }
145 
146     /**
147      * 测试用的main方法.
148      *
149      * @param argc 运行参数.
150      */
151     public static void main(String[] argc) {
152         String os = getOSName();
153         System.out.println("OS Type:" + os);
154         if (os.contains("windows")) {
155             //本地是windows
156             List<String> mac = getWindowsMACAddress();
157             System.out.println("MAC Address:" + mac);
158         } else {
159             //本地是非windows系统 一般就是unix
160             String mac = getUnixMACAddress();
161             System.out.println(mac);
162         }
163         System.out.println("HostName:" + getHostName());
164         System.out.println("IPAddress:" + getIPAddress());
165 
166 
167         //这个更简单
168         Map<String, String> map = System.getenv();
169         String userName = map.get("USERNAME");// 获取用户名
170         String computerName = map.get("COMPUTERNAME");// 获取计算机名
171         String userDomain = map.get("USERDOMAIN");// 获取计算机域名
172 
173 
174         System.out.println(userName);
175         System.out.println(computerName);
176         System.out.println(userDomain);
177 
178         System.out.println(GetMac());
179     }
180 
181     static String GetMac(String... ip)
182     {
183         List<String> macList = new ArrayList<String>();
184         InetAddress ia;
185         byte[] mac = null;
186         try {
187             //获取本地IP对象
188             ia = InetAddress.getLocalHost();
189             //获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
190             mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
191         } catch (Exception e) {
192             e.printStackTrace();
193         }
194         //下面代码是把mac地址拼装成String
195         StringBuffer sb = new StringBuffer();
196         for(int i=0;i<mac.length;i++){
197             if(i!=0){
198                 sb.append("-");
199             }
200             //mac[i] & 0xFF 是为了把byte转化为正整数
201             String s = Integer.toHexString(mac[i] & 0xFF);
202             sb.append(s.length()==1?0+s:s);
203         }
204 
205         //把字符串所有小写字母改为大写成为正规的mac地址并返回
206         return sb.toString().toUpperCase();
207     }
208 
209 }

ps:最后的GetMac参考 java获取Mac地址

原文地址:https://www.cnblogs.com/peng18/p/9041141.html