Get a Notification When Your WAN IP Changes
Home residential internet connections have dynamic public IP addresses that can change whenever the DHCP lease expires. But from my experience, I’ve had the same IP for months, so I was curious when they actually do change and if there is anything you can do anything to trigger a change (i.e. power cycling)?
I started coding up a PHP wan-ip-checker tool and started dreading the idea of creating the CRON job to manage it. And then I remembered the server I plan to put this on is a raspberry pi that is already running Node-Red to turn on and off a series of WIFI LED controllers in my apartment.
After a few minute I had this flow:
- A 24 hour trigger starts the flow at 1AM every morning.
- Reads in a plain text file that has just an ip address saved in it and stores it to msg.previous-ip
- GET request from ipv4bot.whatismyipaddress.com and stores it to msg.current-ip
- A switch node (check if ip changed) then compares msg.previous != msg.current-ip and if so, it then updates the text file with the new ip, adds a log row of the new ip with timestamp, and sends an email to me about the change.
In a while I will post how long my IP stays the same and if restarting your modem can/does change your WAN IP.
You can copy the code below and Import > Clipboard in Node-Red:
[{"id":"97c716c0.5e6798","type":"file","z":"985ead72.cdb57","name":"","filename":"/var/www/html/wan-ip-checker/latest-ip","appendNewline":false,"createDir":false,"overwriteFile":"true","x":810.5,"y":457,"wires":[]},{"id":"448d788f.69a2c8","type":"inject","z":"985ead72.cdb57","name":"Every 24h","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 1 * * *","once":false,"x":130.5,"y":102,"wires":[["3ce3b5da.7fd7fa"]]},{"id":"3ce3b5da.7fd7fa","type":"file in","z":"985ead72.cdb57","name":"read previous-ip","filename":"/var/www/html/wan-ip-checker/latest-ip","format":"utf8","x":285.5,"y":173,"wires":[["52978d82.6248a4"]]},{"id":"433f5d83.5d64f4","type":"switch","z":"985ead72.cdb57","name":"check if ip changed","property":"current-ip","propertyType":"msg","rules":[{"t":"neq","v":"previous-ip","vt":"msg"}],"checkall":"true","outputs":1,"x":419.5,"y":459,"wires":[["97c716c0.5e6798","4e7ab8bd.db98e8"]]},{"id":"86a60c4b.00b86","type":"debug","z":"985ead72.cdb57","name":"debug logging","active":true,"console":"false","complete":"true","x":897.5,"y":187,"wires":[]},{"id":"4248c529.6a6a1c","type":"http request","z":"985ead72.cdb57","name":"GET current-ip","method":"GET","ret":"txt","url":"ipv4bot.whatismyipaddress.com","tls":"","x":572.5,"y":240,"wires":[["d7ef2df6.1ef7f"]]},{"id":"d7ef2df6.1ef7f","type":"change","z":"985ead72.cdb57","name":"set","rules":[{"t":"set","p":"current-ip","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":717.5,"y":239,"wires":[["433f5d83.5d64f4","86a60c4b.00b86"]]},{"id":"52978d82.6248a4","type":"change","z":"985ead72.cdb57","name":"set","rules":[{"t":"move","p":"payload","pt":"msg","to":"previous-ip","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430.5,"y":173,"wires":[["4248c529.6a6a1c"]]},{"id":"6399d3f8.7b6d1c","type":"e-mail","z":"985ead72.cdb57","server":"smtp.gmail.com","port":"465","name":"","dname":"","x":732.5,"y":629,"wires":[]},{"id":"d168fb2e.381928","type":"file","z":"985ead72.cdb57","name":"","filename":"/var/www/html/wan-ip-checker/ip.log","appendNewline":true,"createDir":false,"overwriteFile":"false","x":792.5,"y":588,"wires":[]},{"id":"4e7ab8bd.db98e8","type":"template","z":"985ead72.cdb57","name":"template","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload}} first seen {{headers.date}}","x":501.5,"y":587,"wires":[["d168fb2e.381928","6399d3f8.7b6d1c"]]}]