Tech Off Thread

11 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

detecting public ip

Back to Forum: Tech Off
  • User profile image
    ploe

    i writing a program to which i need the machine's public ip address. any one have a suggestion on how to get that ip?

    the only solution i can think of is accessing a site like whatismyip.com and extracting the ip from the html. :/

  • User profile image
    Maurits

    You could walk through the interfaces, but that wouldn't help if you were behind a NAT router.

    It's a lot easier to detect the connecting IP from the other end of the connection, which is why things like whatismyip.com exist.

  • User profile image
    Minh

    You could then make a request to whatismyip.com and parse the response. Low-tech, but it works.

  • User profile image
    RobChartier

    Get an app like directupdate.  Works great with a crap-load of options for IP detection.

    http://directupdate.net/

  • User profile image
    VBJB

    The December MSDN magazine has an article about this. Here is a link http://msdn.microsoft.com/msdnmag/issues/05/12/AdvancedBasics/default.aspx 

  • User profile image
    Sven Groot

    You can use this webservice to get public IP information, just dump the URL in the Add Web Reference dialog in VS and you're good to go.

    The web service belongs to this MSDN Magazine article which also shows an example on how to use it.

  • User profile image
    ploe

    good article Smiley

  • User profile image
    burtriddley

    It would be wise to run your own webservice. You never know if 3rd party service is running or if the url changes.

  • User profile image
    androidi

    Do NOT do this. Expressionless

    I had this silly idea some time ago and it's too stupid for not sharing [A]


    One could once a month or so download the servers.ini from ****.com or one of its mirrors, then go through the servers.ini, (identd should be running and the host not firewalled plus a bunch of other stuff) and on successful connect query your own ip.

  • User profile image
    ploe

    sorry i failed to mention that the project was in java therefore the specifics didn't apply but it is the same principle anyway Smiley

    so this is what i ended up doing...

    1) i created a page that detects the visitors ip using some simple PHP:
    http://nikto.canisius.edu/~puehnd/getip.php
    <html>
    <head>
    <?php $ip = $_SERVER['REMOTE_ADDR']; ?>
    <title><?php echo $ip; ?></title>
    </head>
    <body>
    <?php echo "Your IP is: " . $ip; ?>
    </body>
    </html>

    2) i created a method to fetch the ip from the page
    //Gets the public IP of the current machine
        public static String FetchPublicIP() {
            String ip = "";
            
            try {
                URL youRL = new URL("http://nikto.canisius.edu/~puehnd/getip.php"); //default service
                BufferedReader in = new BufferedReader(new InputStreamReader(youRL.openStream()));
                String s = "";
                while ((s = in.readLine()) != null) {
                    if (s.startsWith("<title>")) {
                        ip = s.substring(s.indexOf('>')+1, s.lastIndexOf('<'));
                        break;
                    }
                }        
                in.close();
            } catch (IOException e) {}
            
            if (isValidHost(ip)) //make sure ip found is valid
                return ip;
            else
                return "";
        }

    thanks, problem solved.

  • User profile image
    androidi

    I used to use something quite similar, however this is not reliable due to variety of reasons.


    #!/usr/bin/perl
    print "Content-type: text\n\n";
    print $ENV{REMOTE_ADDR};


    #!/opt/perl/bin/perl -w
    use strict;
    use CGI qw(:standard);
    print header(), start_html("IP Address"),p($ENV{'REMOTE_ADDR'}), end_html();

    I just tested these and got some load balancer or perhaps proxy ip.





Conversation locked

This conversation has been locked by the site admins. No new comments can be made.