[solved] Udp command via exec binding

Hi!

I am using openhab 3 on a raspi with openhabian installation.
Due to the fact that the udp/tcp binding is not supported yet, I try to send an udp package via exec binding.

thing setup:
Thing exec:command:udp_out [command="/usr/bin/echo %2$s > /dev/udp/10.0.0.55/5000", autorun=true]

Item setup:
String UDP_OUT "[%s]" {channel="exec:command:udp_out:input"}

rule call:
UDP_OUT.sendCommand("LED_ON")

whitelist content:
/usr/bin/echo %2$s > /dev/udp/10.0.0.55/5000

Nothing happens after rule call, I do see in the log that it is executed but the receiver does not get the command.
A manual call from comand line does the job, the receiver gets the message.
> /usr/bin/echo LED_ON > /dev/udp/10.0.0.55/5000

Someone an idea what’s going wrong?

did you try sudo -u openhab /usr/bin/echo LED_ON > /dev/udp/10.0.0.55/5000 to try it in the openhab-user context?

Thanks for reply!

I changed my thing according to your recommendation (whitlist was updated, too)

Thing exec:command:udp_out [command="sudo -u openhab /usr/bin/echo LED_ON > /dev/udp/10.0.0.55/5000", autorun=true]

Same result, no message was send after item call

Note: The exec binding is working because I do use it with a diffrent command:
Thing exec:command:sky_cmd [command="/usr/bin/sky-remote-cli 10.0.0.53 %2$s", autorun=true]

oh. sorry. the command is for bash-use in a terminal. with that you can simulate what would happen if openhab would trigger the command.

so, after you ssh’d to your openhab server, use the command to see if there’s a different outcome.

You mean to start it from command line?

This is working as expected, with and without sudo -u openhab

 > /usr/bin/echo LED_ON > /dev/udp/10.0.0.55/5000
 > sudo -u openhab /usr/bin/echo LED_ON > /dev/udp/10.0.0.55/5000

Both calls send the message successfully to the receiver

you already whitelisted it, there’s no issue with the user rights…
So I’m a bit lost here, should work! :wink:
what you could try is, whether the execution makes it to your bash, so change your thing and add some action like touch:

Thing exec:command:udp_out [command="sudo -u openhab /usr/bin/echo LED_ON > /dev/udp/10.0.0.55/5000; touch /etc/openhab/misc/exec.whitelist", autorun=true]

of course add your whitelist accourdingly. what this does is bascially change date of your exec.whitelist file. So after changing your item it should change the date of your exec.whitelist file. If it doesn’t, the command is not coming through.
could you also do the following:

  1. login to openHAB console openhab-cli console (pw is habopen)
  2. set org.openhab.binding.exec to TRACE logging afterwards log:set TRACE org.openhab.binding.exec
  3. update your item

then please post the stuff in your logs.

  1. reset to INFO logging afterwards: log:set INFO org.openhab.binding.exec
1 Like

It does change it, so exec call is working.
But why isn’t the udp call not executed :grimacing:

Ahhh, some error output :blush:

2021-01-12 10:38:33.693 [TRACE] [ng.exec.internal.handler.ExecHandler] - Executing command '/usr/bin/echo LED_ON > /dev/udp/10.0.0.55/5000; touch /etc/openhab/misc/exec.whitelist' after a change of the input
 channel to 'LED_ON'
2021-01-12 10:38:33.699 [DEBUG] [ng.exec.internal.handler.ExecHandler] - Passing to shell for parsing command.
2021-01-12 10:38:33.706 [DEBUG] [ng.exec.internal.handler.ExecHandler] - OS: *NIX (Linux)
2021-01-12 10:38:33.709 [TRACE] [ng.exec.internal.handler.ExecHandler] - The command to be executed will be '[sh, -c, /usr/bin/echo LED_ON > /dev/udp/10.0.0.55/5000; touch /etc/openhab/misc/exec.whitelist]'
2021-01-12 10:38:33.727 [DEBUG] [ng.exec.internal.handler.ExecHandler] - Exec [ERROR]: 'sh: 1: cannot create /dev/udp/10.0.0.55/5000: Directory nonexistent'
2021-01-12 10:38:33.748 [DEBUG] [ng.exec.internal.handler.ExecHandler] - Transformed response is 'sh: 1: cannot create /dev/udp/10.0.0.55/5000: Directory nonexistent
sh: 1: cannot create /dev/udp/10.0.0.55/5000: Directory nonexistent'

google says: The functionality you’re trying to use is available in bash, not the POSIX shell

So my workaround, I call a bash script via exec binding

thing:
Thing exec:command:udp_out [command="/etc/openhab/scripts/udp.bash %2$s", autorun=true]

The bash script:

#!/bin/bash
/usr/bin/echo $1 > /dev/udp/10.0.0.55/5000

and finally the item call sends the message successfully:
UDP_OUT.sendCommand("LED_ON")

Thanks for pointing me to the problem @binderth

1 Like

I got same problems since I tested OH 3. I want to control my Home Theatre (LG) with simple udp messages, but without tcp/udp binding, I try to swop to exec binding, like in this thread, but I got the error in the open hab logs, “Permission denied”. I don’t know why?

My configuration is:
OH 3 on a MacBook, works fine
Exec binding is installed
REGEX binding is also installed

Simple example to send the command “Vol_Up” as follow:

items:

Switch         LG_Volume
String         LG_Communication      { channel="exec:command:udp_out:input" }

things:

Thing exec:command:udp_out [command="/etc/openhab/conf/scripts/udp.bash %2$s", autorun=true]

rules:

rule "LG_Volume"
 when
     Item    LG_Volume   received command
 then
     LG_Communication.sendCommand("Vol_Up")
end

bash script:

#!/bin/bash
/usr/bin/echo $1 > /dev/udp/192.168.yyy.xxx/xyz

exec whitelist:

/etc/openhab/conf/scripts/udp.bash %2$s

But I got only the following Error in Openhab log, when I try to send the command via LG_Volume Switch:

2021-07-09 13:10:40.997 [TRACE] [ng.exec.internal.handler.ExecHandler] - Executing command '/etc/openhab/conf/scripts/udp.bash %2$s' after a change of the input channel to 'Vol_Up'
2021-07-09 13:10:40.998 [DEBUG] [ng.exec.internal.handler.ExecHandler] - Passing to shell for parsing command.
2021-07-09 13:10:40.998 [DEBUG] [ng.exec.internal.handler.ExecHandler] - OS: *NIX (Mac OS X)
2021-07-09 13:10:40.998 [TRACE] [ng.exec.internal.handler.ExecHandler] - The command to be executed will be '[sh, -c, /etc/openhab/conf/scripts/udp.bash Vol_Up]'
2021-07-09 13:10:41.453 [DEBUG] [ng.exec.internal.handler.ExecHandler] - Exec [ERROR]: 'sh: /etc/openhab/conf/scripts/udp.bash: Permission denied'
2021-07-09 13:10:41.455 [DEBUG] [ng.exec.internal.handler.ExecHandler] - Transformed response is 'sh: /etc/openhab/conf/scripts/udp.bash: Permission denied'

Who can help me out?

Can you post the permissions of the script?
Maybe the openhab instance does not have access?

Everyone, staff and me have read/write permission.

POSX shell ( sh ) is being used instead of the bash.
The first does not support creating UDP packages in the way you want it to do.
Is the script executable for everyone ? Not sure if that helps.
If that does not help and still sh is being used inside of the binding try to foce it to use bash.
In case that does not work use nc also known as netcat in your shell script.