window下的我现在已经得到,可是linux下的尚未得到,请高手指点!

解决方案 »

  1.   


    ip地址用:
                    InetAddress   ipaddr   =   InetAddress.getLocalHost(); 
                    System.out.println( "Localhost       ip: "+ipaddr.getHostAddress()); 
    网卡信息用
                       ProcessBuilder pb = new ProcessBuilder("ifconfig"); 
    然后处理下
      

  2.   

    上面写的是window下可以用,linux下可以吗
      

  3.   

    以下代码需要 JDK 6 及以后版本,如果是 JDK 6 以下版本的话,没有直接获得 MAC 的 API。import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.util.Enumeration;public class NetworkInfo {    private final static char[] HEX = "0123456789ABCDEF".toCharArray();    public static void main(String[] args) throws SocketException {
            for(Enumeration<NetworkInterface> i = NetworkInterface.getNetworkInterfaces(); i.hasMoreElements(); ) {
                NetworkInterface ni = i.nextElement();
                System.out.println("NETWORK CARD NAME: " + ni.getDisplayName());
                System.out.println("MAC: " + toMacString(ni.getHardwareAddress()));
                for(Enumeration<InetAddress> j = ni.getInetAddresses(); j.hasMoreElements(); ) {
                    System.out.println("   " + j.nextElement());
                }
            }
        }    private static String toMacString(byte[] bys) {
            if(bys == null) {
                return null;
            }
            char[] chs = new char[bys.length * 3 - 1];
            for(int i = 0, k = 0; i < bys.length; i++) {
                if(i > 0) {
                    chs[k++] = '-';
                }
                chs[k++] = HEX[(bys[i] >> 4) & 0xf];
                chs[k++] = HEX[bys[i] & 0xf];
            }
            return new String(chs);
        }
    }
      

  4.   

    package com.xuz.csdn.june19;import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.net.UnknownHostException;public class NetworkInterfaceTest { public static void main(String[] args) { try {
    InetAddress address = InetAddress.getLocalHost();
    NetworkInterface netCard = NetworkInterface
    .getByInetAddress(address);
    byte[] addr = netCard.getHardwareAddress(); StringBuffer sb = new StringBuffer();
    for (int i = 0; i < addr.length; i++) {
    if (addr[i] != 0) {
    sb.append("-");
    } String string = Integer.toHexString(addr[i] & 0xff);
    sb.append(string.length() == 1 ? "0" + string : string);
    } System.out.println(sb.toString().toUpperCase());
    } catch (SocketException e) {
    e.printStackTrace();
    } catch (UnknownHostException e) {
    e.printStackTrace();
    }// Enumeration<NetworkInterface> interfaces = null;
    // try {
    // interfaces = NetworkInterface.getNetworkInterfaces();
    // } catch (SocketException e) {
    // e.printStackTrace();
    // }
    // while (interfaces.hasMoreElements()) {
    // final NetworkInterface ni = interfaces.nextElement();
    // try {
    // if (ni.isLoopback() || ni.isPointToPoint() || ni.isVirtual())
    // continue;
    // } catch (SocketException e) {
    // e.printStackTrace();
    // }
    // byte[] macAddress = null;
    // try {
    // macAddress = ni.getHardwareAddress();
    // } catch (SocketException e) {
    // e.printStackTrace();
    // }
    // if (macAddress != null && macAddress.length > 0)
    // try {
    // System.out.println(new String(macAddress,"gbk"));
    // } catch (UnsupportedEncodingException e) {
    // e.printStackTrace();
    // }
    // } }}
    昨天学习的。
      

  5.   

    在iunx下还可以取得其他的信息吗,例如硬盘的物理序列号,cup的序列号
      

  6.   

    火龙果:
     你昨天给我发过来的代码,运行结果是这样的
    NETWORK CARD NAME: eth0
    MAC: 00-E0-4C-0D-BA-5E
       /fe80:0:0:0:2e0:4cff:fe0d:ba5e%2
       /192.168.8.105
    NETWORK CARD NAME: lo
    MAC: null
       /0:0:0:0:0:0:0:1%1
       /127.0.0.1
    就得不到别的信息了吗?
      

  7.   

    双网卡的话 那样获取不到吧.
    应该是调用linux系统 从返回值中获取你要的信息
      

  8.   

    /**
     * 取当前系统站点本地地址 linux下 和 window下可用 add by RWW
     * 
     * @return
     */
    public static String getLocalIP()
    {
    String sIP = "";
    InetAddress ip = null;
    try
    {
    // 如果是Windows操作系统
    if (isWindowsOS())
    {
    ip = InetAddress.getLocalHost();
    }
    // 如果是Linux操作系统
    else
    {
    boolean bFindIP = false;
    Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
    .getNetworkInterfaces();
    while (netInterfaces.hasMoreElements())
    {
    if (bFindIP)
    {
    break;
    }
    NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
    // ----------特定情况,可以考虑用ni.getName判断
    // 遍历所有ip
    Enumeration<InetAddress> ips = ni.getInetAddresses();
    while (ips.hasMoreElements())
    {
    ip = (InetAddress) ips.nextElement();
    if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() // 127.开头的都是lookback地址
    && ip.getHostAddress().indexOf(":") == -1)
    {
    bFindIP = true;
    break;
    }
    }
    }
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    MyLogger.logserverLogger.error("MultiThreadedTrapReceiver 453: " + e.getMessage());
    }
    if (null != ip)
    {
    sIP = ip.getHostAddress();
    }
    return sIP;
    }
      

  9.   

    不行就Runtime.getRuntime.exex嘛。
      

  10.   

    /**
     * 判断当前系统是否windows 
     * 
     * @return
     */
    public static boolean isWindowsOS()
    {
    boolean isWindowsOS = false;
    String osName = System.getProperty("os.name");
    if (osName.toLowerCase().indexOf("windows") > -1)
    {
    isWindowsOS = true;
    }
    return isWindowsOS;
    }