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 : 126.96.36.199
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 !