诸位Linux下如何获得本机的mac地址!给个实例小程序供参考?java语言可以吗?还有怎么通过java程序去修改本机mac地址!!急!
望知道的大侠给予回复!谢谢......

解决方案 »

  1.   

    package com.lxh.test;import java.io.*;
    /*
     * lxh
     */public class Test {
     public static void main(String[] args) throws Exception  {
      String line;
      String physicalAddress = "read MAC error!"; 
      try {
       Process p=Runtime.getRuntime().exec("cmd.exe /c ipconfig /all");
       //p.waitFor();
       BufferedReader bd =new BufferedReader(new InputStreamReader (p.getInputStream())); 
       while((line=bd.readLine())!=null){
        if(line.indexOf("Physical Address. . . . . . . . . :")!=-1){
         if(line.indexOf(":")!=-1){
          physicalAddress = line.substring(line.indexOf(":")+2); 
          break; //找到MAC,推出循环 
         }
        }
       }
       p.waitFor();
       System.out.println("信息:"+line+" ");
       System.out.println("本机的MAC地址是: "+ physicalAddress); 
      } catch (IOException e) {
       e.printStackTrace();
      }
     }}
    MAC地址是全球唯一的 不能被修改的
      

  2.   


    解析ifconfig命令。
    import java.io.*;
    import java.util.regex.*;
    public class GetMac{
      public static void main(String arsg[])throws Exception{
             String mac="";
             String cmd="ifconfig";
             Process p=Runtime.getRuntime().exec(cmd);
     java.io.InputStream in=p.getInputStream();
     java.io.BufferedReader br=new java.io.BufferedReader(new java.io.InputStreamReader(in));
     String line=null;
     String ip=null;
     while((line=br.readLine())!=null){
        if(line.indexOf("HWaddr")!=-1){
                       Matcher m=Pattern.compile("([0-9A-F]{2,2}:){5,5}[0-9A-F]{2,2}").matcher(line);
                       if(m.find())mac=m.group();
       break;
    }  
     }
     in.close();
     br.close(); 
     System.out.println("mac="+mac);
      }
    }