现在又怎么一个需求  
   用java去读取liunx的网口 
   一般读取出来都是etc0 , ect1....
可是服务器的网口是多个 绑定成了一个逻辑网口(bond)
小弟 我对liunx不是很了解 看api也没发现怎么去读取bond这样的网口 有知道的大神 麻烦告诉咱下 小弟在这里谢谢了啊..

解决方案 »

  1.   

    可以用 NetwrokIntenface 类
      

  2.   

    Enumeration<NetworkInterface> enu=NetworkInterface.getNetWorkInterface()loop.....
      

  3.   

    可以用 NetwrokIntenface 类,例如获得mac:package com.yc.test;import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Formatter;
    import java.util.List;
    import java.util.zip.CRC32;public class GetMACAddress {  public static void main(String[] args) {
        System.out.println(calculateMacIdentifier());
      }  private final static String calculateMacIdentifier() {
        List<NetworkInterface> interfaces = new ArrayList<NetworkInterface>();
        try {
          interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());      Collections.sort(interfaces, new Comparator<NetworkInterface>() {
            @Override
            public int compare(NetworkInterface o1, NetworkInterface o2) {
              try {
                if (o1.isLoopback() && !o2.isLoopback()) {
                  return 1;
                }
                if (!o1.isLoopback() && o2.isLoopback()) {
                  return -1;
                }
              } catch (SocketException e) {
                return 0;
              }
              return o1.getName().compareTo(o2.getName());
            }
          });      for (NetworkInterface iface : interfaces) {
            if (iface.getHardwareAddress() != null) {
              // get the first not null hw address and CRC it
              System.out.println("MAC:" + bytesToHexString(iface.getHardwareAddress()));
              CRC32 crc = new CRC32();
              crc.update(iface.getHardwareAddress());
              return Long.toHexString(crc.getValue());
            }
          }      if (interfaces.isEmpty()) {
          }
          return "";
        } catch (SocketException e) {
          return "";
        }
      }  public static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 2);    Formatter formatter = new Formatter(sb);
        for (byte b : bytes) {
          formatter.format("%02x", b);
        }    return sb.toString();
      }
    }
      

  4.   

    看不懂楼主说的bond逻辑网口指什么?
      

  5.   

    我简单理解是 多个物理网口绑定成为一个逻辑网口  别人通过这个逻辑网口进行访问 然后liunx在对这些请求进行分发  个人理解 不知道对不对 我的代码就是和楼上的一样的Enumeration<NetworkInterface> enu=NetworkInterface.getNetWorkInterface() 然后再遍历出来的 可是他遍历出来的不是etc这样的名字了 ...
       有没有懂网络的 麻烦告诉小白我一下呢 
      

  6.   

    在linxu下还真不知道有这样的逻辑接口。
      

  7.   

    起码我是和eth0、eth1一样,直接用名称读取bond0即可。
    各网口的数据是通过shell命令获取的。
      

  8.   

    恩 eth0 eth1 读出来是这样的 但是现在服务器把这几个网口绑定成了逻辑网口(也就是bond) 在java代码里要怎么获取这个bond网口呢?
      

  9.   

    在Ubuntu下是一样的
    下面是bash#!/bin/bash
    #netdev=eth0
    #netdev=eth1
    netdev=bond
    grep $netdev: /proc/net/dev你可以先看看/proc/net/dev有没你要的网口(Ubuntu下物理网口、逻辑网口的信息都在这里)