JAVA在linux和windows下获得网卡号

 
  1. import java.net.InetAddress;
  2. import java.io.InputStream;
  3. import java.io.BufferedInputStream;
  4. import java.io.IOException;
  5. import java.text.ParseException;
  6. import java.util.StringTokenizer;
  7. public final class NetworkInfo {
  8.  private final static String getMacAddress() throws IOException {
  9.   String os = System.getProperty("os.name");
  10.   try {
  11.    if(os.startsWith("Windows")) {
  12.     return windowsParseMacAddress(windowsRunIpConfigCommand());
  13.    } else if(os.startsWith("Linux")) {
  14.     return linuxParseMacAddress(linuxRunIfConfigCommand());
  15.    } else {
  16.     throw new IOException("unknown operating system: " + os);
  17.    }
  18.   } catch(ParseException ex) {
  19.    ex.printStackTrace();
  20.    throw new IOException(ex.getMessage());
  21.   }
  22.  }
  23.  /*
  24.   * Linux stuff
  25.   */
  26.  private final static String linuxParseMacAddress(String ipConfigResponse) throws ParseException {
  27.   String localHost = null;
  28.   try {
  29.    localHost = InetAddress.getLocalHost().getHostAddress();
  30.   } catch(java.net.UnknownHostException ex) {
  31.    ex.printStackTrace();
  32.    throw new ParseException(ex.getMessage(), 0);
  33.   }
  34.   StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "/n");
  35.   String lastMacAddress = null;
  36.   while(tokenizer.hasMoreTokens()) {
  37.    String line = tokenizer.nextToken().trim();
  38.    boolean containsLocalHost = line.indexOf(localHost) >= 0;
  39.    // see if line contains IP address
  40.    if(containsLocalHost && lastMacAddress != null) {
  41.     return lastMacAddress;
  42.    }
  43.    // see if line contains MAC address
  44.    int macAddressPosition = line.indexOf("HWaddr");
  45.    if(macAddressPosition <= 0continue;
  46.    String macAddressCandidate = line.substring(macAddressPosition + 6).trim();
  47.    if(linuxIsMacAddress(macAddressCandidate)) {
  48.     lastMacAddress = macAddressCandidate;
  49.     continue;
  50.    }
  51.   }
  52.   ParseException ex = new ParseException
  53.    ("cannot read MAC address for " + localHost + " from [" + ipConfigResponse + "]"0);
  54.   ex.printStackTrace();
  55.   throw ex;
  56.  }
  57.  private final static boolean linuxIsMacAddress(String macAddressCandidate) {
  58.   // TODO: use a smart regular expression
  59.   if(macAddressCandidate.length() != 17return false;
  60.   return true;
  61.  }
  62.  private final static String linuxRunIfConfigCommand() throws IOException {
  63.   Process p = Runtime.getRuntime().exec("ifconfig");
  64.   InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
  65.   StringBuffer buffer= new StringBuffer();
  66.   for (;;) {
  67.    int c = stdoutStream.read();
  68.    if (c == -1break;
  69.    buffer.append((char)c);
  70.   }
  71.   String outputText = buffer.toString();
  72.   stdoutStream.close();
  73.   return outputText;
  74.  }
  75.  /*
  76.   * Windows stuff
  77.   */
  78.  private final static String windowsParseMacAddress(String ipConfigResponse) throws ParseException {
  79.   String localHost = null;
  80.   try {
  81.    localHost = InetAddress.getLocalHost().getHostAddress();
  82.   } catch(java.net.UnknownHostException ex) {
  83.    ex.printStackTrace();
  84.    throw new ParseException(ex.getMessage(), 0);
  85.   }
  86.   StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "/n");
  87.   String lastMacAddress = null;
  88.   while(tokenizer.hasMoreTokens()) {
  89.    String line = tokenizer.nextToken().trim();
  90.    // see if line contains IP address
  91.    if(line.endsWith(localHost) && lastMacAddress != null) {
  92.     return lastMacAddress;
  93.    }
  94.    // see if line contains MAC address
  95.    int macAddressPosition = line.indexOf(":");
  96.    if(macAddressPosition <= 0continue;
  97.    String macAddressCandidate = line.substring(macAddressPosition + 1).trim();
  98.    if(windowsIsMacAddress(macAddressCandidate)) {
  99.     lastMacAddress = macAddressCandidate;
  100.     continue;
  101.    }
  102.   }
  103.   ParseException ex = new ParseException("cannot read MAC address from [" + ipConfigResponse + "]"0);
  104.   ex.printStackTrace();
  105.   throw ex;
  106.  }
  107.  private final static boolean windowsIsMacAddress(String macAddressCandidate) {
  108.   // TODO: use a smart regular expression
  109.   if(macAddressCandidate.length() != 17return false;
  110.   return true;
  111.  }
  112.  private final static String windowsRunIpConfigCommand() throws IOException {
  113.   Process p = Runtime.getRuntime().exec("ipconfig /all");
  114.   InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
  115.   StringBuffer buffer= new StringBuffer();
  116.   for (;;) {
  117.    int c = stdoutStream.read();
  118.    if (c == -1break;
  119.    buffer.append((char)c);
  120.   }
  121.   String outputText = buffer.toString();
  122.   stdoutStream.close();
  123.   return outputText;
  124.  }
  125.  /*
  126.   * Main
  127.   */
  128.  public final static void main(String[] args) {
  129.   try {
  130.    System.out.println("Network infos");
  131.    System.out.println("  Operating System: " + System.getProperty("os.name"));
  132.    System.out.println("  IP/Localhost: " + InetAddress.getLocalHost().getHostAddress());
  133.    System.out.println("  MAC Address: " + getMacAddress());
  134.   } catch(Throwable t) {
  135.    t.printStackTrace();
  136.   }
  137.  }
  138. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值