Run shell script via keymap

Hi. I’m looking for a steer in the right direction if possible, rather than support to help me achieve this.

Currently, I have my vpn running 24/7 but need to disable it for certain addons. Ideally this process would be automated as an addon opens/ closes, but I don’t know if that’s possible. So, I’d like to create a shell script that I can map to a button press, which would start/stop my openvpn instance - the equivalent of SSHing into Vero and issuing systemctl start/ stop openvpn

I’ve tried creating a script using notepad++:

#! /bin/bash
sudo systemctl stop openvpn
(this works via command line with bash /path/to/script.sh)

…then map this script to a button press in keyboard.xml using RunScript (path/to/script.sh), but nothing happens.

Having done some digging on osmc and other forums I have a feeling I’d need to write a python script (about which I have no knowledge currently), but it would be really helpful to know if this is even possible first, before wasting a lot more time on this. Thank you!

Did a little addon a while back, that point you into the right direction. Before I updated it for v.19, it actually just started a shell script.

Else I would suggest you checkout:

Thank you - really helpful!

The “runscript” Kodi action is only for python, not bash. The Kodi action for bash scripts is “system.exec”. That being said, although the Kodi keymap wiki would indicate that all three of these button mappings would produce the same notification…

~$ cat say_hello.sh
#! /bin/bash
kodi-send -a "Notification(Hello, World,5000)"

~$ cat say_hello.py
import os
os.system('kodi-send -a "Notification(Hello, World,5000)"')

~$ cat say_hello_from_sh.py
import os
os.system("bash /home/osmc/say_hello.sh")

~$ cat .kodi/userdata/keymaps/hello.xml
<keymap>
  <global>
    <keyboard>
      <p>System.Exec(/home/osmc/say_hello.sh)</p>
      <p mod="ctrl">runscript(/home/osmc/say_hello.py)</p>
      <x>runscript(/home/osmc/say_hello_from_sh.py)</x>
    </keyboard>
  </global>
</keymap>

Only the two being run though python actually worked. Searching the interwebs finds lots of examples of others trying to directly run bash scripts and not being able to get it to work. I tried everything I could think of as well, switching around file paths, permissions, etc., to no avail. Kodi would log that it told the script to run, but it never actually did.

1 Like

Thanks for this. Using the information you’ve both provided, I’ve managed to modify Joakim’s addon to stop openvpn and open another addon in one step - very cool! I’ve also managed to map this automation to a button press through keymaps.

The last piece of the puzzle would be to have openvpn automatically restart when I exit the addon (I’ve managed to map this automation to a button press but I really need it to happen automatically) but imagine that would involve modifying the addon in question - I’ll keep digging. Thanks both!

Hint: Kodi Callbacks

Might help you with, stuff triggering other stuff. Like:

Detecting when a portion of a string is written to the log onLogSimple and and onLogRegex require ‘matchIf’ strings.

When addon without vpn writes it’s closed to log, voila vpn-start addon gets triggered.
Anyway that’s the quick and dirty way I would solve it, but as I said zombieod vpn manager has some "on/off! per addon setting.

Good call - I actually just found that addon before you replied. Not sure how well maintained it is, but certainly looks like it could do the job.

I’ve used vpn manager before. Had issues with it disconnecting randomly when I used it a while back, so opted to use openvpn natively on osmc which has been flawless quite frankly. Also, vpn manager doesn’t recognise my particular addon as it isn’t a plug-in addon (it’s a script/service addon), so can’t trigger a connect/disconnect either side of using it (its connect/disconnect via Window ID option didn’t help in this particular case either).

I’d love to see osmc provide an addon to handle things like this, but I guess there’s not the demand for it, and more important core improvements to be working on. Thanks for the pointer! :pray:

Edit: Kodi Callbacks seems to be grinding the system to a halt, so not an option. Anyway, I’ll mark this thread as solved shortly. Thanks.

To bad, I used it for a short while back when I still had time to play with my X-windows “addon”. pre leia if i remember correctly.

I know there is a “sort of big wishlist” for MyOSMC2, but as you said the core stuff, OS upgrades, new Kodi-releases has a tendency to steal the “little dev time” there is.