How to set up Telnet communication via Exec binding?

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