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.
then I can issue the command to change wifi channel:
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:
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()
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.