WAN IP Checker: Difference between revisions

From Domoticz
Jump to navigation Jump to search
Walter vl (talk | contribs)
mNo edit summary
Walter vl (talk | contribs)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:


== Why do you need one? ==
== 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.
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.
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.  
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. 


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.
There is a simple way to solve this: Install a small dzVents script on your system, that will automatically run periodically.
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.
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 ==
The script below was tested on Raspberry Pi, running Jesse.


<syntaxhighlight lang="lua">
'''Instructions'''  
-- 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)
[[Dummy for virtual Switches|Create a Dummy text device]] named ' Wan IP' and copy and paste the dzvents script in the internal [[EventSystem|event editor]] as a dzvents script.
        -- 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:


'''The Script'''<syntaxhighlight lang="lua" line="1">
local getIp    = 'https://api.ipify.org/?format=json'
local getIp    = 'https://api.ipify.org/?format=json'
return {
return {
  on = {
  on = {
      timer = { 'every 6 hours' },
    timer = { 'every 6 hours' },
      httpResponses = { 'wanIP' }
    httpResponses = { 'wanIP' }
},
},
 
logging = {
  logging = {
    level = domoticz.LOG_FORCE,
      level = domoticz.LOG_FORCE,
    marker = '[WAN IP]'
      marker = '[WAN IP]'
      },
        },
  execute = function(dz, devNil)
 
    local actIP = dz.devices('Wan IP').text  
  execute = function(dz, devNil)
    local newIP = 'Unable to retrieve IP'
      local actIP = dz.devices('Wan IP').text  
 
      local newIP = 'Unable to retrieve IP'
    if (devNil.isTimer)  then
   
        dz.log("Send WanIP request")
      if (devNil.isTimer)  then
        dz.openURL({
        dz.log("Send WanIP request")
          url = getIp,
        dz.openURL({
          method = 'GET',
            url = getIp,
          callback = 'wanIP'
            method = 'GET',
          })
            callback = 'wanIP'
    elseif (devNil.isHTTPResponse) then
            })
        dz.log("WanIP response")
      elseif (devNil.isHTTPResponse) then
        if (devNil.ok) then -- statusCode == 2xx
        dz.log("WanIP response")
          newIP = devNil.json.ip
        if (devNil.ok) then -- statusCode == 2xx
        end  
            newIP = devNil.json.ip
        if newIP ~= actIP then  
        end  
          dz.devices('Wan IP').updateText(newIP)
        if newIP ~= actIP then  
        end  
            dz.devices('Wan IP').updateText(newIP)
    end
        end  
  end
      end
  end
}
}
</syntaxhighlight>

Latest revision as of 22:38, 10 December 2024

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.

Instructions

Create a Dummy text device named ' Wan IP' and copy and paste the dzvents script in the internal event editor as a dzvents script.


The Script

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
}