Telnet Commands / Button?

Hello.
I am trying to archive a goal for wich I need to issue telnet commands in a row.
My logic way of implementing this should be using a button to send commands, but I am completly in deep waters, at least for my experience.
So I will explain what I need to archieve:
My router, from my operator, sometimes changes the wifi channel, and when it does it causes a wreck in my devices; i can go to the interface and change it, but I need a button and/or an automation to do this automatically when I detect that some specific devices are offline for a specified time. Telnet is an option and I can issue the correct commands from a windows telnet command line. I can do automations pretty well, thats not my dificulty, what i donā€™t know is how to issue this telnet commands from inside Home Assistant. If it could be by using a button, the better.
I am running HA on a VM in Windows10.

So, when I telnet my router, at 192.168.1.254, it asks me for my username, then password, and then I can issue the commands.
image

image

then I can issue the command to change wifi channel:

wireless/basic/config --wifi-index=0 --wifi-channel=4

I have been trying to figure this out, reading a lot of posts, but I canā€™t manage to archive this. I read about cheating a .sh file with the commands, and I figured that the command should be something like:

{ sleep 3; echo "meo"; sleep 1 ;echo "mycomplexpassword"; sleep 2 ;echo "wireless/basic/config --wifi-index=0 --wifi-channel=4"; sleep 60; echo "exit"} | telnet 192.168.1.254

So, how can I create a button to do this? Help, please?
Thanks to all in advance!

1 Like

Set up a shell command. That will create a service. You can call a service from a button.

Thanks!
I tryed to setup the shell command in configuration.yaml, added:

shell_command:
  router_meo_to_channel_4: '{ sleep 3; echo -e "meo"; sleep 1 ;echo -e "mycomplexpassword"; sleep 2 ;echo -e "wireless/basic/config --wifi-index=0 --wifi-channel=5"; sleep 60; echo -e "exit"} | telnet 192.168.1.254'

HA restarted OK and I can run the command:
image

But I get this log:

Logger: homeassistant.components.shell_command
Source: /usr/src/homeassistant/homeassistant/components/shell_command/__init__.py:115
Integration: Shell Command ([documentation](https://www.home-assistant.io/integrations/shell_command), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+shell_command%22))
First occurred: 10:56:08 (1 occurrences)
Last logged: 10:56:08

Error running command: `{ sleep 3; echo -e "meo"; sleep 1 ;echo -e "mycomplexpassword"; sleep 2 ;echo -e "wireless/basic/config --wifi-index=0 --wifi-channel=5"; sleep 60; echo -e "exit"} | telnet 192.168.1.254`, return code: 2

NoneType: None

Any ideas? How can I test the command before i add it to the .yaml file ? Sorry for the noob questions, iā€™m really out of my leagueā€¦

I donā€™t know if I can test the command on the command line window in Windows, because I get this:

image

Well, replying to my own post, I managed to find a solution in other post:
https://community.home-assistant.io/t/telnet-switch-with-login-command-line-switch/220411
For the sake of the noobs like me, here is teh walkthough:

What I did:

Using File Editor I created a file inside my python_scripts folder, called it ā€˜router_meo_set_wifichannel_4.pyā€™
So, it was stored in /config/python_scripts/router_meo_set_wifichannel_4.py

Contents of the file with the commands, that you can adapt to your case:

#!/usr/bin/python3
import telnetlib
import time
HOST = "192.168.1.254"                                                   # ip adress of whatever you want to control
telnet = telnetlib.Telnet(HOST)
telnet.read_until(b":",3)                                                # wait for : before writing username
telnet.write(b"meo\n")
telnet.read_until(b":",3)                                                # wait for : before writing password
telnet.write(b"mycomplexpassword\n")
telnet.read_until(b"/cli>",10)                                           # wait for "/cli" before writing command
telnet.write(b"wireless/basic/config --wifi-index=0 --wifi-channel=4\n") # command
telnet.read_until(b"/cli>",120)                                          # wait for "/cli" (commands have been executed)
telnet.write(b"exit\n")                                                  # all done, exit and close
telnet.close()

in configuration.yaml added this:

# Allow Phyton Scripts 
python_script:

# Command line service:
shell_command:
  router_meo_to_channel_4: 'python3 /config/python_scripts/router_meo_set_wifichannel_4.py'

Then I can run the shell command as a service:

Hope it helps more people, very gratefull to swedude for his explanation! :slight_smile:

Hi Pedro, 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

2 Likes

Hi @drhy

Would you mind to share one of the pyscript you did to control audio/video stuffs ? Iā€™m struggling here to succeed to do one for a video matrix :frowning:

Thanks

Yes
example ā€œdateā€ 192.168.1.4

command_line:
  - sensor:
      name: aqara_test
      command: "apk -q add inetutils-telnet && ( sleep 3; printf 'root\n'; sleep 1; printf 'clear\n'; sleep 1; printf 'date\n'; sleep 1; ) | telnet 192.168.1.4 | sed '1,8d' | sed '$d'"
      value_template: "{{ value }}"

immagine

immagine