Problems running a python3 script with exec binding

Hello there,

I am trying to run a python 3 script with the exec binding and i simply cannot get it to work.

It is running when i launch it via terminal or visual studio code but not from the scripts section from the new ui (rule).
So i am pretty sure it’s an openhab permission error …

My system: I am running openhab 3.1.0-SNAPSHOT - Build #2424 on a Debian virtual machine (vbox).

Here’s my rule:

rule "iPad entsperren"
        when
                Item EG_Essbereich_Praesenz received command ON                                                                                                                       
        then
                executeCommandLine("sudo python3 /etc/openhab/scripts/iPad_wake.py")
end

and here’s the script:



import paramiko

#nbytes = 4096
hostname = 'iPad-Wand'
port = 22
username = 'root' 
password = 'secret'
command = 'activator send libactivator.system.homebutton \n activator send libactivator.system.homebutton'
#command = 'activator send libactivator.system.sleepbutton'

client = paramiko.Transport((hostname, port))
client.connect(username=username, password=password)

#stdout_data = []
#stderr_data = []
session = client.open_channel(kind='session')
session.exec_command(command)
# while True:
#     if session.recv_ready():
#         stdout_data.append(session.recv(nbytes))
#     if session.recv_stderr_ready():
#         stderr_data.append(session.recv_stderr(nbytes))
#     if session.exit_status_ready():
#         break

#print 'exit status: ', session.recv_exit_status()
#print ''.join(stdout_data)
#print ''.join(stderr_data)

session.close()
client.close()

I did add user openhabian and openhab to the sudoers file like this:

openhabian      ALL=(ALL) NOPASSWD:ALL
openhab         ALL=(ALL) NOPASSWD:ALL

before that i tried the safer way described here:

but i could not get it to work either.

edit (forgot the error log):

2021-07-03 23:42:41.176 [WARN ] [rg.openhab.core.io.net.exec.ExecUtil] - Error occurred when executing commandLine ‘[sudo python3 /etc/openhab/scripts/iPad_wake.py]’

java.io.IOException: Cannot run program “sudo python3 /etc/openhab/scripts/iPad_wake.py”: error=2, Datei oder Verzeichnis nicht gefunden

at java.lang.ProcessBuilder.start(Unknown Source) ~[?:?]

at java.lang.ProcessBuilder.start(Unknown Source) ~[?:?]

at org.openhab.core.io.net.exec.ExecUtil.executeCommandLine(ExecUtil.java:59) ~[?:?]

at org.openhab.core.model.script.actions.Exec.executeCommandLine(Exec.java:40) ~[?:?]

at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]

at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:?]

at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]

at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]

at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1192) ~[?:?]

at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1167) ~[?:?]

at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1153) ~[?:?]

at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:1098) ~[?:?]

at org.openhab.core.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:151) ~[?:?]

at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:1008) ~[?:?]

at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:971) ~[?:?]

at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:247) ~[?:?]

at org.openhab.core.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]

at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:227) ~[?:?]

at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:475) ~[?:?]

at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:251) ~[?:?]

at org.openhab.core.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]

at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:227) ~[?:?]

at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:213) ~[?:?]

at org.openhab.core.model.script.runtime.internal.engine.ScriptImpl.execute(ScriptImpl.java:80) ~[?:?]

at org.openhab.core.model.script.runtime.internal.engine.DSLScriptEngine.eval(DSLScriptEngine.java:131) ~[?:?]

at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:62) ~[?:?]

at java.util.Optional.ifPresent(Unknown Source) ~[?:?]

at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:59) ~[?:?]

at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1183) ~[?:?]

at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1035) ~[?:?]

at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1051) ~[?:?]

at org.openhab.core.automation.rest.internal.RuleResource.runNow(RuleResource.java:327) ~[?:?]

at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]

at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:?]

at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]

at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]

at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:179) ~[bundleFile:3.4.3]

at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96) ~[bundleFile:3.4.3]

at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:201) ~[bundleFile:3.4.3]

at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:104) ~[bundleFile:3.4.3]

at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59) ~[bundleFile:3.4.3]

