Skip to content

Instantly share code, notes, and snippets.

@notsle
Last active January 3, 2024 06:24
Show Gist options
  • Save notsle/a5561aad60ecfc272d8e to your computer and use it in GitHub Desktop.
Save notsle/a5561aad60ecfc272d8e to your computer and use it in GitHub Desktop.
Get Public IP address with curl and ipchicken.com
curl -s https://ipchicken.com | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'
@thisdavej
Copy link

Thanks for your script as it provides a nice example of using regular expressions to extract info from HTML. I had to do a curl -sL http://ipchicken.com to follow redirects to make it work and then I realized that ipchicken.com is now using https . The updated script would thus be:

curl -s https://ipchicken.com | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'

@notsle
Copy link
Author

notsle commented Dec 1, 2019

Thanks. I updated my original with https

@thisdavej
Copy link

Perfect!

@wagnar-woofbrok
Copy link

This is great, thank you!

@thisdavej
Copy link

This gist is cool because it demonstrates some simple web scraping that can be accomplished using egrep. In practice, I would probably use the following if I were seeking to fetch my public IP address in a shell script:

# show public IP
curl -s ifconfig.me

# use inside a variable
my_ip=$(curl -s ifconfig.me)
echo "My public IP address: $my_ip"

@manipulator01
Copy link

if you don't have egrep installed or don't wanna deal with grep in general, you can do this;

curl ipaddress.ai

wget can also be used;

wget -qO- ipaddress.ai

or visit ipaddress.ai

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment