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

前端 未结 16 2094
无人及你
无人及你 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:06

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

    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.

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

    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 guessing that .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:

    • 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.

    Best of luck. I'll be curious to hear a definitive answer to this question.

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

    On Windows, OSX, Linux, etc then Chris Bunch's answer can be much improved by using

    netstat -rn
    

    in place of a traceroute command.

    Your gateway's IP address will appear in the second field of the line that starts either default or 0.0.0.0.

    This gets around a number of problems with trying to use traceroute:

    1. on Windows traceroute is actually tracert.exe, so there's no need for O/S dependencies in the code
    2. it's a quick command to run - it gets information from the O/S, not from the network
    3. traceroute is sometimes blocked by the network

    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.

    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 field of the line that starts with '0.0.0.0' (tested on Windows 7)

    Windows:

    Network Destination Netmask Gateway Interface Metric

    0.0.0.0 0.0.0.0 192.168.2.254 192.168.2.46 10

    Mac:

    Destination Gateway Flags Refs Use Netif Expire

    default 192.168.2.254 UGSc 104 4 en1

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

    output of netstat -rn is locale specific. on my system (locale=de) the output looks like: ... Standardgateway: 10.22.0.1

    so there is no line starting with 'default'.

    so using netstat might be no good idea.

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

    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.

    You should also have a look at NatPMP.

    A simple library for UPnP can be found at http://miniupnp.free.fr/, though it's in C...

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

    This Version connects to www.whatismyip.com, reads the content of the site and searches via regular expressions the ip adress and prints it to the cmd. Its a little improvement of MosheElishas Code

    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();
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题