当前位置: 动力学知识库 > 问答 > 编程问答 >

windows - How do I find out what my external IP address is?

问题描述:

My computers are sitting behind a router/firewall. How do I programmatically find out what my external IP address is. I can use http://www.whatsmyip.org/ for ad-hoc queries, but the TOS don't allow for automated checks.

Any ideas?

网友答案:

http://ipecho.net/plain appears to be a workable alternative, as whatismyip.com now requires membership for their automated link. They very kindly appear to be offering this service for free, so please don't abuse it.

网友答案:

Unfortunately there is no easy way to do it.

I would use a site like www.whatsmyip.org and parse the output.

checkip.dyndns.com returns a very simple HTML file which looks like this:

<html>
  <head>
    <title>Current IP Check</title>
  </head>
  <body>
    Current IP Address: 84.151.156.163
  </body>
</html>

This should be very easy to parse. Moreover the site is exists for about ten years. There is hope that it will be around for a while.

网友答案:

If you have access to a webserver with modphp, you can roll your own:

<?php print $_SERVER['REMOTE_ADDR']; ?>

If you don't want that to get abused, you'll have to keep it secret or add request limits.

I've been using one on my server for years.

Explicitly:

Create a file called whatismyip.php in your public_html folder in your website. It can be called anything and be anywhere in your webroot.

Add the line above, and then query your server:

curl http://example.com/whatismyip.php

for example.

网友答案:

Unfortunately as of 2013, whatismyip.com charge for the service.

http://www.icanhazip.com is still going strong, 3 years later. Just outputs the IP as text, absolutely nothing else.

http://checkip.dyndns.org still works as well.

You can also use Google if you want to be sure it won't go down, but it can still block you for TOS violations.

https://www.google.ie/search?q=whats+is+my+ip

But even when they block me, they still tell me my client IP address in the error message.

网友答案:

If the router you are behind speak UPnP you could always use a UPnP library for whatever language you are developing in to query the router for its external ip.

网友答案:

Another way is if you have access to a cloud email (yahoo, google, hotmail), send yourself an email. Then view the headers and you should see your IP address in there.

I would look up the exact area but the headers may vary from each implmentation, Look for the received-by and follow that until you get to something that looks like sent-by

EDIT: This answers the how to find IP address, not the via PROGRAMMATIC approach

网友答案:

My WRT54G router tells me through its Local Router Access feature (the http(s) administration interface), and I imagine something similar could be done with many other devices. In this case, the entry page gives the octets of the IPv4 address in four lines containing this phrase:

class=num maxLength=3 size=3 value='i' name='wan_ipaddr_N' id='wan_ipaddr_N'

Where i is the octet value and N is the octet number. This bit of doggerel fetches and parses it for me, courtesy of cygwin:

#! /usr/bin/env perl
use strict;
use warnings 'all';

my( $account, $password ) = @ARGV;

open QUERY,
    "curl --sslv3 --user '$account:$password' https://Linksys/ --silent |"
    or die "Failed to connect to router";

my @ipaddr = ('x','x','x','x');

while( <QUERY> ) {
    $ipaddr[$2] = $1 if /value='(\d+)' name='wan_ipaddr_([0-3])/;
}
close QUERY;
print join('.', @ipaddr);

There is no guarantee that this will work with all versions of the router firmware.

If your router is set to use http for this interface, drop the --sslv3 curl option, and you can use dotted-decimal notation to address the router. To use https with the curl options above, I also did this:

  1. Used a browser to fetch the router's self-signed certificate (saved as Linksys.crt).

  2. Added it to my CA bundle:

    openssl x509 -in Linksys.crt -text >> /usr/ssl/certs/ca-bundle.crt
    
  3. Added 'Linksys' to my hosts file (C:\Windows\System32\Drivers\etc\HOSTS on my Win8 box), as an alias for the router's address. If the dotted-decimal notation is given to curl instead of this alias, it rejects the connection on account of a certificate subject name mismatch.

Alternatively, you could just use the --insecure option to bypass certificate verification, which probably makes more sense in the circumstances.

网友答案:
curl ifconfig.me

or

curl ifconfig.me/ip

Incase you don't have curl installed,

wget ifcondig.me/ip 2>/dev/null && cat ip

Hope this helps.

网友答案:

http://myexternalip.com provides this kind of information. To retrieve your IP you have plenty of options:

  • http://myexternalip.com/ - browser + lot's of examples of how to use it
  • http://myexternalip.com/raw - a pure text answer, only your ip, no other crap
  • http://myexternalip.com/json - a resposnse ready for json-parsers, also supports jsonp
  • HEAD http://myexternalip.com - send only a HEAD-request and get the answer
网友答案:

whatismyip.com or ipchicken.com are very easy to parse.

If you have a webhost or vps you can also determine it, without fear of it randomly going down leaving you stuck.

网友答案:

ifcfg.me allows Lookup via

nslookup
telnet
ftp
and http

even works with IPv6

网友答案:

Since this question was asked a while back, there's now a freely available web service designed specifically to allow you to determine your IP address programmatically, called ipify.

$ curl 'https://api.ipify.org?format=json'

Results in

{"ip": "1.2.3.4" /* your public IP */}
网友答案:

Simple but not elegant for this use. I installed some security cameras and my cable provider - Wide Open West or WOWWAY wanted $10 a month to set up a static IP - EXTORTION for a DHCP reservation ... so, I created a VBS file with the following code to drop the result to dropbox and google drive ... have to delete the file for new one to sync though for some reason.

This runs on a PC at my home. My PC is set to resume on power outage and a task is scheduled to run this every day once (note if you have it run often, the site will block your requests).

Now I can get my IP address on the road and watch people steal my stuff :-)

get_html "http://ipecho.net/plain", "C:\Users\joe\Google Drive\IP.html"

get_html "http://ipecho.net/plain", "C:\Users\joe\Dropbox\IP.html"



sub get_html (up_http, down_http)

dim xmlhttp : set xmlhttp = createobject("msxml2.xmlhttp.3.0")

xmlhttp.open "get", up_http, false

xmlhttp.send

dim fso : set fso = createobject ("scripting.filesystemobject")

dim newfile : set newfile = fso.createtextfile(down_http, true)

newfile.write (xmlhttp.responseText)

newfile.close

set newfile = nothing
set xmlhttp = nothing

end sub
分享给朋友:
您可能感兴趣的文章:
随机阅读: