HA telnet switch platform - what about login?

I want to execute some shell command on another system, from an HA automation. I can already to this manually via any telnet client. I found the HA telnet switch platform, it would do what I need except - wait - what about login? There’s just minimal documentation for this platform, nothing about actually making the telnet connection and supplying a password. I don’t get it.

If you require login you should use an expect script and a commandline switch.

However you are better to do the whole thing via ssh with key auth, if your device supports ssh of course.

1 Like

Actually on a bit of reflection I realize the “switch” model isn’t what I want anyway - because I have no way to know the initial state.

What I want to do is open telnet to the remote system and execute a shell command, blind - no response needed. I don’t want to mess the SSH, the keys, etc, that’s overkill in this situation.

So I need a way to open a telnet session with name/password, send a command, close session - from within an automation. My HA skills are limited but I’m sure there’s something out there.

Like I said, an expect script.

No idea what an ‘expect script’ is but I’ll try to find out.

First google hit https://www.baeldung.com/linux/telnet-automate-using-expect

Ok, a telnet scripting app.

I’d prefer to accomplish this using stuff already available within HA - and maybe some Python.

Try this then Is it possible to create a sensor via Telnet?, or a script to get data via Telnet Commands? - #19 by davefi

expect is not available in HA OS.

2 Likes

Is any tutorial for this, I also need solution just for “open telnet to the remote system and execute a shell command, blind - no response needed”…

I create script and try it run with .shell_command, but no luck, get this error:

Error running command: `bash /config/shell/router_reboot.sh`, return code: 127

Script:

#!/bin/sh
(sleep 1; echo -e "root\r"; sleep 1; echo -e "passwrd\r"; sleep 1; echo -e "reboot\r"; sleep 1; echo -e "exit\r") | telnet 192.168.8.3

Hi Jim, the other option, which I eventually got to is pyscript.

I have a bunch of audio, HDMI and home theater devices that all use telnet for the command interface (along with other gadgets such as TVs which have all their own connections and integrations).

After struggling with complex HA Automations, shell scripts using nc (netcat) and various methods including json to update HA afterwards, I ended up with a relatively slow, un-maintainable and un-reliable solution.

I have some scripting experience but knew nothing about python. But in desperation, knowing that telnetlib for python existed (thank you ! ), and given that HA appears to be largely a python creation, I started (a) learning basic python (supported by Python tester - Test code online), and (b) working through the python alternatives.

I looked at HA’s python_script but it was going to be complex to get relatively simple things done. There were two other options whose names I can’t recall but were also going to be complex.

I eventually found pyscript, read the concise but very good documentation, and eventually migrated. It is well integrated into HA, provides global and persistent variables, has replaced all of my shell scripts (bash) and HA scripts, and almost all of my Automations. Total number of lines has been reduced by about 70%. Pyscript turned out to be easy learn - level is about VB script rather than C# .

I now have an HA for my environment that is much more maintainable and reliable, and is quicker.

David

Hi David,

Do you have any more pointers / examples as to what you did to control your HT devices via Telnet using pyscript?

I have a PJ and Audio / Video matrix that I would like to look into controlling / querying status on.

Thanks
Barry

1 Like