Kodi Community Forum

Full Version: Bash script within addon
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(Caution: Noob-question)
Hi,
i'm new to kodi, python and coding in general. I installed zags hello.world script and started playing with the addon.py. I managed to trigger a bash-script within the addon.py:
Code:
import os




bashCommand = "/bin/bash /home/pi/script.sh"
os.system(bashCommand)

So i was able to kill kodi.bin by simply adding
Code:
#!/bin/sh
killall kodi.bin
to script.sh.
However, if i try to mount an sshfs-filesystem via script.sh, nothing happens
Code:
echo password | sshfs -o allow_other -o password_stdin [email protected]:/ /media/ssh/
the sshfs-script is executed correctly by the addon.py when i run the python-script outside of kodi.

I run Kodi 15.2/Raspbian jessie on a Raspberry Pi 2

Thanks for your help!
ob
Does sshfs need to be run as root?
(2016-01-18, 15:13)el_Paraguayo Wrote: [ -> ]Does sshfs need to be run as root?

No, with raspbian default user "pi", it runs without sudo. But maybe it's a permissions problem. Unfortunately, i cannot test the script with user "kodi" scince i have no idea how to log in as "kodi" via terminal. Is this even possible on raspbian?
Thanks
Permissions was my guess. No experience of sshfs though. Does Kodi run as a different user? if so, who owns /mnt/ssh/ and what are the permissions?
(2016-01-18, 16:11)el_Paraguayo Wrote: [ -> ]Permissions was my guess. No experience of sshfs though. Does Kodi run as a different user? if so, who owns /mnt/ssh/ and what are the permissions?

This is the output of ls -l
drwxr-xr-x 2 kodi pi 4096 Jan 6 21:43 ssh

Kodi runs indeed as a different user, named "kodi" which was autogenerated while installing kodi via apt-get.

EDIT: i just noticed, that, right after i ran the script in kodi, df -h gives the following output:

