Wednesday 5 February 2020

networking - Simplest way to check for dynamic IP change


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.



Answer



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.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...