Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What would be the easiest way to find, with a script, if my IP address has changed?


Background info:

I have a standard home cable modem connexion, and my ISP assigns me a dynamic IP. It changes every now and then, the DHCP lease times seems irregular.

Basically, I'm doing my own dynamic DNS thing, and can update the DNS records pointing to my home server with a click. The only thing remaining is to detect an IP change so I can fully automate the process.

Now there are several websites here which can return your IP address, and I could use a cron job to wget one of these pages and grep the IP address from it... But it seems overkill in terms of CPU / bandwidth usage, and it's not very KISS.

Am I missing some simple and elegant way to retrieve my public IP and check if it has changed?

Extra info: my modem/router is a cheap brick, it does nothing very interesting in that regard. My server runs with Centos 6.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Assuming you're behind NAT, the most elegant way I can think of is to run a cron job on the router itself, periodically checking ifconfig too check its current WAN address. This may even be possible on a 'cheap brick' if you are able to install custom firmware. However elegant, hardly simple.

As discussed here, on Windows, but the argument is equally valid for Centos, in polling the WAN IP from behind NAT, you run into the problem that you don't technically have an external address. Instead, you are routed through one at the router's discretion. Obviously, you would still have access to this information as long as the router is under your control, but there is no universal way of communicating this IP address to hosts on the local network. As a result, your server cannot simply ask the router for the external IP it will be using.

The most efficient alternative would be to just let the router do its magic and actually make the connection to an external server, then asking it to return the address the request originated from. A lot of webservers have been set up specifically for this purpose. For scripting, I can recommend http://icanhazip.com/, as it requires no further parsing on your part (only the IP is returned in plain-text).

If necessary, the answers to the question linked to before ought to provide plenty of tips on implementation.

share|improve this answer
    
Good ideas and links. +1, if only for the icanhazip website that I did not know, and makes the downloading of extra HTML and parsing unnecessary. –  Silver Quettier Mar 6 '13 at 14:54
    
Or checkip.net-me.net/plain –  Alex Mar 6 '13 at 15:44

You can query any website that displays your IP for this task. Using checkip.dyndns.com, since it it text.only.

Example:

wget -q -O - checkip.dyndns.com | grep -Po "[\d\.]+"
share|improve this answer

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.