用java取得mac地址_chinalwb的空间_百度空间

Posted on

用java取得mac地址chinalwb的空间百度空间

分享到

百度分享

扫描打印一体机 请找 QQ: 751510471, 329055754 EPSON ¥1,2500 长期合作 有优惠

2010-07-20 17:14

用java取得mac地址

项目中用到的一个方法 比较粗糙 没做整理 先记录下来 String getMacAddress() { String macadd = "Any"; if (macaddress != null){ //System.out.println("Returning static macaddress"); return macaddress; } // On Solaris if (Key.getRuntimeOS() == LicUtil.SUNOS){ String cmd = "/usr/bin/hostid"; BufferedReader br = null; try { Process p = Runtime.getRuntime().exec(cmd); br = new BufferedReader(new InputStreamReader(p.getInputStream())); String sLine = null; if ((sLine = br.readLine()) != null) { macadd = sLine.trim(); macaddress = macadd; } } catch (Exception ex) { } finally { try { if (br != null) br.close(); } catch (Exception ex) {} } } else if (Key.getRuntimeOS() == LicUtil.HPUX){ // On HP String cmd = "/usr/sbin/lanscan -a"; BufferedReader br = null; try { Process p = Runtime.getRuntime().exec(cmd); br = new BufferedReader(new InputStreamReader(p.getInputStream())); String sLine = null; if ((sLine = br.readLine()) != null) { macadd = sLine.trim(); macaddress = macadd; } } catch (Exception ex) { } finally { try { if (br != null) br.close(); } catch (Exception ex) {} } } else if (Key.getRuntimeOS() == LicUtil.IBMAIX){ // On IBMAIX String cmd = "/usr/bin/uname -m"; BufferedReader br = null; try { Process p = Runtime.getRuntime().exec(cmd); br = new BufferedReader(new InputStreamReader(p.getInputStream())); String sLine = null; if ((sLine = br.readLine()) != null){ macadd = sLine.trim(); macaddress = macadd; } } catch (Exception ex) { } finally { try { if (br != null) br.close(); } catch (Exception ex) {} } } else if (Key.getRuntimeOS() == LicUtil.LINUX){ // On Linux String cmd = "/sbin/ifconfig eth0"; BufferedReader br = null; try { Process p = Runtime.getRuntime().exec(cmd); br = new BufferedReader(new InputStreamReader(p.getInputStream())); String sLine = null; if ((sLine = br.readLine()) != null) { sLine = sLine.trim(); StringTokenizer st = new StringTokenizer(sLine); while (st.hasMoreTokens()) { String tmp = st.nextToken(); if (tmp.equals("HWaddr")) { macadd = st.nextToken(); macaddress = macadd; break; } } } // Add IP address - specail case on Linux if ((sLine = br.readLine()) != null) { sLine = sLine.trim(); StringTokenizer st = new StringTokenizer(sLine); while (st.hasMoreTokens()) { String tmp = st.nextToken(); if (tmp.startsWith("addr:")) { setIP(tmp.substring(5)); break; } } } } catch (Exception ex) { } finally { try { if (br != null) br.close(); } catch (Exception ex) {} } } else if (Key.getRuntimeOS() == LicUtil.WIN){ // On Windows // Yes, popup String cmd = "cmd /c start /min ipconfig /all"; // 0519 more popup thane 0518 String cmd = "cmd /c ipconfig /all"; // 0518 Still see Popup from installer String cmd = "start /min ipconfig /all"; // Get Error in MacAddress
// Pop Up Windows in installer String cmd = "ipconfig /all"; BufferedReader br = null; try { Process p = Runtime.getRuntime().exec(cmd); br = new BufferedReader(new InputStreamReader(p.getInputStream())); String sLine = null; while ((sLine = br.readLine()) != null) { sLine = sLine.trim(); if (sLine.indexOf("Physical Address") != -1){ StringTokenizer winst = new StringTokenizer(sLine, ":"); winst.nextToken(); macadd = winst.nextToken().trim(); macaddress = macadd; //System.out.println("Setting static macaddress"); break; } continue; } } catch (Exception ex) { } finally { try { if (br != null) br.close(); } catch (Exception ex) {} } } return macadd; } /#Java

分享到:

举报 浏览(213) 评论 转载

您可能也喜欢

评论

同时评论给

同时评论给原文作者

发布

500/0

收起|查看更多

帮助中心 | 空间客服 | 投诉中心 | 空间协议

©2012 Baidu a

b c

d e

f

希望本站内容对您有点用处,有什么疑问或建议请在后面留言评论
转载请注明作者(RobinChia)和出处 It so life ,请勿用于任何商业用途