1. 程式人生 > >用純Java程式碼根據IP獲取windows和linux的MAC實體地址

用純Java程式碼根據IP獲取windows和linux的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.       * @param cmd  第一個命令
  28.       * @param another 第二個命令
  29.       * @return   第二個命令的執行結果
  30.       */  
  31.      public static String callCmd(String[] cmd,String[] another) {  
  32.          String result = "";  
  33.          String line = "";  
  34.          try {  
  35.              Runtime rt = Runtime.getRuntime();  
  36.              Process proc = rt.exec(cmd);  
  37.              proc.waitFor();  //已經執行完第一個命令,準備執行第二個命令  
  38.              proc = rt.exec(another);  
  39.              InputStreamReader is = new InputStreamReader(proc.getInputStream());  
  40.              BufferedReader br = new BufferedReader (is);  
  41.              while ((line = br.readLine ()) != null) {  
  42.                  result += line;  
  43.              }  
  44.          }  
  45.          catch(Exception e) {  
  46.              e.printStackTrace();  
  47.          }  
  48.          return result;  
  49.      }
  50.      /**
  51.       *
  52.       * @param ip  目標ip,一般在區域網內
  53.       * @param sourceString 命令處理的結果字串
  54.       * @param macSeparator mac分隔符號
  55.       * @return  mac地址,用上面的分隔符號表示
  56.       */  
  57.      public static String filterMacAddress(final String ip, final String sourceString,final String macSeparator) {  
  58.          String result = "";  
  59.          String regExp = "((([0-9,A-F,a-f]{1,2}" + macSeparator + "){1,5})[0-9,A-F,a-f]{1,2})";  
  60.          Pattern pattern = Pattern.compile(regExp);  
  61.          Matcher matcher = pattern.matcher(sourceString);  
  62.          while(matcher.find()){  
  63.              result = matcher.group(1);  
  64.              if(sourceString.indexOf(ip) <= sourceString.lastIndexOf(matcher.group(1))) {  
  65.                  break;  //如果有多個IP,只匹配本IP對應的Mac.  
  66.              }  
  67.          }
  68.          return result;  
  69.      }
  70.      /**
  71.       *
  72.       * @param ip 目標ip
  73.       * @return   Mac Address
  74.       *
  75.       */  
  76.      public static String getMacInWindows(final String ip){  
  77.          String result = "";  
  78.          String[] cmd = {  
  79.                  "cmd",  
  80.                  "/c",  
  81.                  "ping " +  ip  
  82.                  };  
  83.          String[] another = {  
  84.                  "cmd",  
  85.                  "/c",  
  86.                  "arp -a"  
  87.                  };  
  88.          String cmdResult = callCmd(cmd,another);  
  89.          result = filterMacAddress(ip,cmdResult,"-");  
  90.          return result;  
  91.      }  
  92.      /**
  93.      *
  94.      * @param ip 目標ip
  95.      * @return   Mac Address
  96.      *
  97.      */  
  98.      public static String getMacInLinux(final String ip){  
  99.          String result = "";  
  100.          String[] cmd = {  
  101.                  "/bin/sh",  
  102.                  "-c",  
  103.                  "ping " +  ip + " -c 2 && arp -a"  
  104.                  };  
  105.          String cmdResult = callCmd(cmd);  
  106.          result = filterMacAddress(ip,cmdResult,":");  
  107.          return result;  
  108.      }  
  109.      /**
  110.       * 獲取MAC地址
  111.       * @return 返回MAC地址
  112.       */
  113.      public static String getMacAddress(String ip){
  114.          String macAddress = "";
  115.          macAddress = getMacInWindows(ip).trim();
  116.          if(macAddress==null||"".equals(macAddress)){
  117.              macAddress = getMacInLinux(ip).trim();
  118.          }
  119.          return macAddress;
  120.      }
  121.      /**
  122.      * 測試
  123.      */  
  124.      public static void main(String[] args) {          
  125.          System.out.println(getMacAddress("192.168.2.184"));
  126.      }
  127. }