3

So far, I do the following:

driver.get("http://www.whatismyip.com/");
String myIP = driver.findElement(By.id("greenip")).getText();
System.out.println(myIP);

Is there any better way?

(PS: I tried this solution But NetworkInterface give multiple IPs and on the other hand InetAddress.getLocalHost().getHostAddress() gives wrong IP. )

Community
  • 1
  • 1
  • You may want to check out http://stackoverflow.com/questions/8083479/java-getting-my-ip-address Or http://stackoverflow.com/questions/9481865/how-to-get-ip-address-of-our-own-system-using-java – Blue Ice Jul 26 '13 at 20:56
  • 1
    What do you mean by better? Faster, more reliable? The best way wouldn't involve selenium at all – Nathan Merrill Jul 26 '13 at 20:56
  • Thank you for reply. I use Selenium only because I do not know any other way. Is there any other way? – Бывший Мусор Jul 26 '13 at 20:58
  • I'm going to take a wild guess that you don't know anything about networking, and don't understand that your computer has a non-routable, private IP address. – Brian Roach Jul 26 '13 at 21:15
  • The best way to get your external IP is to to *ask someone else*, as in the website you are using. Leave it as that! – Arran Jul 26 '13 at 22:32

1 Answers1

1

the thing is there is no such thing like my ip. Your machine can have multiple interfaces and each interface can have multiple IPs. so i dnt really think you want to get into it. All you can do is to try web APIs to get your external IP address. Here is a code in java which is actually just looking for your ip on internet with the help of these sites

    public static String getIpAddress() 
    { 
    URL myIP;
    try {
        myIP = new URL("http://api.externalip.net/ip/");

        BufferedReader in = new BufferedReader(
                new InputStreamReader(myIP.openStream())
                );
        return in.readLine();
    } catch (Exception e) 
    {
        try 
        {
            myIP = new URL("http://myip.dnsomatic.com/");

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(myIP.openStream())
                    );
            return in.readLine();
        } catch (Exception e1) 
        {
            try {
                myIP = new URL("http://icanhazip.com/");

                BufferedReader in = new BufferedReader(
                        new InputStreamReader(myIP.openStream())
                        );
                return in.readLine();
            } catch (Exception e2) {
                e2.printStackTrace(); 
            }
        }
    }

return null;
}

now with this you can get your ip from the internet but again that is your external IP

Areeb Gillani
  • 440
  • 4
  • 25