send ssh command via script possible?

Post Reply
sailmich
Posts: 56
Joined: Wednesday 17 February 2016 23:55
Target OS: Raspberry Pi
Domoticz version:
Contact:

send ssh command via script possible?

Post by sailmich » Monday 19 December 2016 19:29

Hi, I have a question.
With a virtual switch in domoticz I would like to switch a camera on another Raspberry.
With command in a terminal ssh pi@x.x.x.x "echo 'motion_enable on' > /home/pi/pikrellcam/www/FIFO" I can enable motion and with off disable.
I tried to integrate the command into event lua script without success.I got unexpected symbol '@' near pi. Is it possible to use ssh in event or do I have to use a seperate script?
Any help would fine,
Thx and regards Mike

SweetPants
Posts: 2293
Joined: Friday 12 July 2013 21:24
Target OS: Linux
Domoticz version: V4.10069
Location: The Netherlands
Contact:

Re: send ssh command via script possible?

Post by SweetPants » Monday 19 December 2016 21:30

Use a separate bash script which you call from LUA (os.execute) maybe?
Ubuntu 16.04.5 LTS, Intel NUC, MySensors 2.3.0-alpha, Pro Mini 3.3/8M RFM69(HW), ESP8266 (SDK2.2.0), Sonoff, RFLink, RFXcom, MQTT, NodeRed, Domoticz Beta (from source)

User avatar
cyberclwn
Posts: 124
Joined: Thursday 20 August 2015 22:53
Target OS: Raspberry Pi
Domoticz version: beta
Location: The Netherlands
Contact:

Re: send ssh command via script possible?

Post by cyberclwn » Monday 19 December 2016 22:34

Hey,

If you use a switch, can't you use the "On Action" field?
I use that to turn on/off the screen of another pi (which i use as a PhotoFrame.)

The setup is like

Code: Select all

script:///home/pi/start_fotolijstje.sh
the script contains:

Code: Select all

runuser -l pi -c "ssh -n -f <user>@<ip or hostname> '~/start_slideshow.sh > /dev/null 2>&1 &'"
(it has to run with user "pi" because i have setup trusted ssh-login, so no password required)
And i have another for the turning off part.

That script then calls another script on the "receiving" pi called start_slideshow.sh.
I have done this because that script also uses the "and that messes up the first script.

But i use for another script, that doesn't use much "or ' this:

Code: Select all

runuser -l pi -c "ssh -n -f <user>@<ip or hostname> 'irsend SEND_ONCE something something'"
Hope that helps
3xPi 2B (Domoticz "live", Domoticz "sandbox", PhotoFrame)
RFXCom433(E), KaKu, Oregon Scientific, Keyes 2-relay, Logitech Media Server, MiLight, Smartwares heating controller(2x), IR Send/Receive, Keyes PIR, XH-M131 DuskSensor, DHT22/11

User avatar
Egregius
Posts: 2733
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: send ssh command via script possible?

Post by Egregius » Monday 19 December 2016 23:13

Why not just do a curl of the command to pikrellcam?

Code: Select all

http://192.168.2.11/fifo_command.php?cmd=motion_enable%20on
http://192.168.2.11/fifo_command.php?cmd=motion_enable%20off
You can also trigger a record, in case motion detect didn't work or something.
I have this when someone presses the doorbell:

Code: Select all

http://192.168.2.11/fifo_command.php?cmd=record%20on%205%2055
wich records a movie 5 seconds before the doorbell and 55 seconds after it.

And to answer to your original question:
Best way to go is enable login without password, with a certificate. Then you can use that certificate to login and send your commands

This is a command that is in my backup script.

Code: Select all

ssh user@remote.nas.be -p 1234 -i /root/.ssh/certificate "rm -f /volume1/homes/Guy/backup/domoticz/__prev; ln -s /volume1/homes/Guy/backup/domoticz/$NOW /volume1/homes/Guy/backup/domoticz/__prev;

sailmich
Posts: 56
Joined: Wednesday 17 February 2016 23:55
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: send ssh command via script possible?

Post by sailmich » Tuesday 20 December 2016 18:24

I took egregius code and put it direct in domoticz into my virtual switch. It works but each time when I change the switch I got an error in domoticz.
Error: Error opening url: http://192.x.x.x/fifo_command.php?cmd=m ... nable%20on
or Error: Error opening url: http://192.x.x.x/fifo_command.php?cmd=m ... able%20off
the other way. :?:
Does anyone know why?
Thx

User avatar
Egregius
Posts: 2733
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: send ssh command via script possible?

Post by Egregius » Tuesday 20 December 2016 19:06

Maybe because there's nothing in the response? Don't know. I don't use on/off actions, they're to slow.

sailmich
Posts: 56
Joined: Wednesday 17 February 2016 23:55
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: send ssh command via script possible?

Post by sailmich » Tuesday 20 December 2016 20:12

@egregius With blockly, some virtual switches, real switches and sensors and a keypad I built a simple alarm system. By arm the system with the keypad I turn motion enable on pikrellcam. I already took your code from pikrellcam to turn a virtual switch in Domoticz. If motion detected and alarm armed I will get an email. I don't want to create video files all-day long, I need them just when nobody is at home. My experience in writing scripts is nearly zero. That's the reason why I always try to keep it as simple as possible. I read a lot in forums to find something which is similar to my problems. Sometimes I'm lucky to find a solution. And this time I asked here and got the right answer from you. Thank you very much and also to all the others who taking time to answer questions of a noob.
What do you mean with maybe there's nothing in the response?
Regards Mike

User avatar
Egregius
Posts: 2733
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: send ssh command via script possible?

Post by Egregius » Tuesday 20 December 2016 20:34

If you open that url in a browser the command is accepted, but not one character is returned. Maybe Domoticz expect something in return.
You could edit the fifo_command.php file in pikrellcam and add something simple like this:

Code: Select all

<?php
require_once(dirname(__FILE__) . '/config.php');
$fifo = fopen(FIFO_FILE,"w");
fwrite($fifo, $_GET["cmd"]);
fclose($fifo);
echo 'ok';
 
Then the page will return a simple 'ok' and maybe that's enough to get rid of the error.

sailmich
Posts: 56
Joined: Wednesday 17 February 2016 23:55
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: send ssh command via script possible?

Post by sailmich » Tuesday 20 December 2016 22:13

I put your code in fifo_command.php, the domoticz switch didn't change pikrellcam anymore. And I wasn't able to reboot pikrellcam with the reboot button in UI. I deleted your code and am able to enable/disable motion and get no error messages anymore :?: . If it stay like this I'm happy even if it is strange.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests