Find your public IP address easily with CLI

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:

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.

After that, you can just type “myip” in a Shell and you get your public IP address 🙂

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 !

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditShare on StumbleUponShare on TumblrPin on PinterestFlattr the authorDigg thisBuffer this pagePrint this page

2 comments

  1. 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) }’)

Leave a Reply

Your email address will not be published. Required fields are marked *