WAN IP Checker

From Domoticz
Revision as of 19:13, 9 December 2024 by Walter vl (talk | contribs)
Jump to navigation Jump to search

Why do you need one?

Lots of people have experienced this: you are away from home, on a business trip or holidays, and your IP provider has given you a new external IP address. Lots of providers use dynamic IP, a static address is hard to come by. Unfortunately they don't tell you this in any way, but you find out the hard way: you can't get into your systems from remote. There are ways around this, for instance by using a DynDNS service. But if the only thing you want is to VPN into your network, this is overkill. There is a simple way to solve this. Install a small dzVents script on your system, that will automatically run periodically. It will retrieve the external IP address and compare that to the address that was stored in the previous run. If they differ. it will notify you and give you the new IP address.

The script below was tested on Raspberry Pi, running Jesse.

All you need to do is change the name of your location and store the script in the directory domoticz\scripts\dzVents\scripts. That's it. By default it will run every four hours. On the first run, it will discover that the text device used to "remember" the IP address does not exist. It will then create this device. In the next run, it will compare the actual IP address with the value in the text device (which will be something like "Hello World"), and find that it differs. You will be notified. Afyer this you will not see anything happening unless the external IP changes.

Things to remember: make sure the notification scheme in Domoticz has been set up and tested. Also make sure the access to Domoticz is open from 127.0.0.1. If you don't use a userid/pw protection, you should be OK. If you do, make sure 127.0.0.1 is in the "Networks" field on the settings page.

The script

<syntaxhighlight lang="lua"> -- Based upon a script written by Emme (Milano, Italy) -- Slightly modified and translated by Manjh -- -- Store the script as GetWANIP.lua in domoticz/scripts/dzVents/scripts -- -- Script will: -- 1. retrieve current WN IP address -- 2. compare to previous IP -- 3. send warning msg if different -- -- runs every four hours, but interval can be changed -- -- during first run, will create text device. -- Second run: will detect WAN IP for the first time --

return { on = { timer = { 'every 4 hours' } -- ***** interval },

logging = {

           level = domoticz.LOG_FORCE,
           marker = '[WAN IP Checker]'
   },
   execute = function(dz, devNil)
       -- Change following field into your location
       -- will be used to construct name of text variable, and also
       -- in the subject of the warning e-mail when WAN IP address changes
       local location = 'Utrecht'   
       
       local dzb      = dz.LOG_FORCE
       local getIp    = 'curl -s https://4.ifcfg.me/' -- URL of website to echo my WAN IP
       local tmpFile  = '/home/pi/domoticz/scripts/wanip.txt' -- location where to store the temp file
       local actIp    = 
       local dev_name = location..'_WAN_IP_address'      
       if dz.devices(dev_name) == nil then 
           dz.log("Creating device: "..dev_name, dzb)
           dz.openURL('127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=1&sensorname='..dev_name..'&sensortype=5')  
       else        
           local devIp    = dz.devices(dev_name) 

local currIp = devIp.text -- get current WAN IP and store in a temp file

           os.execute(getIp..' > '..tmpFile)
           actIp = io.open(tmpFile):read()
           if actIp == nil then 
               actIp = 'Cannot retrieve WAN IP' 
           end         
           if actIp ~= currIp then
               msgTxt = 'WAN IP has changed: '..currIp..' ==> '..actIp
               dz.log(msgTxt, dzb)
               dz.notify('WAN IP change warning '..location, msgTxt, dz.PRIORITY_EMERGENCY)
               devIp.updateText(actIp)
               os.execute('rm '..tmpFile)
           else 
               dz.log('No change in WAN IP', dzb)
           end
       end 
   end

} </syntaxhighlight lang="lua">

UPDATE Looks like link no longer works... in the meantime dzVents evolved and now include async http requests so the script has beed updated:

local getIp = 'https://api.ipify.org/?format=json'

return {

  on = {
     timer = { 'every 6 hours' },
     httpResponses = { 'wanIP' }

},

  logging = {
     level = domoticz.LOG_FORCE,
     marker = '[WAN IP]'
       },
  execute = function(dz, devNil)
     local actIP = dz.devices('Wan IP').text 
     local newIP = 'Unable to retrieve IP'
   
     if (devNil.isTimer)  then
        dz.log("Send WanIP request")
        dz.openURL({
           url = getIp,
           method = 'GET',
           callback = 'wanIP'
           })
     elseif (devNil.isHTTPResponse) then
        dz.log("WanIP response")
        if (devNil.ok) then -- statusCode == 2xx
           newIP = devNil.json.ip
        end 
        if newIP ~= actIP then 
           dz.devices('Wan IP').updateText(newIP)
        end 
     end
  end

}