Start openvpnclient on router via OpenHAB rule when I turn on Netflix

I have a Logitech Harmony Hub (HH) and OpenHAB has learnt this as a Thing. One of the states of the HH is “Netflix”.
I want to create a rule that will execute a script to turn on the openvpn client on my router when OpenHAB detects the HH is in the Netflix state. So far I have only the very simple bones of a script but it is not working. Could anyone offer any advice?

sshpass -p 'passwd' ssh admin@'remote_ip' service start_vpnclient2

So I’ve taken 1 step closer to getting this to work but I still need some help.
I have created a bash script called StartVPN.sh and saved this on a host. This is indeed able to Turn ON the openvpnclients on my router when I run it from the remote host machine. I have shared the SSH Authentication keys so that SSH password is not required when the script is run.
My StartVPN.sh bash script is:

#! /bin/bash
ssh password@host service start-vpnclient2

My next step is to save StartVPN.sh on my Raspberrypi running Openhab2 and then somehow create a Rule with an Action that will run this file.
My problem is the Rules part of PaperUI only allows commands to be written in Javascript. Can anyone help my write a simple Javascript command to execute a bash script stored on the same machine??

First off, exposing your router’s root password is asking for trouble and to have your system comprised and used in another attack. Please use an ssh key that is bound to a single command and parameters on the router, likely one for “on” and another key for “off” (Implementation will depend on your router’s ssh server, information for openssh available at https://www.ssh.com/ssh/authorized_keys/openssh – You’d want command= as well as turning just about everything else off.)

On the scripting, JavaScript intentionally can’t reach anything outside of the browser sandbox. I’d look at writing them in .rules file.

Thanks jeffsf. Totally agree regarding exposure of password, so I am now using ssh authentication keys to negate the password issue. Although I’ve implemented this for all ssh sessions between my OH2 machine and my router, not sure how to make this specific to a particular command…

I had some more progress last night making use of the ExecCommandLine function. I am using it in a rules script to execute a .sh file which initiates a ssh session with my router and turns ON the VPN. The .sh script works when it is run from terminal. Im trying to execute this file from a rules script. I’m using the experimental rules addon of PaperUI, but I assume this is actually editing a .rules file in the background similar to how you suggest, however I cant find the location of where it actually saves the .rules file (the /etc/openhab2/rules folder is empty)

Bad news is it is not actually working yet. I’ve read that the Exec Binding is required which I’ve now downloaded but still not working. rlkoshakRich seems to think that if Exec Binding is needed, it may need v1.x rather than 2.0.0 as currently provided by PaperUI addon. Does this seem plausible? If so how can I get v1.x? My latest rules action script is below:

> executeCommandLine(" ./etc/openhab2/scripts/startATVPN.sh")

and my startATVPN.sh looks like this:

#! /bin/bash
ssh admin@192.168.1.1 service start_vpnclient2

The bad news is, it doesn’t work. I don’t know why, but I think I’m close :wink:

On setting up a single command associated with a key, there is plenty on the Internet describing the use of the command= parameter in the authorized_keys file of OpenSSH. I don’t know what your router is running for an SSH server, so it may be different. The basic idea is that you generate two key pairs. The first is bound to, for example, /usr/local/bin/openvpn_up and the second is bound to /usr/local/bin/openvpn_down Neither script accepts parameters.

As for the limitations of PaperUI, they can be great. You are probably better off writing your rules in a .rules file since the “friendly” UIs are pretty weak at much more than basic configuration.

The exec binding has documentation at http://docs.openhab.org/addons/bindings/exec/readme.html

You can add a logging statement to your rule to see if it is being executed at all, if you don’t know that from the logs already. Is there anything in the logs that indicate what the failure is? I would hope that the 2.0 binding is functional. If it isn’t log a bug against it.

My rule was inserted into PaperUI and the action module of the script is as follows

executeCommandLine(" ./etc/openhab2/scripts/startATVPN.sh")

When I look into the logs I do indeed see an error:

2017-04-02 23:38:45.950 [ERROR] [internal.handler.ScriptActionHandler] - Script execution failed: ReferenceError: “executeCommandLine” is not defined in at line number 1

I’ll look into the command= parameter, and also editing the .rules file. Any idea where the PaperUI would save the .rule file? its not in /etc/openhab2/rules…
Thanks.

Items, Things, Rules,… created in PaperUI or HABmin are not visible in “files” per se. You can write the rule in a file ending with .rules and put it in the /etc/openhab2/rules/ folder, assuming you’re using a deb installed version of openHAB.

It looks like the problem is that executeCommandLine is not understood. That line looks very different than the documentation for the binding linked above, which looks like you need to define a Thing associated with the command, then poke its run Channel to run it.