How to get the current external IP from Linux.

July 17th, 2010 by Claudiu Leave a reply »

Here’s my code snippet for getting the current external IP address.

Method 1 (using curl):

This one requires curl to be installed.


curl -s  http://checkip.dyndns.org | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'

Method 2 (using wget):


wget -q -O- http://checkip.dyndns.org | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'

I recommend the second because it should work on any system.

Of course you can replace Dyndns with any “IP get” page like http://whatismyip.com.

Advertisement

2 comments

  1. Great post! I see you started to like cURL :)

  2. voku1987 says:

    curl -L -s –max-time 10 http://checkip.dyndns.org | grep -Eo -m 1 ‘([[:digit:]]{1,3}.){3}[[:digit:]]{1,3}’

    lynx -dump http://www.formyip.com/ | awk “/IP is/” | awk {‘print $4′}

    wget -O – -q icanhazip.com

Leave a Reply