如何在 Java 中确定路由器/网关的 IP?我可以很容易地获得我的IP。我可以使用网站上的服务获取我的互联网 IP。但我如何确定我的网关的 IP?

如果您了解相关方法,那么在 .NET 中这有点容易。但在 Java 中如何做到这一点呢?

有帮助吗?

解决方案

不幸的是,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);
    }
}

这假设网关是第二个令牌而不是第三个。如果是,则需要额外添加 st.nextToken(); 将分词器再推进一位。

其他提示

在 Windows、OSX、Linux 等上,Chris Bunch 的答案可以通过使用得到很大的改进

netstat -rn

代替一个 traceroute 命令。

您的网关的 IP 地址将出现在以下任一行开头的第二个字段中: default 或者 0.0.0.0.

这解决了尝试使用时的许多问题 traceroute:

  1. 在 Windows 上 traceroute 实际上是 tracert.exe, ,所以代码中不需要操作系统依赖
  2. 这是一个快速运行的命令 - 它从操作系统而不是网络获取信息
  3. traceroute 有时会被网络屏蔽

唯一的缺点是需要不断地读取行 netstat 输出直到找到正确的行,因为将有不止一行输出。

编辑: 如果您使用的是 MAC(在 Lion 上测试),则默认网关的 IP 地址位于以“default”开头的行的第二个字段中,或者在 第三场 以“0.0.0.0”开头的行(在 Windows 7 上测试)

视窗:

网络目标网络掩码网关接口指标

0.0.0.0 0.0.0.0 192.168.2.254 192.168.2.46 10

苹果:

目标网关标志参考使用 Netif 过期

默认 192.168.2.254 UGSc 104 4 en1

try{
    Process result = Runtime.getRuntime().exec("netstat -rn");

    BufferedReader output = new BufferedReader(new InputStreamReader(result.getInputStream()));

    String line = output.readLine();
    while(line != null){
        if ( line.startsWith("default") == true )
            break;      
        line = output.readLine();
    }

    StringTokenizer st = new StringTokenizer( line );
    st.nextToken();
    gateway = st.nextToken();
    st.nextToken();
    st.nextToken();
    st.nextToken();
    adapter = st.nextToken();

} catch( Exception e ) { 
    System.out.println( e.toString() );
    gateway = new String();
    adapter = new String();
}

在 Windows 上,解析 IPConfig 的输出将为您提供默认网关,而无需等待跟踪。

您可能最好使用 checkmyip.org 之类的东西,它将确定您的公共 IP 地址 - 不一定是您的第一跳路由器:在大学,我有一个“真实”IP 地址,而在家里,它是我本地路由器的公共 IP 地址。

您可以解析返回的页面,或者找到另一个允许您将 IP 地址作为唯一字符串获取的站点。

(我的意思是用 Java/其他方式加载这个 URL,然后获取您需要的信息)。

这应该是完全独立于平台的。

关于 UPnP:请注意,并非所有路由器都支持 UPnP。如果他们这样做,它可能会被关闭(出于安全原因)。所以你的解决方案可能并不总是有效。

您还应该看看 NatPMP。

可以在以下位置找到一个简单的 UPnP 库: http://miniupnp.free.fr/, ,虽然是C语言...

要克服 Traceroute (基于 ICMP 的广域命中)提到的问题,您可以考虑:

  1. 跟踪路由到您的公共 IP(避免广域命中,但仍然是 ICMP)
  2. 使用非 ICMP 实用程序,如 ifconfig/ipconfig(但存在可移植性问题)。
  3. 目前看来最好、最便携的解决方案是 shell 和解析 netstat (请参阅代码示例 这里)

netstat -rn 的输出是特定于区域设置的。在我的系统(locale=de)上,输出如下所示:...标准网关:10.22.0.1

所以没有以“default”开头的行。

所以使用 netstat 可能不是一个好主意。

此版本连接到 www.whatismyip.com,读取网站内容并通过正则表达式搜索 IP 地址并将其打印到 cmd。它是对 MosheElishas 代码的一点改进

import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader; 
import java.net.URL;  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  

public class Main {

