2011-02-17 15 views
0

我運行Ubuntu 10.10和我的主機配置如下:瞭解JDK 1.6的Ubuntu Linux操作系統的IPv6的輸出處理

[email protected]:~# cat /etc/hosts 
192.168.20.20 maxim-desktop # Added by NetworkManager 
192.168.10.20 maxim-lp 
127.0.0.1  localhost.localdomain localhost 
::1  maxim-desktop localhost6.localdomain6 localhost6 

# The following lines are desirable for IPv6 capable hosts 
::1  localhost ip6-localhost ip6-loopback 
fe00::0 ip6-localnet 
ff00::0 ip6-mcastprefix 
ff02::1 ip6-allnodes 
ff02::2 ip6-allrouters 
ff02::3 ip6-allhosts 
[email protected]:~# cat /etc/resolv.conf 
# Generated by NetworkManager 
nameserver 192.168.20.1 
[email protected]:~# cat /etc/hostname 
maxim-desktop 
[email protected]:~# hostname 
maxim-desktop 
[email protected]:~# hostname --fqdn 
[email protected]:~# ifconfig 
eth0  Link encap:Ethernet HWaddr 00:1c:c0:f2:ba:89 
      inet addr:192.168.20.20 Bcast:192.168.0.255 Mask:255.255.255.0 
      inet6 addr: fe80::21c:c0ff:fef2:ba89/64 Scope:Link 
      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 
      RX packets:9023854 errors:0 dropped:0 overruns:0 frame:0 
      TX packets:8532803 errors:0 dropped:0 overruns:0 carrier:0 
      collisions:0 txqueuelen:1000 
      RX bytes:4839992361 (4.8 GB) TX bytes:1067998152 (1.0 GB) 
      Interrupt:20 Memory:d3300000-d3320000 

lo  Link encap:Local Loopback 
      inet addr:127.0.0.1 Mask:255.0.0.0 
      inet6 addr: ::1/128 Scope:Host 
      UP LOOPBACK RUNNING MTU:16436 Metric:1 
      RX packets:12333251 errors:0 dropped:0 overruns:0 frame:0 
      TX packets:12333251 errors:0 dropped:0 overruns:0 carrier:0 
      collisions:0 txqueuelen:0 
      RX bytes:3216352432 (3.2 GB) TX bytes:3216352432 (3.2 GB) 

我試圖從Java代碼內達到相同的結果。

不幸的是,下面的代碼似乎並沒有削減它。

//Copyright (c) 2011, Maxim Veksler 
//All rights reserved. 
// 
//Redistribution and use in source and binary forms, with or without 
//modification, are permitted provided that the following conditions are met: 
// * Redistributions of source code must retain the above copyright 
//  notice, this list of conditions and the following disclaimer. 
// * Redistributions in binary form must reproduce the above copyright 
//  notice, this list of conditions and the following disclaimer in the 
//  documentation and/or other materials provided with the distribution. 
// * Neither the name of the <organization> nor the 
//  names of its contributors may be used to endorse or promote products 
//  derived from this software without specific prior written permission. 
// 
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
//DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY 
//DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
//ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

import java.net.Inet4Address; 
import java.net.Inet6Address; 
import java.net.InetAddress; 
import java.net.NetworkInterface; 
import java.net.SocketException; 
import java.util.Enumeration; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

/** 
* Hostname and IP address info, based on JDK6 NetworkInterface 
* 
* @author Maxim Veksler <[email protected]> 
*/ 
public class NetworkUtil { 
    private static Logger log = LoggerFactory.getLogger(NetworkUtil.class); 

    public static void main(String[] args) { 
     System.out.println("MAC: " + NetworkUtil.getMachineMac()); 
     System.out.println("HOSTNAME: " + NetworkUtil.getMachineHostname()); 
     System.out.println("IP: " + NetworkUtil.getMachineIPAddress()); 
     System.out.println("HOSTNAME ipv6: " + NetworkUtil.getMachineIPv6Hostname()); 
     System.out.println("IP ipv6: " + NetworkUtil.getMachineIPv6Address()); 
    } 

    /** 
    * Get the MAC address of the remote IP (if on local LAN). 
    * @param hostnameOrIP The target IP or Hostname (if you have DNS configured). 
    * 
    * @return MAC address if IP in local LAN, null if not. 
    */ 
    public static String getRemoteHostMac(String hostnameOrIP) { 
     try { 
      InetAddress address = InetAddress.getByName(hostnameOrIP); 
      NetworkInterface networkInterface = NetworkInterface.getByInetAddress(address); 
      return obtainMACFromAddress(networkInterface); 
     } catch (Exception e) { 
      if(log.isDebugEnabled()) { 
       log.debug("Failed to obtain MAC address for address " + hostnameOrIP, e); 
      } 
     } 

     // Means we had a failure. 
     return null; 
    } 

    /** 
    * Get the machine address of the machine we are currently running on. 
    * @return something like 08-00-27-DC-4A-9E or null if can't obtain mac 
    */ 
    public static String getMachineMac() { 
     try { 
      return obtainMACFromAddress(getNonLoopbackNetworkInterface()); 
     } catch (Exception e) { 
      if(log.isDebugEnabled()) { 
       log.debug("Failed to obtain MAC address for localhost", e); 
      } 
     } 

     return null; 
    } 

    /** 
    * Get machine hostname, based on IPv4 configurations. 
    * 
    * @return String representing FQDN or null if can't find hostname 
    */ 
    public static String getMachineHostname() { 
     try { 
      NetworkInterface networkInterface = getNonLoopbackNetworkInterface(); 
      Inet4Address address = getInet4Address(networkInterface); 
      if(address != null) 
       return address.getCanonicalHostName(); 
     } catch (Exception e) { 
      if(log.isDebugEnabled()) { 
       log.debug("Failed to obtain MachineHostname", e); 
      } 
     } 

     return null; 
    } 

