如何在Java中确定路由器/网关的IP? [英] How can I determine the IP of my router/gateway in Java?

查看:510
本文介绍了如何在Java中确定路由器/网关的IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

如果您了解自己的方式,这在.NET中有点容易。但是你是如何用Java做的呢?

This is somewhat easy in .NET if you know your way around. But how do you do it in Java?

推荐答案

遗憾的是,Java并不像其他语言那样令人愉快。这就是我的所作所为:

Java doesn't make this as pleasant as other languages, unfortunately. Here's what I did:

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(); 以使标记生成器再推进一次。

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.

这篇关于如何在Java中确定路由器/网关的IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