Author Topic: Telnet session with Lua  (Read 9549 times)

Offline Djoss

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
Telnet session with Lua
« on: September 28, 2009, 09:00:16 pm »
Is it possible to establish a telnet session with a remote device using Lua?  Are there any existing scripts doing this to take as an example?

Thanks!

Offline Vera1234

  • Sr. Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
Re: Telnet session with Lua
« Reply #1 on: May 05, 2012, 03:20:36 am »
Did you ever got an answer to this? I have the same question. . . .

Offline PurdueGuy

  • Hero Member
  • *****
  • Posts: 1169
  • Karma: +21/-0
Re: Telnet session with Lua
« Reply #2 on: May 05, 2012, 04:17:56 am »
You can connect via LUA to telnet ports.  What do you want the telnet session to do?  Telnet is really just a tcp socket connection and sending/receiving characters.

I have a basic plugin for telnet to a TiVo and sending commands, but the TiVo doesn't reply, it just accepts the command.

I know you can also read from telnet connections, the denon plugin does that.
Vera3, Leviton, Intermatic, SQ Blaster, HomeWave, DSC 1832, Insteon/Altsteon

Offline Vera1234

  • Sr. Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
Telnet session with Lua
« Reply #3 on: May 05, 2012, 10:05:45 am »
I have a device (Linn Akurate Kontrol) which allows the following telnet (raw) commands:

SUBSCRIBE Preamp/Product
ACTION Preamp/Product 1 SetStandby "true"
UNSUBSCRIBE

In between these commands the device will provide a return (like "EVENT 167 1 Standby "true").

These commands will put the device in standby mode and that is exactly what I want it to do via the Vera. Of course the "false" gets the device online.

But I don't know where to start creating a plugin for this ;-)

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3258
  • Karma: +191/-9
Re: Telnet session with Lua
« Reply #4 on: May 06, 2012, 01:40:23 am »
Vera1234,

Is the telnet session kept open indefinitely, or are you expected to open a connection, send your command, then disconnect? It makes it easier if the session stays open the whole time.

Are all the commands line-based (i.e., they all end with a LF character)? Again, it's easier if this is the case.

There's only a little documentation from MCV on making a plugin. Here is the most complete worked example. Otherwise we can point you at working code by hobbyist developers like ourselves. Once you've answered the above questions we can nominate the closest example to what you want, and you can edit the plugin from there.

Offline Vera1234

  • Sr. Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
Re: Telnet session with Lua
« Reply #5 on: May 06, 2012, 03:25:36 am »
From these commands:
SUBSCRIBE Preamp/Product
ACTION Preamp/Product 1 SetStandby "true"
UNSUBSCRIBE

The unsubscribe is the command that closes the connection from the device. It literally kills the telnet connection. The other commands (subscribe or action) keep the connection open for the next command to send.

The commands are line-based. When I run these commands through a telnet session, I need to press <enter> after every command (subscribe, action, or unsubscribe). The protocol is "raw".

Would be great if you can pinpoint me to some "easy" code that I could re-engineer? Thanks!

Offline PurdueGuy

  • Hero Member
  • *****
  • Posts: 1169
  • Karma: +21/-0
Re: Telnet session with Lua
« Reply #6 on: May 06, 2012, 03:39:53 am »
Can multiple devices connect at once?  With my TiVo (and Denon, I think), no more than one device can have an active connection.  That's why I modified my TiVo plugin to do a connect/send/close with each command, since Vera and other things will talk to the TiVo at different times.
Vera3, Leviton, Intermatic, SQ Blaster, HomeWave, DSC 1832, Insteon/Altsteon

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3258
  • Karma: +191/-9
Telnet session with Lua
« Reply #7 on: May 06, 2012, 04:44:40 am »
What you are calling "raw telnet" is nothing more than a TCP socket to port 23. ("Cooked telnet" has escape sequences which are not relevant here.)

If you need to have the plugin connect-communicate-disconnect, then your plugin needs to do the socket connection itself using the LuaSocket library. I guess you could use PurdueGuy's plugins as examples. If you do this, you won't be able to catch asynchronous messages from the other end, because you are disconnected.

If it's OK or desirable for the plugin to have a continuous connection, you can have the Luup engine (which hosts your plugin's Lua code) open the socket for you. Your plugin calls luup.io.open() at startup and sends messages with luup.io.write(). There are examples which do this but I wouldn't call any of them "easy".

