如何在linux中用java得到网卡物理地址?
网上有一种方法是调用系统命令,应该调用哪一条命令呢?
哪位大哥有例子啊?
谢谢先~~~~

解决方案 »

  1.   

    如果想用纯java实现的话,可以考虑使用jpcap.这个项目文件里边有demo的.
      

  2.   

    jpcap?谢谢了
    不过还有其他方法吗?
    网上都是windows的
      

  3.   

    昨天我用JDK1.5在windows下写的一个程序用的是ProcessBuilder类 同样类似于Runtime.exec()
    你看看是否可以参考:
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;public class ProcessBuilderDemo {
    public static void main(String[] args) {
    //Physical Address. . . . . . . . . :
    InputStream input = null;
    try{
    //执行命令
    ProcessBuilder builder = new ProcessBuilder("ipconfig" ,"/all");
    Process process = builder.start();
    input = process.getInputStream();

    //把得到的流得到
    byte[] b = new byte[1024];
    StringBuffer buffer = new StringBuffer();
    while (input.read(b) > 0) {
    buffer.append(new String(b));
    }

    //分析流
    String value = buffer.substring(0);
    String systemFlag = "Physical Address. . . . . . . . . :";
    int index = value.indexOf(systemFlag);
    List<String> address = new ArrayList<String>();
    if (0 < index) {
    value = buffer.substring(index + systemFlag.length());
    address.add(value.substring(0, 18));
    } //打印输出
    for (String add : address) {
    System.out.println(add);
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    } finally {
    try {
    input.close();
    }
    catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    }
    }
      

  4.   

    谢谢crazycy(崔毅)~~!!!
    我改改到linux下试试看,呵呵
      

  5.   

    晕,JDK1.4里没有ProcessBuilder类~~
    不过可以运行~~搞不懂~~