Hello everybody,
In this post, I present you a small and useful bash script that can be used on Linux/Unix/OSX systems. By working in IT, I often need to know what is my public IP address. Everytime, I must use Google and write something like this: “whatsmyip“… I recently had the idea to write my own bash script. This script is very simple:
#!/bin/bash PUBLIC_IP=`wget http://ipecho.net/plain -O - -q ; echo` echo "My Public IP address is :" $PUBLIC_IP
Save this script in a file named, for example “myip” and move it into a directory presents in the PATH. After that, don’t forget to make it executable.
$ echo $PATH $ /usr/local/sbin:/usr/local/bin:/usr/bin:/sbin:/bin $ $ sudo cp myip /usr/local/sbin $ sudo chmod +x /usr/local/sbin/myip
After that, you can just type “myip” in a Shell and you get your public IP address 🙂
$ myip My Public IP address is : 84.222.33.12
This script is very simple and can be customised of course. We can imagine to check more than one web site to add redundancy and use another website that is able to return the IPv6 version of the public IP address if available. Don’t hesitate to post your improvements !
Instead of relying to an http web service, you can use a DNS resolver which is more reliable, like:
PUBLIC_IP=$(dig +short myip.opendns.com @resolver1.opendns.com)
I use the next two lines in a bash application (LiveWB) to get the country:
EXTIPADDR=$(dig +short myip.opendns.com @resolver1.opendns.com)
COUNTRY=$(whois $EXTIPADDR | awk -F’:[ \t]+’ ‘tolower($1) ~ /^country$/ { print tolower($2) }’)
Hello Vangelis and thank you for you awesome answer ! I will try to include your solution on my script 🙂 I will post the update soon