如何通过Java获取Linux上的计算机的IP?

12 java linux networking ip-address

如何通过Java获取Linux上的计算机的IP?

我在网上搜索了一些例子,我发现了一些关于NetworkInterface类的内容,但我无法理解我是如何得到IP地址的.

如果我在同一时间运行多个网络接口会发生什么?将返回哪个IP地址.

我真的很感激一些代码示例.

PS:我到目前为止使用的是InetAddress类,这对于跨平台应用程序来说是一个糟糕的解决方案.(WIN/Linux的).

adr*_*rau 30

不要忘记外部不可见的环回地址.这是一个提取第一个非环回IP(IPv4或IPv6)的函数

private static InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6) throws SocketException {
    Enumeration en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
        NetworkInterface i = (NetworkInterface) en.nextElement();
        for (Enumeration en2 = i.getInetAddresses(); en2.hasMoreElements();) {
            InetAddress addr = (InetAddress) en2.nextElement();
            if (!addr.isLoopbackAddress()) {
                if (addr instanceof Inet4Address) {
                    if (preferIPv6) {
                        continue;
                    }
                    return addr;
                }
                if (addr instanceof Inet6Address) {
                    if (preferIpv4) {
                        continue;
                    }
                    return addr;
                }
            }
        }
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)


gre*_*awk 13

来自Java Tutorial

为什么InetAddress不是一个好的解决方案?我没有在关于跨平台兼容性的文档中看到任何内容?

此代码将枚举所有网络接口并检索其信息.

import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;

public class ListNets 
{
    public static void main(String args[]) throws SocketException {
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets))
            displayInterfaceInformation(netint);
    }

    static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
        out.printf("Display name: %s\n", netint.getDisplayName());
        out.printf("Name: %s\n", netint.getName());
        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            out.printf("InetAddress: %s\n", inetAddress);
        }
        out.printf("\n");
     }
}  
Run Code Online (Sandbox Code Playgroud)

以下是示例程序的示例输出:

Display name: bge0
Name: bge0
InetAddress: /fe80:0:0:0:203:baff:fef2:e99d%2
InetAddress: /121.153.225.59

Display name: lo0
Name: lo0
InetAddress: /0:0:0:0:0:0:0:1%1
InetAddress: /127.0.0.1
Run Code Online (Sandbox Code Playgroud)

  • 要回答"为什么InetAddress不是一个好的解决方案?"的问题:在主机名在/ etc/hosts中列为127.0.0.1的linux系统上,结果不是很有用.在主机名未出现在/ etc/hosts或dns中的系统上,会出现UnknownHostException.在os x上,主机名似乎通常可以很好地解决.我不知道窗户上的情况如何,但我认为它运作良好. (3认同)

小智 5

这段代码工作4me:

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


public class ShowIp {

    public static void main(String[] args) throws SocketException {
        NetworkInterface ni = NetworkInterface.getByName("eth0");
        Enumeration<InetAddress> inetAddresses =  ni.getInetAddresses();


        while(inetAddresses.hasMoreElements()) {
            InetAddress ia = inetAddresses.nextElement();
            if(!ia.isLinkLocalAddress()) {
                System.out.println("IP: " + ia.getHostAddress());
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)