How to set up Telnet communication via Exec binding?

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.

What is your preferred scripting programming language ?
E.g for perl you may have a look at this module: Net::Telnet - interact with TELNET port or other TCP ports - metacpan.org
What you also could use is expect: Expect command and how to automate shell scripts like magic

these are just two ideas out of …

It might be easier to use the serial binding with the serial port on the device. No authentication would be needed.

You just specify an item like this:

String hdmiMTX             "HDMI Matrix"           { serial="/dev/ttyUSB0@9600" }

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.

Regards S

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

1 Like

Many thanks for help of you all!

I used expect as you suggested but went for a little shortcut.

  1. expect installation
  2. expect_autoexpect
  3. in rules I used only execution of automatically prepared script
executeCommandLine('/home/openhabian/s_0202.exp', 10000)

Simple and working fine.

Thank you!

Hi again!

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)

Sorry, I should read about changes first…

Thank you for your post!
I was able to setup a telnet script to control my Sentry Switched CDU. Now I can reboot my devices easily from OpenHAB.

1 Like