Author Topic: Retrieve External IP - How to Trim leading characters from a retrieved value ?  (Read 229 times)

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2244
  • Karma: +30/-39
  • Life Moves Pretty Fast....
    • Node Central
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 10, 2017, 02:01:16 pm by parkerc »

Offline akbooer

  • Master Member
  • *******
  • Posts: 5032
  • Karma: +216/-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: 2244
  • Karma: +30/-39
  • 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: 5032
  • Karma: +216/-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: 2244
  • Karma: +30/-39
  • 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: 2244
  • Karma: +30/-39
  • 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

  • Sr. Member
  • ****
  • Posts: 431
  • Karma: +17/-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: 2244
  • Karma: +30/-39
  • 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

  • Sr. Member
  • ****
  • Posts: 431
  • Karma: +17/-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: 2244
  • Karma: +30/-39
  • 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: 5032
  • Karma: +216/-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.