2011-09-09 39 views
1

我正在使用ipconfig/all命令和java中的正則表達式來查找MAC地址。正則表達式在不同語言環境下工作

我在ipconfig/all命令的輸出中搜索Physical Address

但問題是我想正則表達式在不同的區域設置上工作,即它可以找到任何區域設置的物理地址。

在此先感謝。

+1

你可以嘗試檢查MAC地址本身 - IIRC的Windows顯示它在格式'01-23-45-67-89-ab' – Piskvor

回答

1

,而不是尋找 「物理地址」,或任何其他本地化版本(每次需要支持新語言時都需要添加本地化版本),您可以只編寫一個正則表達式來查找MAC地址本身。

既然我們知道,一個典型的MAC地址是由每兩個十六進制數字,用冒號,句號,或破折號分開6個分組,下面的正則表達式將做到這一點:

([a-fA-F0-9]{2}[:\-\.]){5}[a-fA-F0-9]{2} 

說明: (兩個十六進制數字後跟一個冒號:,重複5次)(最後兩個十六進制數字)

+1

小修正:Windows部分使用自己的約定,所以你需要用短劃線'-'來代替冒號(唉,不是無處不在 - 有時使用'-',有時': ',有時什麼都沒有;這個特殊用途有短劃線) – Piskvor

+0

啊,好點。我更新了正則表達式以允許冒號,點或破折號。根據需要添加儘可能多的特殊字符。在某些情況下,我想沒有分隔符。如果你正在處理這種情況,你也可以通過正則表達式中的'?'使分隔符可選。 – jefflunt

1

選項1:

您可以使用正則表達式這樣的(英語,法語,西班牙語):

/(Physical Address|Adresse Physique|Direccion fisica)/ 

後來檢查哪些區域,那麼你正在使用,因此更新您的正則表達式。

選項2:

使用的Java(JDK 1.6)直接獲取MAC地址

import java.net.InetAddress; 
import java.net.NetworkInterface; 
import java.net.SocketException; 
import java.net.UnknownHostException; 

public class MacAddress { 

public static void main(String[] args) { 
    try { 
     //InetAddress address = InetAddress.getLocalHost(); 
     InetAddress address = InetAddress.getByName("192.168.0.158"); 

     /* 
     * Get NetworkInterface for the current host and then read the 
     * hardware address. 
     */ 
     NetworkInterface ni = NetworkInterface.getByInetAddress(address); 
     if (ni != null) { 
      byte[] mac = ni.getHardwareAddress(); 
      if (mac != null) { 
       /* 
       * Extract each array of mac address and convert it to hexa with the 
       * following format 08-00-27-DC-4A-9E. 
       */ 
       for (int i = 0; i < mac.length; i++) { 
        System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""); 
       } 
      } else { 
       System.out.println("Address doesn't exist or is not accessible."); 
      } 
     } else { 
      System.out.println("Network Interface for the specified address is not found."); 
     } 
    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
    } catch (SocketException e) { 
     e.printStackTrace(); 
    } 
} 

}

+0

1)本會打破不在列表中的語言(XP至少支持30種語言版本),2),因爲這是'ipconfig',而不是'iptables' - Windows的源代碼不會隨時可用,這意味着您需要獲得所有以各種語言進行安裝並獲得描述。 3)如果描述在Windows的新版本中發生變化,那麼您會遇到困難。 – Piskvor

+0

我已經提供了一個Java代碼替代 – Stephan

1

這裏就是我得到的,當我運行ipconfig /all

Adaptér sítě Ethernet Připojení k místní síti: 
     Přípona DNS podle připojení . . . : example.com 
     Popis . . . . . . . . . . . . . . : AMD PCNET Family PCI Ethernet Adapter 
     Fyzická Adresa. . . . . . . . . . : DE-AD-BE-EF-CA-FE 
     Protokol DHCP povolen . . . . . . : Ano 
     Automatická konfigurace povolena : Ano 
     Adresa IP . . . . . . . . . . . . : 192.168.0.158 
     Maska podsítě . . . . . . . . . . : 255.255.255.0 
     Výchozí brána . . . . . . . . . . : 192.168.0.1 
     Server DHCP . . . . . . . . . . . : 192.168.0.1 
     Servery DNS . . . . . . . . . . . : 192.168.0.1 
     Primární server WINS. . . . . . . : 192.168.0.1 
     Zapůjčeno . . . . . . . . . . . . : 9. září 2011 16:05:32 
     Zápůjčka vyprší . . . . . . . . . : 9. září 2011 20:05:32 

正如你所看到的,查找字符串「Physical Address」是沒有用的,因爲沒有一個。但是,請注意Windows有自己的MAC地址格式 - 用連字符(和按字母順序的部分大寫)分隔每兩個十六進制數字。所以,尋找正則表達式:

([0-9A-F]{2}-){5}[0-9A-F]{2} 

會給你你正在尋找的MAC地址。警告:許多計算機都有多個網絡接口(有線和WiFi,各種VPN等),因此輸出中可能會有多個MAC。

+0

saakra toto je anglicke論壇:-) - 這是英語論壇,btw +1 – mKorbel

+0

