java中获取mac 地址

本文介绍在Java中通过IP地址获取远程MAC地址的方法,包括在不同操作系统(如Windows和Linux)上的实现方式,以及可能遇到的问题和解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值