Author Topic: Monitor Your External IP Address - Plugin  (Read 573 times)

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2339
  • Karma: +32/-45
  • Life Moves Pretty Fast....
    • Node Central
Monitor Your External IP Address - Plugin
« on: August 08, 2017, 07:05:43 am »
Hi

I'm struggling with this, as it works with one local value I create one way, but not the other - yet both print the same value ?  And for some reason the wget request returns different (html language) information when doing string.sub, which is different to what it prints ?

What am I missing ?

Code: [Select]
-- http://lua-users.org/wiki/StringLibraryTutorial

-- Testing Trimming.
local mytext = "Current IP Address: 111.222.33.444 "
print (mytext)
print(string.sub(mytext, -15))
print(string.sub(mytext, 21))
print(" ")

-- Test with IP address
local status, ip = luup.inet.wget("http://checkip.dyndns.org")
print (ip)
print (string.sub(ip, -15))
print (string.sub(ip, 21))
« Last Edit: August 25, 2017, 07:57:05 pm by parkerc »

Offline akbooer

  • Master Member
  • *******
  • Posts: 5180
  • Karma: +223/-67
  • "Less is more"
Re: How to Trim leading characters from a retrieved value ?
« Reply #1 on: August 08, 2017, 07:19:52 am »
Well, the returned HTML contains lots of stuff other than the IP address.

It's really unwise to expect the required information to be in a fixed string position... suppose there's one more digit in the IP address, for example?

Try this, instead:

Code: [Select]
print (ip: match "%d+%.%d+%.%d+%.%d+")

...and don't forget to read up on Lua pattern matching operators!
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2339
  • Karma: +32/-45
  • Life Moves Pretty Fast....
    • Node Central
Re: How to Trim leading characters from a retrieved value ?
« Reply #2 on: August 08, 2017, 08:37:44 am »
Well, the returned HTML contains lots of stuff other than the IP address.

Do you know why the print request does not show all the HTML?

It's really unwise to expect the required information to be in a fixed string position... suppose there's one more digit in the IP address, for example?

Agreed, and I was struggling to learn a way how to surgically extract the IP,  it so all I could think of was to trim.

Try this, instead:

Code: [Select]
print (ip: match "%d+%.%d+%.%d+%.%d+")

...and don't forget to read up on Lua pattern matching operators!

Will certainly try, :) sadly I only get to dip in and out of this sort of thing, so my mind is always filled (gets clogged) up with so much other unrelated stuff ..

Offline akbooer

  • Master Member
  • *******
  • Posts: 5180
  • Karma: +223/-67
  • "Less is more"
Re: How to Trim leading characters from a retrieved value ?
« Reply #3 on: August 08, 2017, 12:26:17 pm »
Do you know why the print request does not show all the HTML?

What makes you think that?  I get:

Code: [Select]
<html><head><title>Current IP Check</title></head><body>Current IP Address: XX.XXX.XXX.XXX</body></html>


/body></html>


urrent IP Check</title></head><body>Current IP Address: XX.XXX.XXX.XXX</body></html>


The first line looks like the whole thing, the other two are truncated due to your string.sub() editing.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2339
  • Karma: +32/-45
  • Life Moves Pretty Fast....
    • Node Central
Re: How to Trim leading characters from a retrieved value ?
« Reply #4 on: August 08, 2017, 04:16:14 pm »
Interesting..

Then it must be a quirk in Rex's LuaTest tool (due to it being HTML/web content) because LuaTest returns the following - just my IP addesss via Print.. (which I've replaced for obvious reasons :)

Quote
LuaTest 1.5.2

Lua file: /nas/luatest/externalIP.lua

Results
No errors
Runtime: 128.5 ms
Code returned: nil

Print output
Current IP Address: 123.456.789.123     

123.456.789.123     

Code
   1   -- http://lua-users.org/wiki/StringLibraryTutorial
   2   
   3   local status, ip = luup.inet.wget("http://checkip.dyndns.org")
   4   print(ip)
   5   print (ip: match "%d+%.%d+%.%d+%.%d+")
« Last Edit: August 08, 2017, 04:20:15 pm by parkerc »

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2339
  • Karma: +32/-45
  • Life Moves Pretty Fast....
    • Node Central
I'm attempting to build a plugin to store the External IP as a variable, I've created the attached but have an issue with the code in the Implantation file. Lua test reports the following syntax error and I'm stumped?? I'm sure it's obvious to someone, but I'm not seeing it?

Quote
LuaTest 1.5.2

Lua file: /nas/luatest/externalip.lua

Results
Code error: Line 4: syntax error near 'luup'

Print output
(none)

Code
   1   function initialize (lul_device)
   2        local status, ip = luup.inet.wget("http://checkip.dyndns.org")
   3        local match (ip: match "%d+%.%d+%.%d+%.%d+")
   4        luup.variable_set("urn:nodecentral-com:serviceId:ExternalIP1","ExternalIP", match, lul_device)
   5             end
   6   initialize()
« Last Edit: August 10, 2017, 02:29:27 pm by parkerc »

Offline jswim788

  • Hero Member
  • *****
  • Posts: 508
  • Karma: +25/-2
