I’m looking for help in setting up connection via Telnet with HDMI Switch. As I suppose I found similar problem in this topic link but after spending a few nights trying to work it out I still cannot get it done…
Ecec script should connect to device via Telnet; send username, password, command and quit connection. I installed telnet on my openhabian and I can control my switch from console that’s why I think Exec binding can do it as well…
As I suppose platform or device documentation doesn’t matter.
Can somebody share simple tutorial or example of exec script that can establish telnet connection and send a simple command?
My device
IP adress: 192.168.10.200
Username: admin
Password: 123
Example command : 0303
Openhabian 2.5 on RaspberryPI
Forget about the Exec binding or executeCommandLine. Figure out how to make this work in a shell script. That’s going to be the hard part and it’s completely outside of openHAB itself.
Once it works in a shell script you’ll be able to call that from openHAB.\
Neither the Exec binding nor executeCommandLine are interactive. So you need to figure out how to issue all the commands in one go, which is what a shell script is for.
And then send commands to it via a rule:
hdmiMTX.sendCommand(“0303”)
It looks like the device will send status updates via the serial port… With those you could keep track of the device’s status if it changes via the IR remote.
I recommend looking at expect, it’s really powerful.
I use it frequently to connect to various clis on switches, firewalls and other network peripherals.
If you are more into python there is an expect version for that too.
Yes install expect.
I use it to drop the power on POE ports on my switch if I have to power reset the device that is on that port.
The you run something like this:
expect_autoexpect telnet 192.168.0.5
and this will generate all the commands you need etc.
There will be a file called script.exp in the directory you ran the command from.
It will have commands etc similar to this:
set timeout -1
spawn telnet 192.168.0.5
match_max 100000
expect -exact "Trying 192.168.0.5...\r
Connected to 192.168.0.5.\r
Escape character is '^\]'.\r
\r
\r
User Access Verification\r
\r
Username: "
send -- "secretuser\r"
expect -exact "secretuser\r
Password: "
send -- "secretpassword\r"
expect -exact "\r
Switch>"
send -- "exit\r"
expect eof
Note: Make sure you type everything correctly otherwise the incorrect commands will be in the script.exp file. This includes backspaces and deletes etc.
Then to run the script (Javascript) example below:
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
//below is if you are going to use the execute command
var Exec = Java.type("org.openhab.core.model.script.actions.Exec");
//below is also neede for the execute command
var Duration = Java.type("java.time.Duration");
Exec.executeCommandLine(Duration.ofSeconds(20), "/usr/local/sbin/zoneminder-record.sh","Record");
The above example runs the script /usr/local/sbin/zoneminder-record.sh and passes a parameter of Record
I installed OH3 on new device running Ubuntu Server. Now I don’t know how to run the same script… Can you help me to do the same in OH3? I tried to create rule from rules folder same as on OH2 but it doesn’t work… What am I missing?
Most probably the differnerence between OH3: executeCommandLine(Duration.ofSeconds(timeout), String commandLine)
and OH2: executeCommandLine(String commandLine, int timeout)