    public static void main(String[] args) {
        BufferedReader buffer = null;
        try {
            URL url = new URL(
                    "http://www.whatismyip.com/tools/ip-address-lookup.asp");
            InputStreamReader in = new InputStreamReader(url.openStream());
            buffer = new BufferedReader(in);
            String line = buffer.readLine();
            Pattern pattern = Pattern
                    .compile("(.*)value=\"(\\d+).(\\d+).(\\d+).(\\d+)\"(.*)");
            Matcher matcher;
            while (line != null) {
                matcher = pattern.matcher(line);
                if (matcher.matches()) {
                    line = matcher.group(2) + "." + matcher.group(3) + "."
                            + matcher.group(4) + "." + matcher.group(5);
                    System.out.println(line);
                }
                line = buffer.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();

        } finally {
            try {
                if (buffer != null) {
                    buffer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader; 
import java.net.URL;  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  

public class Main {

    public static void main(String[] args) {
        BufferedReader buffer = null;
        try {
            URL url = new URL(
                    "http://www.whatismyip.com/tools/ip-address-lookup.asp");
            InputStreamReader in = new InputStreamReader(url.openStream());
            buffer = new BufferedReader(in);
            String line = buffer.readLine();
            Pattern pattern = Pattern
                    .compile("(.*)value=\"(\\d+).(\\d+).(\\d+).(\\d+)\"(.*)");
            Matcher matcher;
            while (line != null) {
                matcher = pattern.matcher(line);
                if (matcher.matches()) {
                    line = matcher.group(2) + "." + matcher.group(3) + "."
                            + matcher.group(4) + "." + matcher.group(5);
                    System.out.println(line);
                }
                line = buffer.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();

        } finally {
            try {
                if (buffer != null) {
                    buffer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

这并不像听起来那么容易。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

它是距离您的 IP 一跳的 IP,并且可能与您的 IP 共享几个八位位组的信息。

祝你好运。我很想听到这个问题的明确答案。

如果你有的话,尝试使用traceroute。

'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

解析第二行。是的,它很丑陋,但它会让你继续前进,直到有人发布更好的东西。

马修:是的,这就是“我可以在网站上使用服务获取Internet IP”的意思。对不起,要成为笨拙的人。

布莱恩/尼克:Traceroute 应该没问题,只是很多路由器都禁用了 ICMP,因此它总是停止运行。

我认为将 traceroute 和 uPnP 结合起来会有效。这就是我打算做的事情,我只是希望我错过了一些明显的事情。

谢谢大家的评论,所以听起来我没有遗漏任何明显的东西。我已经开始实施 uPnP 的一些功能来发现网关。

您可以查询网址“http://whatismyip.com/automation/n09230945.asp”。例如:

    BufferedReader buffer = null;
    try {
        URL url = new URL("http://whatismyip.com/automation/n09230945.asp");
        InputStreamReader in = new InputStreamReader(url.openStream());
        buffer = new BufferedReader(in);

        String line = buffer.readLine();
        System.out.println(line);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (buffer != null) {
                buffer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

在 Windows 中,您只需使用以下命令即可:

ipconfig | findstr /i "Gateway"

这将为您提供如下输出:

Default Gateway . . . . . . . . . : 192.168.2.1
Default Gateway . . . . . . . . . : ::

但是我无法用 Java 运行这个命令,当我弄清楚这个问题时我会发布。

您可以使用 netstat -rn 命令可在 Windows、OSX、Linux 等平台上使用。这是我的代码:

private String getDefaultAddress() {
        String defaultAddress = "";
        try {
            Process result = Runtime.getRuntime().exec("netstat -rn");

            BufferedReader output = new BufferedReader(new InputStreamReader(
                    result.getInputStream()));

            String line = output.readLine();
            while (line != null) {
                if (line.contains("0.0.0.0")) {

                    StringTokenizer stringTokenizer = new StringTokenizer(line);
                    stringTokenizer.nextElement(); // first element is 0.0.0.0
                    stringTokenizer.nextElement(); // second element is 0.0.0.0
                    defaultAddress = (String) stringTokenizer.nextElement();
                    break;
                }

                line = output.readLine();

            } // while
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return defaultAddress;

} // getDefaultAddress

我不确定它是否适用于每个系统,但至少在这里我发现了这一点:

import java.net.InetAddress;
import java.net.UnknownHostException;
public class Main
{
    public static void main(String[] args)
    {
        try
        {
            //Variables to find out the Default Gateway IP(s)
            String canonicalHostName = InetAddress.getLocalHost().getCanonicalHostName();
            String hostName = InetAddress.getLocalHost().getHostName();

            //"subtract" the hostName from the canonicalHostName, +1 due to the "." in there
            String defaultGatewayLeftover = canonicalHostName.substring(hostName.length() + 1);

            //Info printouts
            System.out.println("Info:\nCanonical Host Name: " + canonicalHostName + "\nHost Name: " + hostName + "\nDefault Gateway Leftover: " + defaultGatewayLeftover + "\n");
            System.out.println("Default Gateway Addresses:\n" + printAddresses(InetAddress.getAllByName(defaultGatewayLeftover)));
        } catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
    }
    //simple combined string out of the address array
    private static String printAddresses(InetAddress[] allByName)
    {
        if (allByName.length == 0)
        {
            return "";
        } else
        {
            String str = "";
            int i = 0;
            while (i < allByName.length - 1)
            {
                str += allByName[i] + "\n";
                i++;
            }
            return str + allByName[i];
        }
    }
}

对我来说这会产生:

Info:
Canonical Host Name: PCK4D-PC.speedport.ip
Host Name: PCK4D-PC
Default Gateway Leftover: speedport.ip

Default Gateway Addresses:
speedport.ip/192.168.2.1
speedport.ip/fe80:0:0:0:0:0:0:1%12

我需要对其他系统/配置/PC 网关设置进行更多测试,以确认它是否适用于所有地方。有点怀疑,但这是我第一次发现。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top