DEV Community

Cover image for Retrieve your external IP using dig and bash.
Rafael Pérez
Rafael Pérez

Posted on

Retrieve your external IP using dig and bash.

Need your external I.P. address but are tired of going to [insert what's my i.p. website here]?, or are on your way to automate something related in bash?, here's a recipe that permits you to do just that:

In your bin directory, create a file and call it "myip".

/bin/touch myip
Enter fullscreen mode Exit fullscreen mode

Edit the file with your favorite text editor.

/bin/nano myip
Enter fullscreen mode Exit fullscreen mode

Insert the following instruction.

dig +short myip.opendns.com @resolver1.opendns.com
Enter fullscreen mode Exit fullscreen mode

If you don't have dig installed, it's about time you did, it's an excellent network diagnostics tool.

sudo apt install dig
Enter fullscreen mode Exit fullscreen mode

Apply necessary permissions.

/bin/chmod +rwx myip
Enter fullscreen mode Exit fullscreen mode

If your bin directory is in your path environment variable like it already should, then you can just type "myip" from just anywhere and obtain your (IPV4) external address.

you@YOUR-COMPUTER:~$ myip
42.42.42.42
you@YOUR-COMPUTER:~$

Top comments (0)