at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96) ~[bundleFile:3.4.3]

at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) ~[bundleFile:3.4.3]

at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121) ~[bundleFile:3.4.3]

at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:265) ~[bundleFile:3.4.3]

at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234) ~[bundleFile:3.4.3]

at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208) ~[bundleFile:3.4.3]

at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160) ~[bundleFile:3.4.3]

at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:225) ~[bundleFile:3.4.3]

at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:298) ~[bundleFile:3.4.3]

at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:217) ~[bundleFile:3.4.3]

at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) ~[bundleFile:3.1.0]

at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:273) ~[bundleFile:3.4.3]

at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:791) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:550) ~[bundleFile:9.4.40.v20210413]

at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:71) ~[bundleFile:?]

at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:602) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1624) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1435) ~[bundleFile:9.4.40.v20210413]

at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:294) ~[bundleFile:?]

at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:501) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1594) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1350) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) ~[bundleFile:9.4.40.v20210413]

at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:82) ~[bundleFile:?]

at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.server.Server.handle(Server.java:516) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:388) ~[bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:633) [bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:380) [bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277) [bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311) [bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105) [bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104) [bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:336) [bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:313) [bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:171) [bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:129) [bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:383) [bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:882) [bundleFile:9.4.40.v20210413]

at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1036) [bundleFile:9.4.40.v20210413]

at java.lang.Thread.run(Unknown Source) [?:?]

Caused by: java.io.IOException: error=2, Datei oder Verzeichnis nicht gefunden

at java.lang.ProcessImpl.forkAndExec(Native Method) ~[?:?]

at java.lang.ProcessImpl.(Unknown Source) ~[?:?]

at java.lang.ProcessImpl.start(Unknown Source) ~[?:?]

… 88 more

thank you for your support!

You’ll want to be using OH3 executeCommandLine() syntax, not OH2 syntax from older postings.

2 Likes

oh my.
thank you for pointing to that.

i tried to adapt my config and came up with this:

things:

Thing exec:command:ipadwake [ command="python3 /etc/openhab/scripts/iPad_wake.py", interval=0, autorun=false ]

items:

Switch      iPadWakeRunning {channel="exec:command:ipadwake:run"}

rule:

rule "iPad entsperren"
        when
                Item EG_Essbereich_Praesenz received command ON                                                                                                                       
        then
                iPadWakeRunning.sendCommand(ON)
end

whitlist:

# For security reasons all commands that are used by the exec binding or transformation need to be whitelisted.
# Every command needs to be listed on a separate line below.
python3 /etc/openhab/scripts/iPad_wake.py

it triggers but nothing happens.
i tried:
ssudo -u openhab python3 /etc/openhab/scripts/iPad_wake.py

but it gives cli gives me an error:


Traceback (most recent call last):
  File "/etc/openhab/scripts/iPad_wake.py", line 1, in <module>
    import paramiko
ModuleNotFoundError: No module named 'paramiko

I installed paramiko with pip3 install paramiko and pip install paramiko and tried executing it with python as well - same error.

Do you have any idea where to look at?
Thank you!

Although your described installation should work I have one question as you wrote that you are on a Debian VM: doesn’t your distro provide a package that is called: python3-paramiko ?
Could you try to install and use that one ?

1 Like

hello

yes, it works :slight_smile: thank you so much! where did you find the soution? :slight_smile:
i only get a warning after the script is executed:

2021-07-04 20:14:44.798 [WARN ] [ng.exec.internal.handler.ExecHandler] - Couldn't transform response because transformationService of type 'REGEX' is unavailable

i cannot make any sense out of this because i am not using any transformation at all …

thanks so much!

Tweaked the rule a little bit to also lock it when no one is present:

rule "iPad entsperren/sperren"
        when
                Item EG_Essbereich_Praesenz changed                                                                                                                       
        then
                if (EG_Essbereich_Praesenz.state == ON)
                        {
                        iPadWakeRunning.sendCommand(ON)
                        }
                if (EG_Essbereich_Praesenz.state == OFF)
                        {
                        iPadSleepRunning.sendCommand(ON)
                        }
end