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

后端 未结 13 1142
我寻月下人不归
我寻月下人不归 2020-12-13 04:15

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,

相关标签:
13条回答
  • 2020-12-13 04:34
    curl ifconfig.me
    

    or

    curl ifconfig.me/ip
    

    Incase you don't have curl installed,

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

    Hope this helps.

    0 讨论(0)
  • 2020-12-13 04:35

    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 */}
    
    0 讨论(0)
  • 2020-12-13 04:36

    ifcfg.me allows Lookup via

    nslookup
    telnet
    ftp
    and http

    even works with IPv6

    0 讨论(0)
  • 2020-12-13 04:41

    Simple but not elegant for this use. 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
    
    0 讨论(0)
  • 2020-12-13 04:42

    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.

    0 讨论(0)
  • 2020-12-13 04:43

    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.

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