linuxbashshellubuntucentos

Methods to detect public IP address in bash


As part of an installation script, I want to make an "educated guess" about the machines public IP address. It should be as robust as possible, working on a variety of hosts and platforms. Here is an example:

https://gist.github.com/4669037

This script uses 3 methods and then defaults to the value of /etc/hostname. I have only tested this on Ubuntu so far. Will this be likely to work on other distributions? And are there additional methods that I could add?


Solution

  • curl ipinfo.io/ip
    

    Or

    wget -q -O - ipinfo.io/ip
    

    Or

    lynx -source ipinfo.io/ip
    

    get public ip address

    You can find other ip reporting websites instead of ipinfo.io as well. To name a few:

    Also, what is my ip shows more information about that ip.