What is my Public IP Address?

August 22nd, 2007 mysurface Posted in curl, egrep, grep, Misc, Text Manipulation, wget | Hits: 102534 | 10 Comments »

What is my public IP address? There is a lots of website can provide you the information. What if I wanna grep it for my script? We can use curl and grep for this purpose.

First, lets pick few websites that provide the service to reveal my public IP.
http://www.ipchicken.com/ – the chicken looks so funny.
http://whatismyip.com/ – a guy playing VR game.
http://myip.dk/ – a simple website but served the purpose.

Next, lets crawl the page and grep the IP

curl -s http://myip.dk/ | egrep -m1 -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'

You can use wget if you do not install curl

wget -qO - http://myip.dk/ | egrep -m1 -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'

The important part is the regex. [0-9] indicate any digit within 0 – 9 and {1,3} indicate it should appear minimum 1 time, maximum 3 times. -o indicate grep to print the matched part and -m1 is to limit the grep process to stop at first match.

http://whatismyip.org/ – no need to grep, just curl it.

curl -s http://whatismyip.org/

It seems that getting public IP from http://whatismyip.org/ is far more convenient, the limitation is that you are allow to access the page only 3 times within 10 minutes.

After all, I just enjoy greping with regex :)

[中文翻译]

10 Responses to “What is my Public IP Address?”

  1. Cool!

    This one is for the ‘visually impaired’ users:
    mpg123 “http://moanmyip.com/output/$(curl http://www.moanmyip.com | egrep -m1 -o ‘[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}’).mp3″

    Cheers. ;)

  2. myip.dk and ip-adress are the old ones dunno how i even remember that ;S

    check http://www.iplobster.com – cunning lobster hehe but i needed ipv4 info

  3. this is a great piece of work. Good job man and a great website

  4. [...] 我的公网IP是多少? curl -s http://myip.dk/ | egrep -m1 -o ‘[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}’ wget -qO – http://myip.dk/ | egrep -m1 -o ‘[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}’ curl -s http://whatismyip.org/ [...]

  5. this is a great piece of work. Good job man and a great website

  6. View your ip address and more:
    http://my-i-p.com

  7. Keep up the good work dude!

  8. oo yeah…this is the best article…tnx :X

  9. oo yeah…this is the best article…tnx :X

Leave a Reply