简体   繁体   English

我如何从Java获取Linux机器中的实际IP

[英]How can I get the actual IP in a linux machine from Java

I am trying to get the local IP address from a Linux machine BUT NOT get the loopback. 我正在尝试从Linux机器获取本地IP地址, 但未获得回送。
To do that I am using the following code (I am not sure if what I am doing is correct): 为此,我正在使用以下代码(不确定所执行的操作是否正确):

NetworkInterface ni = NetworkInterface.getByName("eth0");    
Enumeration<InetAddress> inetAddresses =  ni.getInetAddresses();
while(inetAddresses.hasMoreElements()) {  
         InetAddress ia = inetAddresses.nextElement();  
         if(!ia.isLinkLocalAddress()) {  
              //this is not loopback  
         }    
}  

When I run this I get 2 IPs (I was interested only in one of these) which when I do an ifconfig I see one (the one I want to get) is in the entry for eth0 while the other is in the entry for eth0:54 . 当我运行它时,我得到2个IP(我只对其中之一感兴趣),当我执行ifconfig我看到其中一个(我想要获得的)在eth0的条目中,而另一个在eth0:54的条目中eth0:54
I don't even know what is eth0:54 . 我什至不知道eth0:54是什么。

How can I get the IP I want? 如何获得我想要的IP?

Linux machines can have more than one IP address including loopback. Linux机器可以具有多个IP地址,包括回送。 There is no concept of uniqueness for IP addresses. IP地址没有唯一性的概念。

What you might be looking for is the hostname (and its IP address) You can get this by reading /etc/hostname and looking up its IP address. 您可能要查找的是主机名(及其IP地址),您可以通过读取/ etc / hostname并查找其IP地址来获得。 Note: its possible it doesn't have an IP address if the machine is not setup in a normal manner. 注意:如果未以正常方式设置机器,则可能没有IP地址。

I had the same question but using PHP instead of Java: 我有同样的问题,但使用PHP而不是Java:

Simply find the ip address of server 只需找到服务器的IP地址

The best answer was that its not generally possible without serious back flips which doesn't really have to do with the language you are using and more to do with the underlying system. 最好的答案是,如果没有严重的后向翻转,通常是不可能的,这实际上与您使用的语言无关,而与底层系统无关。

Any modern computer have multiple IP-numbers, 127.0.0.1 being one of them. 任何现代计算机都有多个IP号码,其中127.0.0.1是其中之一。 The actual configuration does not always get correctly reported up to the Java layer (in my experience). 实际配置并不一定总能正确报告到Java层(以我的经验)。

You may simply want to execute /sbin/ifconfig -a on a scheduled basis (or at startup time) and log the complete output. 您可能只想按计划(或在启动时)执行/sbin/ifconfig -a并记录完整的输出。

Try this, 尝试这个,

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, UnknownHostException {
     System.out.println(System.getProperty("os.name"));
     Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets))
        if (netint.getName().equals("wlan0") || netint.getName().equals("en0")) {
             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");
 }
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM