用纯Java代码根据IP获取windows和linux的MAC物理地址

一、环境介绍

1.最近在使用struts2和JSP开发web网页时,碰到一个问题,我需要在后台根据ip获取访问服务器的物理地址,然后进行其他业务处理,但是我发现我在后台没有对应的接口或函数能够满足我。当时有想过使用active控件方式获取,但是这样局限性太大,又要麻烦客户安装控件,于是就寻找纯Java代码获取客户端的MAC物理地址,在网上找了很久,终于找到一个代码,现在分享出来给大家,希望有用得到的朋友拿去使用做参考。


  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;


  5. /**

  6. * 根据ip获取mac地址

  7. */
  8. public class GetMacAddress {
  9.      public static String callCmd(String[] cmd) {  
  10.          String result = "";  
  11.          String line = "";  
  12.          try {  
  13.              Process proc = Runtime.getRuntime().exec(cmd);  
  14.              InputStreamReader is = new InputStreamReader(proc.getInputStream());  
  15.              BufferedReader br = new BufferedReader (is);  
  16.              while ((line = br.readLine ()) != null) {  
  17.              result += line;  
  18.              }  
  19.          }  
  20.          catch(Exception e) {  
  21.              e.printStackTrace();  
  22.          }  
  23.          return result;  
  24.      }
  25.      
  26.      
  27.      /**
  28.       *
  29.       * @param cmd  第一个命令
  30.       * @param another 第二个命令
  31.       * @return   第二个命令的执行结果
  32.       */  
  33.      public static String callCmd(String[] cmd,String[] another) {  
  34.          String result = "";  
  35.          String line = "";  
  36.          try {  
  37.              Runtime rt = Runtime.getRuntime();  
  38.              Process proc = rt.exec(cmd);  
  39.              proc.waitFor();  //已经执行完第一个命令,准备执行第二个命令  
  40.              proc = rt.exec(another);  
  41.              InputStreamReader is = new InputStreamReader(proc.getInputStream());  
  42.              BufferedReader br = new BufferedReader (is);  
  43.              while ((line = br.readLine ()) != null) {  
  44.                  result += line;  
  45.              }  
  46.          }  
  47.          catch(Exception e) {  
  48.              e.printStackTrace();  
  49.          }  
  50.          return result;  
  51.      }
  52.      
  53.      
  54.      
  55.      /**
  56.       *
  57.       * @param ip  目标ip,一般在局域网内
  58.       * @param sourceString 命令处理的结果字符串
  59.       * @param macSeparator mac分隔符号
  60.       * @return  mac地址,用上面的分隔符号表示
  61.       */  
  62.      public static String filterMacAddress(final String ip, final String sourceString,final String macSeparator) {  
  63.          String result = "";  
  64.          String regExp = "((([0-9,A-F,a-f]{1,2}" + macSeparator + "){1,5})[0-9,A-F,a-f]{1,2})";  
  65.          Pattern pattern = Pattern.compile(regExp);  
  66.          Matcher matcher = pattern.matcher(sourceString);  
  67.          while(matcher.find()){  
  68.              result = matcher.group(1);  
  69.              if(sourceString.indexOf(ip) <= sourceString.lastIndexOf(matcher.group(1))) {  
  70.                  break;  //如果有多个IP,只匹配本IP对应的Mac.  
  71.              }  
  72.          }
  73.    
  74.          return result;  
  75.      }
  76.      
  77.      
  78.      
  79.      /**
  80.       *
  81.       * @param ip 目标ip
  82.       * @return   Mac Address
  83.       *
  84.       */  
  85.      public static String getMacInWindows(final String ip){  
  86.          String result = "";  
  87.          String[] cmd = {  
  88.                  "cmd",  
  89.                  "/c",  
  90.                  "ping " +  ip  
  91.                  };  
  92.          String[] another = {  
  93.                  "cmd",  
  94.                  "/c",  
  95.                  "arp -a"  
  96.                  };  
  97.    
  98.          String cmdResult = callCmd(cmd,another);  
  99.          result = filterMacAddress(ip,cmdResult,"-");  
  100.    
  101.          return result;  
  102.      }  
  103.    
  104.      /**
  105.      *
  106.      * @param ip 目标ip
  107.      * @return   Mac Address
  108.      *
  109.      */  
  110.      public static String getMacInLinux(final String ip){  
  111.          String result = "";  
  112.          String[] cmd = {  
  113.                  "/bin/sh",  
  114.                  "-c",  
  115.                  "ping " +  ip + " -c 2 && arp -a"  
  116.                  };  
  117.          String cmdResult = callCmd(cmd);  
  118.          result = filterMacAddress(ip,cmdResult,":");  
  119.    
  120.          return result;  
  121.      }  
  122.      
  123.      /**
  124.       * 获取MAC地址
  125.       * @return 返回MAC地址
  126.       */
  127.      public static String getMacAddress(String ip){
  128.          String macAddress = "";
  129.          macAddress = getMacInWindows(ip).trim();
  130.          if(macAddress==null||"".equals(macAddress)){
  131.              macAddress = getMacInLinux(ip).trim();
  132.          }
  133.          return macAddress;
  134.      }
  135.          
  136.      /**
  137.      * 测试
  138.      */  
  139.      public static void main(String[] args) {          
  140.          System.out.println(getMacAddress("192.168.2.184"));
  141.      }
  142.    
  143. }
复制代码


2.下面我说说代码整体思路,代码主要通过windows系统或Linux系统的命令来根据ip获取MAC物理地址,首先使用ping命令,如果能正常ping通,系统会缓存访问过的ip和MAC地址,然后通过arp命令去获取所有访问过的缓存ip和MAC地址信息,然后通过java字符串处理代码进行处理即可获取想要的IP地址对应的MAC地址。下面是具体代码(如果服务端是Linux系统建议在使用ARP命令时最好一条命令能过滤出IP对应的MAC地址,下次我将补上这一条命令):

二、源码下载

1.下载地址:  GetMacAddress.java (4.19 KB, 下载次数: 6) 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值