Instead of this: local match (ip: match "%d+%.%d+%.%d+%.%d+") maybe you want this?
Code: [Select]
local myIP = ip: match "%d+%.%d+%.%d+%.%d+" and then use myIP in your variable_set?

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2339
  • Karma: +32/-45
  • Life Moves Pretty Fast....
    • Node Central
Instead of this: local match (ip: match "%d+%.%d+%.%d+%.%d+") maybe you want this?
Code: [Select]
local myIP = ip: match "%d+%.%d+%.%d+%.%d+" and then use myIP in your variable_set?

Thanks @jswim, that did it, but how ? Was the word 'match' doing ?

This has at least allowed the plugin to load, but the challenge now is to understand why it's not creating the new ExternalIP variable ? Any ideas ?

Latest version attached.

Offline jswim788

  • Hero Member
  • *****
  • Posts: 508
  • Karma: +25/-2
Need to use the variable myIP in the variable_set:
Code: [Select]
luup.variable_set("urn:nodecentral-com:serviceId:ExternalIP1","ExternalIP", myIP, lul_device)
Not sure if that being 'nil' would have caused the variable to not be created.  Anyway, if the above doesn't work, add some luup.log() lines and see what your log shows.

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2339
  • Karma: +32/-45
  • Life Moves Pretty Fast....
    • Node Central
That did it :) Thanks so much, its those little things that still seem to trip me up.

Next on the list is to try and tweak things a bit, and see how I can make the IP visible on the front of the device on the UI too.

Offline akbooer

  • Master Member
  • *******
  • Posts: 5180
  • Karma: +223/-67
  • "Less is more"
I'd strongly suggest you use a syntax-aware editor to catch such things.

ZeroBraneStudio is just ideal for this.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2339
  • Karma: +32/-45
  • Life Moves Pretty Fast....
    • Node Central
Re: Monitor Your External IP Address - Plugin
« Reply #11 on: August 25, 2017, 07:56:22 pm »
Work continues (when I have time) on creating an external IP monitoring plugin.

Here is the latest Implementation file. (as always comment/recommendations welcome)

Code: [Select]
<?xml version="1.0" encoding="UTF-8"?>
<implementation>
<settings>
<protocol>crlf</protocol>
<handleChildren>1</handleChildren>
</settings>
<functions>
function initialize (lul_device)
local status, ip = luup.inet.wget("http://checkip.dyndns.org")
local newIP = ip: match "%d+%.%d+%.%d+%.%d+"
local existingIP = luup.variable_get("urn:nodecentral-com:serviceId:ExternalIP1","ExternalIP", 376)
local oldIP = luup.variable_get("urn:nodecentral-com:serviceId:ExternalIP1","OldIP", 376)

print("My new IP is : '"..newIP.."'")
luup.log("My new IP is : '"..newIP.."'")
print("My existing IP is : '"..existingIP.."'")
luup.log("My existing IP is : '"..existingIP.."'")
print("My old IP is : '"..oldIP.."'")
luup.log("My old IP was : '"..oldIP.."'")

-- if (oldIP == nil or '')
-- then luup.variable_set("urn:nodecentral-com:serviceId:ExternalIP1","OldIP", "000.000.000.000", 376)
-- luup.log("No previous IP is present so adding a temporary one")
-- print ("No previous IP is present so adding a temporary one")
if (newIP == existingIP)
then luup.log("External IP has not changed, so nothing to do")
print("External IP has not changed, so nothing to do")
if (newIP ~= existingIP)
then
luup.log("External IP has changed")
print("External IP has changed")
luup.variable_set("urn:nodecentral-com:serviceId:ExternalIP1","OldIP", oldIP, 376)
luup.variable_set("urn:nodecentral-com:serviceId:ExternalIP1","ExternalIP", newIP, 376)
luup.variable_set("urn:micasaverde-com:serviceId:HaDevice1", "LastUpdate", os.time(), 376)
-- print("Update made  : '"..oldIP.."'")
-- luup.log("Update made  : '"..oldIP.."'")
print("External IP has changed, so update made")
luup.log("External IP has changed, so update made")
--end
end
end
end

initialize()
</functions>
<startup>initialize</startup>
</implementation>


Work continues on this on the plugin thread..

http://forum.micasaverde.com/index.php/topic,50487.0.html
« Last Edit: September 03, 2017, 06:15:07 pm by parkerc »

Offline BulldogLowell

  • Hero Member
  • *****
  • Posts: 1576
  • Karma: +191/-85
Re: Monitor Your External IP Address - Plugin
« Reply #12 on: September 12, 2017, 03:55:05 pm »
you pass the device here...
Code: [Select]
function initialize (lul_device)

but yet you've hard coded the device into your function here:
Code: [Select]
local existingIP = luup.variable_get("urn:nodecentral-com:serviceId:ExternalIP1","ExternalIP", 376)
local oldIP = luup.variable_get("urn:nodecentral-com:serviceId:ExternalIP1","OldIP", 376)

