Monitor Your External IP Address - Plugin

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]-- lua-users wiki: String Library Tutorial

– 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))[/code]

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:

print (ip: match "%d+%.%d+%.%d+%.%d+")

…and don’t forget to read up on Lua pattern matching operators!

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

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.

[quote=“akbooer, post:2, topic:196914”]Try this, instead:

print (ip: match "%d+%.%d+%.%d+%.%d+")

…and don’t forget to read up on Lua pattern matching operators![/quote]

Will certainly try, :slight_smile: 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 …

What makes you think that? I get:

<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.

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 :slight_smile:

[b][u]LuaTest 1.5.2[/u][/b]

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 – lua-users wiki: String Library Tutorial
2
3 local status, ip = luup.inet.wget(“http://checkip.dyndns.org”)
4 print(ip)
5 print (ip: match “%d+%.%d+%.%d+%.%d+”)

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?

[b]LuaTest 1.5.2[/b]

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()

Instead of this: local match (ip: match “%d+%.%d+%.%d+%.%d+”) maybe you want this?

local myIP = ip: match "%d+%.%d+%.%d+%.%d+" and then use myIP in your variable_set?

[quote=“jswim788, post:7, topic:196914”]Instead of this: local match (ip: match “%d+%.%d+%.%d+%.%d+”) maybe you want this?

local myIP = ip: match "%d+%.%d+%.%d+%.%d+" 

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.

Need to use the variable myIP in the variable_set:

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.

That did it :slight_smile: 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.

I’d strongly suggest you use a syntax-aware editor to catch such things.

ZeroBraneStudio is just ideal for this.

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]<?xml version="1.0" encoding="UTF-8"?>


crlf
1


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()

initialize
[/code]

Work continues on this on the plugin thread…

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

you pass the device here…

function initialize (lul_device)

but yet you’ve hard coded the device into your function here:

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:

local existingIP = luup.variable_get("urn:nodecentral-com:serviceId:ExternalIP1","ExternalIP", lul_device)

why do you log non-events?

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?

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:

luup.variable_set("urn:micasaverde-com:serviceId:HaDevice1", "LastUpdate", os.time(), 376)
because you already have it!

make it easier on yourself during debug:

luup.log("External IP has changed, so update made")
function logToVera(mssg)
  luup.log("MyPluginName: "..mssg)
end

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

and

tail -f LuaUPnP.log | grep MyPluginName:

props to @akbooer for teaching me some of this!

EDIT:

if (newIP == existingIP)

then the only alternative is they don’t match! So:

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]
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[/code]

note how to comment a block of code…

--[[ comments here and here are ignored --]]

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

[quote=“parkerc, post:14, topic:196914”]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[/quote]

Thanks :wink:

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 3G Bridge/Plugin.

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…

1 Like
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!!!

It didn’t. However, it IS written in standard C, to be totally transportable.

See here: The evolution of an extension language: a history of Lua

BTW, it’s not ‘lua’ but ‘Lua’. On the Lua website, it says:

What's in a name?

“Lua” (pronounced LOO-ah) means “Moon” in Portuguese. As such, it is neither an acronym nor an abbreviation, but a noun. More specifically, “Lua” is a name, the name of the Earth’s moon and the name of the language. Like most names, it should be written in lower case with an initial capital, that is, “Lua”. Please do not write it as “LUA”, which is both ugly and confusing, because then it becomes an acronym with different meanings for different people. So, please, write “Lua” right!

so, I’m not much of a capitalist… :wink: