java中获取mac 地址
2011年06月01日
通过IP地址获取远程MAC地址:
public static String getMacAddressIP(String remotePcIP) { String str = ""; String macAddress = ""; try { Process pp = Runtime.getRuntime().exec("nbtstat -A " + remotePcIP); InputStreamReader ir = new InputStreamReader(pp.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (int i = 1; i 1) { macAddress = str.substring( str.indexOf("MAC Address") + 14, str.length()); break; } } } } catch (IOException ex) { } return macAddress; }
在windows中获取MAC地址:
public class Test { public static void main(String[] args) { try { Process process = Runtime.getRuntime().exec("ipconfig /all"); InputStreamReader ir = new InputStreamReader(process .getInputStream()); LineNumberReader input = new LineNumberReader(ir); String line; while ((line = input.readLine()) != null) if (line.indexOf("Physical Address") > 0) { String MACAddr = line.substring(line.indexOf("-") - 2); System.out.println("MAC address = [" + MACAddr + "]"); } } catch (java.io.IOException e) { System.err.println("IOException " + e.getMessage()); } } }
linux下获取ip地址:
public static byte[] getIp() throws UnknownHostException { byte[] b = InetAddress.getLocalHost().getAddress(); Enumeration allNetInterfaces = null; try { allNetInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { e.printStackTrace(); } InetAddress ip = null; NetworkInterface netInterface = null; while (allNetInterfaces.hasMoreElements()) { netInterface = (NetworkInterface) allNetInterfaces.nextElement(); if (netInterface.getName().trim().equals("eth0")){ Enumeration addresses = netInterface.getInetAddresses(); while (addresses.hasMoreElements()) { ip = (InetAddress) addresses.nextElement(); } break; } } if (ip != null && ip instanceof Inet4Address) { return b = ip.getAddress(); } return b; }
NETBIOS,据说可以远程获取,且仅限于Windows主机,但我没有成功过,一跨网段就没戏。Linux下有工具nbtscan。
Process p = Runtime.getRuntime().exec("nbtscan -r " + ip); InputStreamReader ir = new InputStreamReader(p.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (int i = 1; i = 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; }
这是在JDK1.6以后的好方法
public String getMacAddress(String host) { String mac = ""; StringBuffer sb = new StringBuffer(); try { NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getB yName(host)); byte[] macs = ni.getHardwareAddress(); for(int i=0; i
2011年06月01日
通过IP地址获取远程MAC地址:
public static String getMacAddressIP(String remotePcIP) { String str = ""; String macAddress = ""; try { Process pp = Runtime.getRuntime().exec("nbtstat -A " + remotePcIP); InputStreamReader ir = new InputStreamReader(pp.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (int i = 1; i 1) { macAddress = str.substring( str.indexOf("MAC Address") + 14, str.length()); break; } } } } catch (IOException ex) { } return macAddress; }
在windows中获取MAC地址:
public class Test { public static void main(String[] args) { try { Process process = Runtime.getRuntime().exec("ipconfig /all"); InputStreamReader ir = new InputStreamReader(process .getInputStream()); LineNumberReader input = new LineNumberReader(ir); String line; while ((line = input.readLine()) != null) if (line.indexOf("Physical Address") > 0) { String MACAddr = line.substring(line.indexOf("-") - 2); System.out.println("MAC address = [" + MACAddr + "]"); } } catch (java.io.IOException e) { System.err.println("IOException " + e.getMessage()); } } }
linux下获取ip地址:
public static byte[] getIp() throws UnknownHostException { byte[] b = InetAddress.getLocalHost().getAddress(); Enumeration allNetInterfaces = null; try { allNetInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { e.printStackTrace(); } InetAddress ip = null; NetworkInterface netInterface = null; while (allNetInterfaces.hasMoreElements()) { netInterface = (NetworkInterface) allNetInterfaces.nextElement(); if (netInterface.getName().trim().equals("eth0")){ Enumeration addresses = netInterface.getInetAddresses(); while (addresses.hasMoreElements()) { ip = (InetAddress) addresses.nextElement(); } break; } } if (ip != null && ip instanceof Inet4Address) { return b = ip.getAddress(); } return b; }
NETBIOS,据说可以远程获取,且仅限于Windows主机,但我没有成功过,一跨网段就没戏。Linux下有工具nbtscan。
Process p = Runtime.getRuntime().exec("nbtscan -r " + ip); InputStreamReader ir = new InputStreamReader(p.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (int i = 1; i = 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; }
这是在JDK1.6以后的好方法
public String getMacAddress(String host) { String mac = ""; StringBuffer sb = new StringBuffer(); try { NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getB yName(host)); byte[] macs = ni.getHardwareAddress(); for(int i=0; i