简体   繁体   English

如何获取所连接接口的IP地址

[英]how to get the ip address of the connected interface

the way how we get Ip address using java code is clear and many questions about this subject were answered. 使用Java代码获取IP地址的方式很明确,并且回答了许多与此主题有关的问题。

but assuming that you have Vmware or VirtualBox on your machine,so you will have extra virtual network cards each one have its own Ip Address. 但是假设您的计算机上装有Vmware或VirtualBox,那么您将拥有额外的虚拟网卡,每个网卡都有其自己的IP地址。

when executing a little program the result was like 192.168.xx which belongs to one of my virtual network adapters. 当执行一个小程序时,结果类似于192.168.xx,它属于我的虚拟网络适配器之一。 but using "What Is My IP" the result was like 197.xxx 但是使用“什么是我的IP”,结果就像197.xxx

so how can i get the ip adress of the connected interface ? 那么如何获得连接接口的IP地址呢?

Your interface IP 192.168.xx is your local adapter interface. 接口IP 192.168.xx是本地适配器接口。 The prefix 192.168 indicates that this is a private non-routable address that is valid only behind a NAT firewall. 前缀192.168表示这是一个私有的不可路由地址,仅在NAT防火墙之后有效。

The IP returned by WhatIsMyIP is the external address of the firewall/gateway through which you access the Internet. WhatIsMyIP返回的IP是访问Internet的防火墙/网关的外部地址。 Unless your computer is connected directly to the Internet these two addresses will never be the same. 除非您的计算机直接连接到Internet,否则这两个地址将永远不会相同。

this should do the trick: 这应该可以解决问题:

import java.io.*;
import java.util.*;

public class ExecTest {
public static void main(String[] args) throws IOException {
    Process result = Runtime.getRuntime().exec("traceroute -m 1 www.amazon.com");

    BufferedReader output = new BufferedReader(new            InputStreamReader(result.getInputStream()));
    String thisLine = output.readLine();
    StringTokenizer st = new StringTokenizer(thisLine);
    st.nextToken();
    String gateway = st.nextToken();
    System.out.printf("The gateway is %s\n", gateway);
}
}

Courtesy of Chris Bunch from: How can I determine the IP of my router/gateway in Java? 来自Chris Bunch,来自: 如何确定Java中路由器/网关的IP?

Greets! 问候!

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

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