try:
Code: [Select]
local existingIP = luup.variable_get("urn:nodecentral-com:serviceId:ExternalIP1","ExternalIP", lul_device)
why do you log non-events?
Code: [Select]
if (newIP == existingIP)
  then luup.log("External IP has not changed, so nothing to do")
  print("External IP has not changed, so nothing to do")

just look for the state change and act/log accordingly.

did you know that luup.variable.get actually returns two values?

Code: [Select]
local existingIP, lastUpdateTime = luup.variable_get("urn:nodecentral-com:serviceId:ExternalIP1","ExternalIP", 376)
you can use that, and you wouldn't need the other variable:
Code: [Select]
luup.variable_set("urn:micasaverde-com:serviceId:HaDevice1", "LastUpdate", os.time(), 376)because you already have it!

make it easier on yourself during debug:
Code: [Select]
luup.log("External IP has changed, so update made")

Code: [Select]
function logToVera(mssg)
  luup.log("MyPluginName: "..mssg)
end

logToVera("External IP has changed, so update made")

and
Code: [Select]
tail -f LuaUPnP.log | grep MyPluginName:
props to @akbooer for teaching me some of this!

EDIT:

Code: [Select]
if (newIP == existingIP)
then the only alternative is they don't match!  So:
Code: [Select]
if (newIP == existingIP) then
  luup.log("External IP has not changed, so nothing to do")
  print("External IP has not changed, so nothing to do")
else
  -- the other thing
end

and look to beautify your code for proper indentation:
Code: [Select]
function initialize (lul_device)
  local status, ip = luup.inet.wget("http://checkip.dyndns.org")
  local newIP = ip: match "%d+%.%d+%.%d+%.%d+"
  local existingIP = luup.variable_get("urn:nodecentral-com:serviceId:ExternalIP1","ExternalIP", 376)
  local oldIP = luup.variable_get("urn:nodecentral-com:serviceId:ExternalIP1","OldIP", 376)

  print("My new IP is : '"..newIP.."'")
  luup.log("My new IP is : '"..newIP.."'")
  print("My existing IP is : '"..existingIP.."'")
  luup.log("My existing IP is : '"..existingIP.."'")
  print("My old IP is : '"..oldIP.."'")
  luup.log("My old IP was : '"..oldIP.."'")
  --[[
  if (oldIP == nil or '') then
    luup.variable_set("urn:nodecentral-com:serviceId:ExternalIP1","OldIP", "000.000.000.000", 376)
    luup.log("No previous IP is present so adding a temporary one")
    print ("No previous IP is present so adding a temporary one")
 
  --]]
 if (newIP == existingIP)
  then luup.log("External IP has not changed, so nothing to do")
  print("External IP has not changed, so nothing to do")
else
    luup.log("External IP has changed")
    print("External IP has changed")
    luup.variable_set("urn:nodecentral-com:serviceId:ExternalIP1","OldIP", oldIP, 376)
    luup.variable_set("urn:nodecentral-com:serviceId:ExternalIP1","ExternalIP", newIP, 376)
    luup.variable_set("urn:micasaverde-com:serviceId:HaDevice1", "LastUpdate", os.time(), 376)
    -- print("Update made  : '"..oldIP.."'")
    -- luup.log("Update made  : '"..oldIP.."'")
    print("External IP has changed, so update made")
    luup.log("External IP has changed, so update made")
    --end
  end
end
end

note how to comment a block of code...
Code: [Select]
--[[
  comments here
  and here
  are ignored
--]]



« Last Edit: September 12, 2017, 04:37:29 pm by BulldogLowell »

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2339
  • Karma: +32/-45
  • Life Moves Pretty Fast....
    • Node Central
Re: Monitor Your External IP Address - Plugin
« Reply #13 on: September 18, 2017, 02:42:02 pm »
Thanks @bulldogLowell - I'll aim to factor in what I can from your suggestions in the other thread for this plugin idea.

Work continues - http://forum.micasaverde.com/index.php/topic,50487.0.html

Offline BulldogLowell

  • Hero Member
  • *****
  • Posts: 1576
  • Karma: +191/-85
Re: Monitor Your External IP Address - Plugin
« Reply #14 on: September 19, 2017, 06:40:14 pm »
Thanks @bulldogLowell - I'll aim to factor in what I can from your suggestions in the other thread for this plugin idea.

Work continues - http://forum.micasaverde.com/index.php/topic,50487.0.html

Thanks  ;)

I have a static IP so I presently don't have a need for your plugin, but looking at all the implementation files certainly gives me ideas regarding my[url =http://forum.micasaverde.com/index.php/topic,50750.0.html] 3G Bridge/Plugin[/url].

My main issues were really:
1) lua, which I finally have a handle on.  Though I've messed about with it with various scripting in the past, it took me a while to really understand the concept of a table.  Coming from C/C++ it is a little wicked to imagine returning multiple values and lua's sort-of loose typing.  It seems . lot more like Javascript in that respect.  Now that I've worked with it, I can really see the power of it.  I still cannot for the life of me figure out how it evolved from C!!!
2) figuring out the relationship between all of the plugin files... but once I "got it" well, I got it!

The experienced forum participants are a big help, as I can see for you too...