如何通过模式获得物理地址?

时间:2014-04-24 12:16:33

标签: java mac-address

我希望通过java中的模式识别来获取mac地址WiFi计算机,但是这个putten给我所有的mac地址(WIFI,以太网适配器....)可以帮我获得mac地址的妻子。

     public static  GetMac() {
            byte[] macB = null;
            byte[] bytesEncoded = null;
            try {
                String command = "ipconfig /all";
                Process p = Runtime.getRuntime().exec(command);
                BufferedReader inn = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));               
                //I want get just  mac address 
                Pattern pattern = Pattern.compile(".*Physical Addres.*: (.*)");
                while (true) {
                    String line = inn.readLine();

                    if (line == null)
                        break;
                    Matcher mm = pattern.matcher(line);
                    if (mm.matches()) {
                        String macS = mm.group(1);
                        macB = macS.getBytes();
                        bytesEncoded = Base64.encodeBase64(macB);
                        break;
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
        }

2 个答案:

答案 0 :(得分:1)

最简单的方法是扫描ipconfig结果在包含mac id的行前面加上 - >“物理地址............... :“ before mac id ”00-00-00-00-00-00“。 在该行中找到 “:”的索引添加一个,并将该索引的子字符串添加到文件的长度为您提供Mac Output of ipconfig/all

批量

@echo
off 
ipconfig /all 
pause
exit

java代码

 package test1;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 public class ProcessCommanLine {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ProcessCommanLine batchExecuteService = new ProcessCommanLine();
        ProcessCommanLine.run();
    }

    static void run() {
        // TODO Auto-generated method stub
        try {
            String cmds[] = {"D:\\test1.bat"};
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(cmds);
            process.getOutputStream().close();
            InputStream inputStream = process.getInputStream();
            InputStreamReader inputstreamreader = new InputStreamReader(inputStream);
            BufferedReader bufferedrReader = new BufferedReader(inputstreamreader);
            String strLine = "";
            while ((strLine = bufferedrReader.readLine()) != null) {
               // System.out.println(strLine);
                String ph="Physical Address";
                String subtring=null;
                if(strLine.length()>ph.length())
                subtring=strLine.substring(3,ph.length()+3); 
               if(strLine.contains(ph))
               {    
                   int i=strLine.indexOf(":")+1;
                   System.out.println(strLine.substring(i, strLine.length()));

               }
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

}

输出

Mac

答案 1 :(得分:0)

Hej,我是用这种方法在Ubuntu系统上做的:

public static String getMac(final String pattern) {
        StringBuilder sb = new StringBuilder();
        String line = "";
        try {
            Process p = Runtime.getRuntime().exec("ifconfig");
            InputStream inputStream = p.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            while((line = br.readLine()) != null) {
                if(line.contains(pattern)) {
                    sb.append(line);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(SystemUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

        String[] split = sb.toString().split("Adress");
        String macAddress = split[1].substring(2, split[1].length()-1);
        return "Your MAC of " + pattern + " is: " + macAddress;
    }

更新: 这是使用Pattern和Matcher的版本:

public static String getMac(final String pattern) {
        StringBuilder sb = new StringBuilder();
        String line = "";
        try {
            Process p = Runtime.getRuntime().exec("ifconfig");
            Pattern regEx = Pattern.compile(pattern);

            InputStream inputStream = p.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            while((line = br.readLine()) != null) {
                Matcher m = regEx.matcher(line);
                if(m.find()) {
                    sb.append(line);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(SystemUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

        String[] split = sb.toString().split("Adress");
        String macAddress = split[1].substring(2, split[1].length()-1);
        return "Your MAC of " + pattern + " is: " + macAddress;
    }

我用

打电话
System.out.println(SystemUtil.getMac("wlan"));

也许有帮助。