Getting your IP from anywhere

Curious software engineer
Search for a command to run...

Curious software engineer
No comments yet. Be the first to comment.
In this series, I will treat subjects related to software development, software engineering, and related career.
Let's say you are doing freelance work for a client who wants to launch a blog. While Hashnode is great, it's probably not adapted in every case. Especially for non-developers and if you need high customization also. Here I propose to deploy a blog u...
It’s the summer holidays, after all. So I wanted to spent some time with my 7 years old daughter to teach her programming. ⌨️ I spent some time before discussing with her some of the basics concept verbally: the different parts of a computer, and bas...

This article is a follow up on my previous one introducing the concept of SE Spectrum: https://sonny.alvesdi.as/a-better-way-to-discuss Existing solutions For long, the easiest way to run a spectrum was to use visual drawing or diagram tools like ...

Introduction to SE Spectrum

One of the greatest tool available on GitHub is Dependabot. For those not familiar with it, in short it's a bot reviewing your list of dependencies and proposing PRs to update them. https://docs.github.com/en/code-security/getting-started/dependabot-...

Photo by Harli Marten on Unsplash As long as I remember I have always been interested by skepticism. And I think one element that really resonated with me has been that conference talk by Phil Plait: https://www.youtube.com/watch?v=FrFRbGjUtJk 15 ...

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
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
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
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
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
}
You can notice I use Certbot, if you don't know about Certbot, check my article:
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