How can I determine the IP of my router/gateway in Java?

前端 未结 16 2095
无人及你
无人及你 2020-11-27 06:43

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 gat

相关标签:
16条回答
  • 2020-11-27 07:14

    You can use netstat -rn command which is available on Windows, OSX, Linux, etc platform. Here is my code:

    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
    
    0 讨论(0)
  • 2020-11-27 07:15

    On windows parsing the output of IPConfig will get you the default gateway, without waiting for a trace.

    0 讨论(0)
  • 2020-11-27 07:18

    Try shelling out to traceroute if you have it.

    'traceroute -m 1 www.amazon.com' will emit something like this:

    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.

    0 讨论(0)
  • 2020-11-27 07:19

    I'm not sure if it works on every system but at least here I found this:

    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];
            }
        }
    }
    

    For me this produces:

    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
    

    I'd require more tests on other Systems/Configurations/PC-Gateway-Setups to confirm if it works everywhere. Kind of doubt it but this was the first I found.

    0 讨论(0)
  • 2020-11-27 07:22

    Matthew: Yes, that is what I meant by "I can get my internet IP using a service on a website." Sorry about being glib.

    Brian/Nick: Traceroute would be fine except for the fact that lots of these routers have ICMP disabled and thus it always stalls.

    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.

    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.

    0 讨论(0)
  • 2020-11-27 07:23

    In windows you can just use the following command:

    ipconfig | findstr /i "Gateway"
    

    Which will give you output like:

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

    However I can't run this command with Java, gonna post when I figure this out.

    0 讨论(0)
提交回复
热议问题