@mKorbel:我很清楚這一點。正如你可能從第2段注意到的那樣,我使用本地化文本來顯示輸出中可能不存在字符串「Physical address」,並且[列出所有可能的l10ns](http:// stackoverflow。 com/questions/7362763/regular-expression-to-work-on-different-locales/7362823#7362823)有點無用 - 這就是爲什麼我建議直接尋找MAC地址的原因。 – Piskvor

1

通知FO特別是對於HP /康柏MAC地址某些計算機應入店從

Process pcs = Runtime.getRuntime().exec("wmic bios"); 

如果BIOS定製(Largiest公司),那麼該數據應該是通過使用JNI/JNA(大量VB的入店/圍繞C#腳本)

例如:

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.InetAddress; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.regex.Pattern; 

public class MAC_TEST { 

    private static Pattern macPattern = Pattern.compile("[0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:]" 
      + "[0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}"); 

    private static List getWindowsMACAddresses() { 
     try { 
      //Process conf = Runtime.getRuntime().exec("wmic bios");//for HP computers 
      Process conf = Runtime.getRuntime().exec("ipconfig /all"); 
      //Process p = Runtime.getRuntime().exec("wmic bios /all"); 
      BufferedReader input = new BufferedReader(
        new InputStreamReader(conf.getInputStream())); 
      return getMACAddresses(input); 
     } catch (Exception e) { 
      System.err.println("Error Reading Windows MAC Address."); 
     } 
     return new ArrayList(1); 
    } 

    private static List getLinuxMACAddresses() { 
     try { 
      Process conf = Runtime.getRuntime().exec("/sbin/ifconfig"); 
      BufferedReader input = new BufferedReader(
        new InputStreamReader(conf.getInputStream())); 
      return getMACAddresses(input); 
     } catch (Exception e) { 
      System.err.println("Error Reading Linux MAC Address."); 
     } 
     return new ArrayList(1); 
    } 

    private static List getHPUXMACAddresses() { 
     try { 
      Process conf = Runtime.getRuntime().exec("/etc/lanscan"); 
      BufferedReader input = new BufferedReader(
        new InputStreamReader(conf.getInputStream())); 
      return getMACAddresses(input); 
     } catch (Exception e) { 
      System.err.println("Error Reading HPUX MAC Address."); 
     } 
     return new ArrayList(1); 
    } 

    private static List getSolarisMACAddresses() { 
     try { 
      List rtc = new ArrayList(1); 
      Process conf = Runtime.getRuntime().exec("/usr/sbin/arp " 
        + InetAddress.getLocalHost().getHostAddress()); 
      BufferedReader input = new BufferedReader(
        new InputStreamReader(conf.getInputStream())); 
      rtc.addAll(getMACAddresses(input)); 
      input.close(); 
      input = null; 
      conf = null; 
      //Solaris reports MAC address without first 0, change the pattern at re-test 
      macPattern = Pattern.compile("[0-9a-fA-F][-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}" 
        + "[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}"); 
      conf = Runtime.getRuntime().exec("/usr/sbin/arp " 
        + InetAddress.getLocalHost().getHostAddress()); 
      input = new BufferedReader(new InputStreamReader(
        conf.getInputStream())); 
      rtc.addAll(getMACAddresses(input)); 
      //Revert pattern 
      macPattern = Pattern.compile("[0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}" 
        + "[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}"); 
      return rtc; 
     } catch (Exception e) { 
      System.err.println("Error Reading Solaris MAC Address."); 
     } 
     return new ArrayList(1); 
    } 

    private static List getMACAddresses(BufferedReader input) throws Exception { 
     List MACs = new ArrayList(1); 
     String theLine; 
     while ((theLine = input.readLine()) != null) { 
      String[] ss = macPattern.split(theLine); 
      for (int p = 0; p < ss.length; p++) { 
       String s = theLine.substring(theLine.indexOf(ss[p]) + ss[p].length()).trim(); 
       if (!s.isEmpty()) { 
        String s1 = s.replaceAll("-", ":"); 
        String s2 = s1.substring(0, s1.lastIndexOf(':') + 3); 
        if (s2.length() == 16 || s2.length() == 17) { 
         MACs.add(s2); 
        } 
       } 
      } 
     } 
     return MACs; 
    } 

    public static void main(String[] args) { 
     try { 
      System.out.println("WINDOWS ... Found the following MAC Addresses: "); 
      List MACS = getWindowsMACAddresses(); 
      System.out.println("*-----------------*"); 
      for (int i = 0; i < MACS.size(); i++) { 
       System.out.println("|" + MACS.get(i) + "|"); 
      } 
      System.out.println("*-----------------*"); 
      System.out.println(" "); 
      System.out.println("Linux ... Found the following MAC Addresses: "); 
      MACS = getLinuxMACAddresses(); 
      System.out.println("*-----------------*"); 
      for (int i = 0; i < MACS.size(); i++) { 
       System.out.println("|" + MACS.get(i) + "|"); 
      } 
      System.out.println("*-----------------*"); 
      System.out.println(" "); 
      System.out.println("Solaris ... Found the following MAC Addresses: "); 
      MACS = getSolarisMACAddresses(); 
      System.out.println("*-----------------*"); 
      for (int i = 0; i < MACS.size(); i++) { 
       System.out.println("|" + MACS.get(i) + "|"); 
      } 
      System.out.println("*-----------------*"); 
      System.out.println(" "); 
      System.out.println("HPUX ... Found the following MAC Addresses: "); 
      MACS = getHPUXMACAddresses(); 
      System.out.println("*-----------------*"); 
      for (int i = 0; i < MACS.size(); i++) { 
       System.out.println("|" + MACS.get(i) + "|"); 
      } 
      System.out.println("*-----------------*"); 
      System.out.println(" "); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    private MAC_TEST() { 
    } 
}