DEV Community

Cover image for Getting your IP from anywhere
Sonny Alves Dias
Sonny Alves Dias

Posted on • Originally published at sonny.alvesdi.as

Getting your IP from anywhere

There are several circumstances where you need to know the IP of your current machine. It may also be programmatically through a script or an Ansible playbook.

To do that you can curl a website like ifconfig.co, icanhazip.com, or even myip.com(you need to parse the JSON though).
For example:

curl ifconfig.co
Enter fullscreen mode Exit fullscreen mode

On an AWS instance, you can reliably curl http://169.254.169.254/latest/meta-data/ to retrieve IPs and other info on your current instance. More details there https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html

While these APIs are free, they are 3rd parties and may have API rate limits. One day they could also disappear or not function, right? To avoid any incident, we want to find a reliable solution that lasts. And if it can also work in an internal network without internet it would be perfect.

In that case, I recommend you create a new endpoint on any domain you like. And deploy the open-source software behind ifconfig.co: https://github.com/mpolden/echoip

I deployed it myself, and it is available here: https://ip.alvesdi.as.

Here is how I did. First, I pulled the Docker image:

docker pull mpolden/echoip
Enter fullscreen mode Exit fullscreen mode

Then to activate the Geolocation, I went to MaxMind website here: https://dev.maxmind.com/geoip/geoip2/geolite2/
Signed up for a free account and downloaded the GeoLite 2 Databases for ASN, Cities, and Countries. I also made a script to automate the update of my databases daily using my license key. Here is the script I use:

#!/bin/bash
LICENSE_KEY= # Fill your key
wget "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key=${LICENSE_KEY}&suffix=tar.gz" -O ASN.tar.gz
wget "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=${LICENSE_KEY}&suffix=tar.gz" -O City.tar.gz
wget "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=${LICENSE_KEY}&suffix=tar.gz" -O Country.tar.gz

for file in *.tar.gz; do tar xzvf "${file}" --strip-components 1 && rm "${file}"; done
Enter fullscreen mode Exit fullscreen mode

And then here is how I start the container:

docker run --name ip -p 8080:8080 -d -v ./GeoLite2-ASN.mmdb:/GeoLite2-ASN.mmdb -v ./GeoLite2-City.mmdb:/GeoLite2-City.mmdb -v ./GeoLite2-Country.mmdb:/GeoLite2-Country.mmdb --restart=unless-stopped mpolden/echoip -H "X-Real-IP" -a /GeoLite2-ASN.mmdb -c /GeoLite2-City.mmdb -f /GeoLite2-Country.mmdb
Enter fullscreen mode Exit fullscreen mode

I use -H "X-Real-IP" because I host it behind Nginx. FYI here is my Nginx configuration:

server {
    server_name ip.alvesdi.as;

    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_pass         http://127.0.0.1:8080;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/ip.alvesdi.as/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/ip.alvesdi.as/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = ip.alvesdi.as) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    server_name ip.alvesdi.as;
    listen 80;
    return 404; # managed by Certbot
}
Enter fullscreen mode Exit fullscreen mode

Voilà, if you did the same, you now have your own reliable IP service as well. Feel free to reuse it in all your scripts and Ansible playbook.


Photo by Fares Hamouche on Unsplash

Latest comments (0)