Simplest way to check for dynamic IP change

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.


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\.]+"