Ok, ball's in your court. You have to decide on the lifetime of the connections.

Offline Vera1234

  • Sr. Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
Re: Telnet session with Lua
« Reply #8 on: May 06, 2012, 05:10:00 am »
Maybe I don't understand the whole connection lifetime discussion, sorry.

The device only supports one concurrent telnet session. But I have no other control devices using this telnet session as the other control devices (ipad, ipod, etc) use the upnp connection on a different port.

The first command: SUBSCRIBE Preamp/Product, is needed to open an event "channel" into the device. The device returns an event notification of what a specific value is or an error message, but it also registers the session to an event channel number. If the telnet session gets disconnectd the second command will fail.

So the second command: ACTION Preamp/Product 1 SetStandby "true", is the actual action command that turns the device on or off (depending on the true or false value).

And the third command (UNSUBSCRIBE) closes the actual telnet session, literally. I think this could be skipped, but I rather play nice and stick to the protocol.

I downloaded @PurdueGuy's Tivo code and had a look at it. This piece from I_Tivo1.xml:
"-- TiVos require a Carriage Return after each command
          tivoSocket:send(cmd .. "\r")
          tivoSocket:close()
"
does it close the telnet session after every individual single command?

They way my device works is that I don't need to close the connection after every single command, as it is done by the UNSUBSCRIBE command, correct?

What do you think?

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3258
  • Karma: +191/-9
Telnet session with Lua
« Reply #9 on: May 06, 2012, 05:19:00 am »
After a SUBSCRIBE and its immediate response, if you just leave the telnet session open, do you ever expect to see anything sent back? Like, asynchronous events?

If no, then this whole discussion is irrelevant and you can just send your command and then disconnect.

If yes, and you need those asynchronous events, then you have to keep the telnet session open, so you can receive them.

Make sense?

Offline Vera1234

  • Sr. Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
Telnet session with Lua
« Reply #10 on: May 06, 2012, 05:53:31 am »
Ah, thanks I get it. The device doesn't send any asynchronous events that I am aware of (or need).

So do I leave the TivoSocket close() command out? So don't use it?

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3258
  • Karma: +191/-9
Telnet session with Lua
« Reply #11 on: May 06, 2012, 06:59:15 am »
In that case, you're good to largely copy PurdueGuy's code. I'd just put each of the three send-commands one after another in a single function, possibly with some sleeps in there if you find you need them. Keep the close() just for paranoia, in case the connection gets interrupted and the UNSUBSCRIBE goes astray: you still want the connection to close. (Also there are esoteric reasons to do with the TCP state diagram that you probably don't want me to go into.)

Offline Vera1234

  • Sr. Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
Re: Telnet session with Lua
« Reply #12 on: May 06, 2012, 02:03:12 pm »
Thanks.
..., possibly with some sleeps in there if you find you need them. ...
What is the proper syntax for a sleep command? It's a good suggestion!

This is code I am thinking of doing:

          -- Linn LPEC requires a SUBSCRIBE before the ACTION
     linnSocket:send("SUBSCRIBE Preamp/Product" .. "\r")
          linnSocket:send(cmd .. "\r")
     -- Linn LPEC requires a UNSUBSCRIBE after the ACTION
     linnSocket:send("UNSUBSCRIBE" .. "\r")
          linnSocket:close()

Sounds correct?

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3258
  • Karma: +191/-9
Re: Telnet session with Lua
« Reply #13 on: May 06, 2012, 05:48:03 pm »
sleep() is part of the MCV extension set to Lua: http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_sleep

You've got enough to get started now with a good chance of success. Let us know how it goes!

Offline skhanay

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
Re: Telnet session with Lua
« Reply #14 on: October 21, 2018, 03:50:03 am »
Hello.
I can't find any sample for send command from Vera lite to any device by telnet. I need reboot my ip camera every day. I have login, pwd and command it works great by my computer, but I don't won't to use it. Could you help me, how do it from Vera lite with lua?