    /** 
    * Get machine hostname, based on IPv6 configurations. 
    * @return String representing FQDN or null if can't find hostname 
    */ 
    public static String getMachineIPv6Hostname() { 
     try { 
      NetworkInterface networkInterface = getNonLoopbackNetworkInterface(); 
      Inet6Address address = getInet6Address(networkInterface); 
      if(address != null) 
       return address.getCanonicalHostName(); 
     } catch (Exception e) { 
      if(log.isDebugEnabled()) { 
       log.debug("Failed to obtain IPv6Hostname", e); 
      } 
     } 

     return null; 
    } 


    /** 
     * Get machine IP, based on IPv4 configurations. 
    * 
    * @return String representing IP or null if can't find properly configured interface 
    */ 
    public static String getMachineIPAddress() { 
     try { 
      NetworkInterface networkInterface = getNonLoopbackNetworkInterface(); 
      Inet4Address address = getInet4Address(networkInterface); 
      if(address != null) 
       return address.getHostAddress(); 
     } catch (Exception e) { 
      if(log.isDebugEnabled()) { 
       log.debug("Failed to obtain MachineIPAddress", e); 
      } 
     } 

     return null; 
    } 

    /** 
    * Get machine IP, based on IPv6 configurations. 
    * 
    * @return String representing IP or null if can't find properly configured interface 
    */ 
    public static String getMachineIPv6Address() { 
     try { 
      NetworkInterface networkInterface = getNonLoopbackNetworkInterface(); 
      Inet6Address address = getInet6Address(networkInterface); 
      if(address != null) 
       return address.getHostAddress(); 
     } catch (Exception e) { 
      if(log.isDebugEnabled()) { 
       log.debug("Failed to obtain MachineIPv6Address", e); 
      } 
     } 

     return null; 
    } 


    /* 
    * ######################## 
    * Helper private functions 
    */ 

    private static String obtainMACFromAddress(NetworkInterface networkInterface) throws SocketException { 
     if(networkInterface != null) { 
      byte[] mac = networkInterface.getHardwareAddress(); 
      if(mac == null) 
       throw new Error("Failed to obtain mac address from interface: " + networkInterface.getDisplayName()); 

      StringBuilder stringBuilder = new StringBuilder(17); 
      /* 
      * Extract each array of mac address and convert it to hexa with the 
      * following format 08-00-27-DC-4A-9E. 
      */ 
      for (int i = 0; i < mac.length; i++) { 
       stringBuilder.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); 
      } 

      return stringBuilder.toString(); 
     } 

     return null; 
    } 

    private static Inet4Address getInet4Address(NetworkInterface networkInterface) { 
     if(networkInterface != null) { 
      Enumeration<InetAddress> NICAddresses = networkInterface.getInetAddresses(); 
      while(NICAddresses.hasMoreElements()) { 
       InetAddress address = NICAddresses.nextElement(); 

       if(address instanceof Inet4Address) 
        return (Inet4Address)address; 
      } 
     } 

     return null; 
    } 

    private static Inet6Address getInet6Address(NetworkInterface networkInterface) { 
     if(networkInterface != null) { 
      Enumeration<InetAddress> NICAddresses = networkInterface.getInetAddresses(); 
      while(NICAddresses.hasMoreElements()) { 
       InetAddress address = NICAddresses.nextElement(); 

       if(address instanceof Inet6Address) 
        return (Inet6Address)address; 
      } 
     } 

     return null; 
    } 

    private static NetworkInterface getNonLoopbackNetworkInterface() throws SocketException { 
     // We need to iterate over all NIC's machine has because stupid ubuntu does not assign 
     // MAC address to default loopback interface... 
     Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces(); 
     while(b.hasMoreElements()) { 
      NetworkInterface networkInterface = b.nextElement(); 
      Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); 
      while(inetAddresses.hasMoreElements()) { 
       InetAddress address = inetAddresses.nextElement(); 
       if(!address.isLoopbackAddress()) 
        return networkInterface; 
      } 
     } 

     // Means we haven't found any non loopback interfaces. Bummer, return empty handed. 
     return null; 
    } 

} 

印在我的機器上執行以下操作:

MAC: 00-1C-C0-F2-BA-89 
HOSTNAME: maxim-desktop 
IP: 192.168.20.20 
HOSTNAME ipv6: fe80:0:0:0:21c:c0ff:fef2:ba89%2 
IP ipv6: fe80:0:0:0:21c:c0ff:fef2:ba89%2 
  • 正如你可以看到主機輸出IPv4的IPv6的&是不一樣的,我不知道它爲什麼要這樣呢?
  • 此外,%2的含義是什麼附加到ipv6相關查詢 - 這是我的代碼或JVM/Linux內核問題中的錯誤?

謝謝,
Maxim。

回答

1
  1. 如果你在看代碼,這裏是它做什麼:

    • 找到一個非迴環網絡接口
    • 查詢的IP地址(即你的情況,爲eth0)。該接口(在您的情況下,192.168.20.20或fe80:0:0:0:21c:c0ff:fef2:ba89%2)
    • 解析IP地址。正如您從hosts文件中看到的,192.168.20.20正確解析爲maxim-desktop,但沒有與您的IPv6地址相對應的主機名。
  2. IPv6地址與fe80開始(或者,更精確地說,fe80::/10網絡中的地址)是鏈路本地地址,而且%2表示哪個網絡接口的系統應使用。

+0

%2對我來說是新的,任何想法爲什麼它被添加到ipv6中? - ipv4不包含這種表示法。注意:我已經看了很多這個代碼,我是這個工具類的實現者:) – 2011-02-17 21:24:11

相關問題