I want get mac address WiFi computer by pattern recognition in java, but this putten give me all mac address(WIFI,Ethernet adapter....) Can help me get just mac address wife.

     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();
            }   
        }
有帮助吗?

解决方案

The simplest thing to do is to scan ipconfig result In which line containing mac id is preceded by ->"Physical Address ...............:" before mac id " 00-00-00-00-00-00" . Find Index of ":" in that line add one to that and subtring of that index to length of file gives you the Mac Output of ipconfig/all

Batch

@echo
off 
ipconfig /all 
pause
exit

java code

 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();
        }
    }

}

Output

Mac

其他提示

Hej, i do this on a Ubuntu System with this method:

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;
    }

UPDATE: This is a version with Pattern and Matcher usage:

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;
    }

I call it with

System.out.println(SystemUtil.getMac("wlan"));

Maybe it helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top