Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to find my external IP address from a bash script. At the moment I use this function:

myip () { 
    lwp-request -o text checkip.dyndns.org | awk '{ print $NF }'
}

But it depends on perl-libwww, perl-html-format, perl-html-tree installed. What other ways can I get my external IP?

share|improve this question
3  
What do you mean by the external IP? The two answers so far use HTTP. The response may end up being the IP of your ISP's proxy server. (Which may be what you want.) – billpg Oct 14 '11 at 17:21
@billpg: I mean the IP of the NAT router – eugene y Oct 14 '11 at 20:09
Then you'll need a what-is-my-IP web service that uses HTTPS. Alas, I don't know of any. – billpg Oct 14 '11 at 22:22
@billpg ipcheckit.com – Gilles Oct 14 '11 at 22:48

10 Answers

up vote 11 down vote accepted

Since whatsmyip.org and ifconfig.me have already been mentioned:

curl -s icanhazip.com
share|improve this answer
1  
Another one: ip.appspot.com or ip.appspot.com – Lekensteyn Nov 9 '11 at 8:31
These two support IPv6. – Josh Lee Oct 8 '12 at 17:31

curl -s http://whatismyip.org/

share|improve this answer
3  
+1 I knew about whatismyip.com, but not whatismyip.org, that's awesome. – Julian Oct 14 '11 at 15:15
Never knew about that one! Great site! – IDWMaster Oct 14 '11 at 22:48
2  
@MaciekSawicki Is the -s option really necessary in this case? I tried with/without it in my fedora 15 - bash 4.2.10(1) and it worked in both ways. – ztank1013 Oct 15 '11 at 14:05
1  
Doesn't seem to work anymore via CLI, but going to the webpage from a web browser works. I'm using openSUSE 12.1 x64. – SaultDon Aug 16 '12 at 16:13
3  
whatismyip.com has removed the free service to check external IP. So, I'm afraid this is no longer correct. icanhazip.com still works. – daSong Jan 17 at 10:20
wget -O - -q http://whatismyip.org/
share|improve this answer
I was not able to get my IP like this, just gives it as:<img src='ipimg.php'/> – Yuugian Apr 4 at 13:38

You can use ifconfig.me as alternative to whatismyip.org.

curl -s http://ifconfig.me

Also ifconfig.me has some additional functional. To find out what else information you can receive visit the website.

share|improve this answer
netcat icanhazip.com 80 <<< $'GET / HTTP/1.0\nHost: icanhazip.com\n\n' | tail -n1
share|improve this answer
Blank output here, although the site works. Any idea why? I am behind a proxy, if that's relevant, but wget icanhazip.com works. – l0b0 Apr 4 at 11:23
@l0b0 Try to omit the | tail -n1 part and see what you get from the proxy – eugene y Apr 4 at 13:18
Nothing, just exit code 1. Ditto for netcat icanhazip.com 80. Looks like it ignores $http_proxy and friends, because specifying the proxy and port with -x just resulted in a hanging process. – l0b0 Apr 4 at 14:05

If you want to use HTTPS to avoid some potential pitfalls:

_result=$(wget -qO- https://ipcheckit.com/)
_result="${_result##*Your IP address is<br><b>}"
printf '%s\n' "${_result%%</b></p>*}"
share|improve this answer
Perhaps you could elaborate on what the potential pitfalls are that you are avoiding here? – Caleb Oct 17 '12 at 6:27
The domain ipcheckit.com apparently is for sale and not hosts the IP address displaying service anymore. – manatwork Jan 17 at 11:59

Here is another alternative that depends on hosts who's business resolves around managing dynamic IP rather that "public service" sites that may go away or change format.

1) Register your server at one of the many free dynamic dns services (e.g. no-ip.com) This will give you a DNS entry like xxx.no-ip.org.

2) Install the service's dynamic update tool (reports IP changes to service).

To get the IP address in a script, just do:

$external_ip = `dig +short xxx.no-ip.org`

Great for use in cron job to check if dynamic IP has changed and some configuration entries need to be changed.

share|improve this answer
The question is tagged bash. As your code is not valid in bash, please specify what else is it. – manatwork Mar 20 at 18:26

You also may use for this:

curl -s getip.tk

or

wget -O - -q getip.tk

Also it's can print User-Agent: getip.tk/agent or force print IP (for browsers): getip.tk/ip

share|improve this answer

I prefer to use this:

curl curlmyip.com

It's short and simple to remember.

share|improve this answer

Another way to view your external IP address is as follows:

curl -s http://www.ip-details.com | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"
share|improve this answer
Please detail what this does, the site mentioned might go away at any time. – vonbrand Apr 4 at 12:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.