Telnet session with Lua

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!

Did you ever got an answer to this? I have the same question. . . .

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.

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

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.

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

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.

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.

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?

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?

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?

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

Thanks.

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?

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!

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?