df: ‘/media/ssh’: Transport endpoint is not connected
This post (http://stackoverflow.com/a/31493771/3087339) suggests you need to run the unmount command and try again.

Way out of my depth here...
(2016-01-18, 18:40)el_Paraguayo Wrote: [ -> ]This post (http://stackoverflow.com/a/31493771/3087339) suggests you need to run the unmount command and try again.

Way out of my depth here...

Doen't work, but thank you anyway for this hint.
Do you get any error output in your kodi.log file when you run the script?
(2016-01-18, 19:50)el_Paraguayo Wrote: [ -> ]Do you get any error output in your kodi.log file when you run the script?

No, however, here's the debug-log output:
Code:
19:03:00 T:1599075248    INFO: initializing python engine.
19:03:00 T:1599075248   DEBUG: CPythonInvoker(6, /home/kodi/.kodi/addons/script.hello.world-master/addon.py): start processing
19:03:00 T:1835889584   DEBUG: PushCecKeypress - received key  b duration 359
19:03:00 T:1599075248  NOTICE: -->Python Interpreter Initialized<--
19:03:00 T:1599075248   DEBUG: CPythonInvoker(6, /home/kodi/.kodi/addons/script.hello.world-master/addon.py): the source file to load is "/home/kodi/.kodi/addons/script.hello.world-master/addon.py"
19:03:00 T:1599075248   DEBUG: CPythonInvoker(6, /home/kodi/.kodi/addons/script.hello.world-master/addon.py): setting the Python path to /home/kodi/.kodi/addons/script.hello.world-master:/usr/lib/python2.7:/usr/lib/python2.7/plat-arm-linux-gnueabihf:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PILcompat
19:03:00 T:1599075248   DEBUG: CPythonInvoker(6, /home/kodi/.kodi/addons/script.hello.world-master/addon.py): entering source directory /home/kodi/.kodi/addons/script.hello.world-master
19:03:00 T:1599075248   DEBUG: CPythonInvoker(6, /home/kodi/.kodi/addons/script.hello.world-master/addon.py): instantiating addon using automatically obtained id of "script.hello.world" dependent on version 2.14.0 of the xbmc.python api
19:03:01 T:1599075248    INFO: CPythonInvoker(6, /home/kodi/.kodi/addons/script.hello.world-master/addon.py): script successfully run
19:03:01 T:1599075248    INFO: Python script stopped
19:03:01 T:1599075248   DEBUG: Thread LanguageInvoker 1599075248 terminating
Maybe try using subprocess instead of os.system to catch the output of the sshfs command and print it so it shares in the log.
(2016-01-19, 10:38)el_Paraguayo Wrote: [ -> ]Maybe try using subprocess instead of os.system to catch the output of the sshfs command and print it so it shares in the log.

+1
(2016-01-20, 20:17)Lunatixz Wrote: [ -> ]
(2016-01-19, 10:38)el_Paraguayo Wrote: [ -> ]Maybe try using subprocess instead of os.system to catch the output of the sshfs command and print it so it shares in the log.

+1

Can you tell me how to do this? I tried
subprocess.Popen('echo password | sshfs -f -o allow_other -o password_stdin [email protected]:/ /media/ssh >> /home/kodi/log.txt', shell=True)
log.txt is beeing generated, but stayed empty.

Thanks
Something like this:
Code:
from subprocess import check_output, CalledProcessError

try:
    cmd = "echo password | sshfs -f -o allow_other -o password_stdin [email protected]:/ /media/ssh"
    subproc = check_output(cmd, shell=True)
    print "SSHFS executed successfully. Output: {}".format(subproc)
except CalledProcessError, e:
    print "SSHFS execution failed. Output {}".format(e.output)

It's ugly code and I don't recommend using "shell=True" normally but this is just for debugging.
(2016-01-21, 21:40)el_Paraguayo Wrote: [ -> ]Something like this:
Code:
from subprocess import check_output, CalledProcessError

try:
    cmd = "echo password | sshfs -f -o allow_other -o password_stdin [email protected]:/ /media/ssh"
    subproc = check_output(cmd, shell=True)
    print "SSHFS executed successfully. Output: {}".format(subproc)
except CalledProcessError, e:
    print "SSHFS execution failed. Output {}".format(e.output)

It's ugly code and I don't recommend using "shell=True" normally but this is just for debugging.

Thanks for the code!
As far as i can see, there is no output. Here's the related part of my kodi.log
Code:
11:20:30 T:1574355888    INFO: initializing python engine.
11:20:30 T:1574355888   DEBUG: CPythonInvoker(62, /home/kodi/.kodi/addons/script.hello.world-master/addon.py): start processing
11:20:30 T:1574355888  NOTICE: -->Python Interpreter Initialized<--
11:20:30 T:1574355888   DEBUG: CPythonInvoker(62, /home/kodi/.kodi/addons/script.hello.world-master/addon.py): the source file to load is "/home/kodi/.kodi/addons/script.hello.world-master/addon.py"
11:20:30 T:1574355888   DEBUG: CPythonInvoker(62, /home/kodi/.kodi/addons/script.hello.world-master/addon.py): setting the Python path to /home/kodi/.kodi/addons/script.hello.world-master:/usr/lib/python2.7:/usr/lib/python2.7/plat-arm-linux-gnueabihf:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PILcompat
11:20:30 T:1574355888   DEBUG: CPythonInvoker(62, /home/kodi/.kodi/addons/script.hello.world-master/addon.py): entering source directory /home/kodi/.kodi/addons/script.hello.world-master
11:20:30 T:1574355888   DEBUG: CPythonInvoker(62, /home/kodi/.kodi/addons/script.hello.world-master/addon.py): instantiating addon using automatically obtained id of "script.hello.world" dependent on version 2.14.0 of the xbmc.python api
11:20:30 T:1835705264   DEBUG: PushCecKeypress - received key  b duration 531
11:20:34 T:1574355888  NOTICE: SSHFS execution failed. Output
11:20:34 T:1574355888    INFO: CPythonInvoker(62, /home/kodi/.kodi/addons/script.hello.world-master/addon.py): script successfully run
11:20:34 T:1574355888    INFO: Python script stopped
11:20:34 T:1574355888   DEBUG: Thread LanguageInvoker 1574355888 terminating
pi@raspberrypi:/home/kodi/.kodi/temp $
I set sshfs to verbose-mode by adding -d
OK. But at least we can see that it is failing. So that's progress!

Let's tweak the code slightly. Try this:
Code:
from subprocess import check_output, CalledProcessError, STDOUT

try:
    cmd = "echo password | sshfs -f -o allow_other -o password_stdin [email protected]:/ /media/ssh"
    subproc = check_output(cmd, shell=True, stderr=STDOUT)
    print "SSHFS executed successfully. Output: {}".format(subproc)
except CalledProcessError, e:
    print "SSHFS execution failed. Output {}".format(e.output)
Pages: 1 2