用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
您可能也喜欢
评论
同时评论给
同时评论给原文作者
500/0
©2012 Baidu a