如何在 Java 中确定路由器/网关的 IP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I determine the IP of my router/gateway in Java?
提问by Frank Krueger
How can I determine the IP of my router/gateway in Java? I can get my IP easily enough. I can get my internet IP using a service on a website. But how can I determine my gateway's IP?
如何在 Java 中确定路由器/网关的 IP?我可以很容易地获得我的IP。我可以使用网站上的服务获取我的互联网 IP。但是如何确定网关的 IP?
This is somewhat easy in .NET if you know your way around. But how do you do it in Java?
如果您了解自己的方法,这在 .NET 中会比较容易。但是你如何在 Java 中做到这一点呢?
采纳答案by Chris Bunch
Java doesn't make this as pleasant as other languages, unfortunately. Here's what I did:
不幸的是,Java 并不像其他语言那样令人愉快。这是我所做的:
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);
}
}
This presumes that the gateway is the second token and not the third. If it is, you need to add an extra st.nextToken();
to advance the tokenizer one more spot.
这假定网关是第二个令牌而不是第三个。如果是,您需要添加一个额外的东西st.nextToken();
来让分词器再前进一个位置。
回答by Brian
That is not as easy as it sounds. Java is platform independent, so I am not sure how to do it in Java. I am guessingthat .NET contacts some web site which reports it back. There are a couple ways to go. First, a deeper look into the ICMP protocol may give you the information you need. You can also trace the IP you go through (your route). When you encounter an IP that is not in the following ranges:
这并不像听起来那么容易。Java 是独立于平台的,所以我不确定如何在 Java 中做到这一点。我猜测.NET 会联系某个网站,然后将其报告回来。有几种方法可以走。首先,深入了解 ICMP 协议可能会为您提供所需的信息。您还可以跟踪您通过的 IP(您的路由)。当您遇到不在以下范围内的 IP 时:
- 10.0.0.0 – 10.255.255.255
- 172.16.0.0 – 172.31.255.255
- 192.168.0.0 – 192.168.255.255
- 10.0.0.0 – 10.255.255.255
- 172.16.0.0 – 172.31.255.255
- 192.168.0.0 – 192.168.255.255
it is the IP one hop away from yours, and probably shares a few octets of information with your IP.
它是距离您的 IP 一跳的 IP,并且可能与您的 IP 共享几个八位字节的信息。
Best of luck. I'll be curious to hear a definitive answer to this question.
祝你好运。我很想听到这个问题的明确答案。
回答by Nick Brosnahan
Try shelling out to traceroute if you have it.
如果你有的话,试着去追踪路由。
'traceroute -m 1 www.amazon.com' will emit something like this:
'traceroute -m 1 www.amazon.com' 将发出如下内容:
traceroute to www.amazon.com (72.21.203.1), 1 hops max, 40 byte packets
1 10.0.1.1 (10.0.1.1) 0.694 ms 0.445 ms 0.398 ms
Parse the second line. Yes, it's ugly, but it'll get you going until someone posts something nicer.
解析第二行。是的,它很丑,但它会让你继续前进,直到有人发布更好的东西。
回答by Matthew Schinckel
You may be better off using something like checkmyip.org, which will determine your public IP address - not necessarily your first hop router: at Uni I have a "real" IP address, whereas at home it is my local router's public IP address.
您最好使用诸如 checkmyip.org 之类的东西,它会确定您的公共 IP 地址 - 不一定是您的第一跳路由器:在 Uni 我有一个“真实”的 IP 地址,而在家里,它是我本地路由器的公共 IP 地址。
You can parse the page that returns, or find another site that allows you to just get the IP address back as the only string.
您可以解析返回的页面,或者找到另一个站点,该站点允许您将 IP 地址作为唯一的字符串返回。
(I'm meaning load this URL in Java/whatever, and then get the info you need).
(我的意思是在 Java 中加载这个 URL/任何东西,然后获取你需要的信息)。
This should be totally platform independent.
这应该是完全独立于平台的。
回答by Frank Krueger
Matthew: Yes, that is what I meant by "I can get my internet IP using a service on a website." Sorry about being glib.
马修:是的,这就是我所说的“我可以使用网站上的服务获取我的互联网 IP”。抱歉我是油嘴滑舌的。
Brian/Nick: Traceroute would be fine except for the fact that lots of these routers have ICMP disabled and thus it always stalls.
Brian/Nick:Traceroute 应该没问题,除了很多这些路由器都禁用了 ICMP 并因此它总是停滞不前。
I think a combination of traceroute and uPnP will work out. That is what I was planning on doing, I as just hoping I was missing something obvious.
我认为 traceroute 和 uPnP 的组合会奏效。这就是我打算做的,我只是希望我遗漏了一些明显的东西。
Thank you everyone for your comments, so it sounds like I'm not missing anything obvious. I have begun implementing some bits of uPnP in order to discover the gateway.
谢谢大家的评论,所以听起来我没有遗漏任何明显的东西。我已经开始实施一些 uPnP 以发现网关。
回答by Andreas Kraft
Regarding UPnP: be aware that not all routers support UPnP. And if they do it could be switched off (for security reasons). So your solution might not always work.
关于 UPnP:请注意,并非所有路由器都支持 UPnP。如果他们这样做,它可能会被关闭(出于安全原因)。因此,您的解决方案可能并不总是有效。
You should also have a look at NatPMP.
您还应该看看 NatPMP。
A simple library for UPnP can be found at http://miniupnp.free.fr/, though it's in C...
可以在http://miniupnp.free.fr/找到一个简单的 UPnP 库,尽管它是在 C 中...
回答by tardate
To overcome the issues mentioned with traceroute (ICMP-based, wide area hit) you could consider:
要克服 traceroute(基于 ICMP,广域命中)提到的问题,您可以考虑:
- traceroute to your public IP (avoids wide-area hit, but still ICMP)
- Use a non-ICMP utility like ifconfig/ipconfig (portability issues with this though).
- What seems the best and most portable solution for now is to shell & parse netstat (see the code example here)
- traceroute 到您的公共 IP(避免广域命中,但仍然是 ICMP)
- 使用非 ICMP 实用程序,如 ifconfig/ipconfig(尽管存在可移植性问题)。
- 目前看来最好和最便携的解决方案是 shell 和解析 netstat(请参阅此处的代码示例)
回答by bruceatk
On windows parsing the output of IPConfig will get you the default gateway, without waiting for a trace.
在 Windows 上解析 IPConfig 的输出将为您提供默认网关,而无需等待跟踪。
回答by Alnitak
On Windows, OSX, Linux, etc then Chris Bunch's answer can be much improved by using
在 Windows、OSX、Linux 等上,使用 Chris Bunch 的答案可以大大改进
netstat -rn
in place of a traceroute
command.
代替traceroute
命令。
Your gateway's IP address will appear in the second field of the line that starts either default
or 0.0.0.0
.
您网关的 IP 地址将出现在以default
或开头的行的第二个字段中0.0.0.0
。
This gets around a number of problems with trying to use traceroute
:
这解决了尝试使用的许多问题traceroute
:
- on Windows
traceroute
is actuallytracert.exe
, so there's no need for O/S dependencies in the code - it's a quick command to run - it gets information from the O/S, not from the network
traceroute
is sometimes blocked by the network
- 在 Windows
traceroute
上实际上是tracert.exe
,因此代码中不需要 O/S 依赖项 - 这是一个快速运行的命令 - 它从 O/S 获取信息,而不是从网络获取
traceroute
有时被网络封锁
The only downside is that it will be necessary to keep reading lines from the netstat
output until the right line is found, since there'll be more than one line of output.
唯一的缺点是必须从netstat
输出中读取行,直到找到正确的行,因为将有不止一行输出。
EDIT:The Default Gateway's IP Address is in the second field of the line that starts with 'default' if you are on a MAC (tested on Lion), or in the third fieldof the line that starts with '0.0.0.0' (tested on Windows 7)
编辑:如果您使用的是 MAC(在 Lion 上测试),则默认网关的 IP 地址位于以“default”开头的行的第二个字段中,或者位于以“0.0.0.0”开头的行的第三个字段中(在 Windows 7 上测试)
Windows:
视窗:
Network Destination Netmask Gateway Interface Metric
0.0.0.0 0.0.0.0 192.168.2.254192.168.2.46 10
网络目标网络掩码网关接口指标
0.0.0.0 0.0.0.0 192.168.2.254192.168.2.46 10
Mac:
苹果电脑:
Destination Gateway Flags Refs Use Netif Expire
default 192.168.2.254UGSc 104 4 en1
目标网关标志引用使用 Netif 过期
默认 192.168.2.254UGSc 104 4 en1
回答by Hamza Yerlikaya
try{
String gateway;
Process result = Runtime.getRuntime().exec("netstat -rn");
BufferedReader output = new BufferedReader(new InputStreamReader(result.getInputStream()));
String line = output.readLine();
while(line != null){
if ( line.trim().startsWith("default") == true || line.trim().startsWith("0.0.0.0") == true )
break;
line = output.readLine();
}
if(line==null) //gateway not found;
return;
StringTokenizer st = new StringTokenizer( line );
st.nextToken();
st.nextToken();
gateway = st.nextToken();
System.out.println("gateway is: "+gateway);
} catch( Exception e ) {
System.out.println( e.toString() );
gateway = new String();
adapter = new String();
}