Announcement

Collapse
No announcement yet.

Event to send ssh command?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Event to send ssh command?

    Hi! I'm looking for some help. I'm want an event to send a SSH command to a remote Raspberry PI. I already se up RSA and can do this via putty, so it works. I also installed putty-tools to get plink. Maybe something else? See below my event. I need to execute the following command from an event:

    ______________________________
    ssh pi@192.168.10.18 sudo reboot
    -----------------------------------------------------


    Here is my my information on the PI3 with HS3:

    Current Date/Time: 5/11/2017 8:08:09 PM
    HomeSeer Version: HS3 ZEE S2 Edition 3.0.0.318
    Linux version: Linux HomeTrollerZeeS2V2 4.1.19-v7+ #858 SMP Tue Mar 15 15:56:00 GMT 2016 armv7l GNU/Linux System Uptime: 0 Days 23 Hours 41 Minutes 49 Seconds
    IP Address: 192.168.10.16
    Number of Devices: 219
    Number of Events: 80
    Available Threads: 395

    Enabled Plug-Ins
    2.0.35.0: BLLAN
    3.0.0.68: HSTouch Server
    3.0.0.14: NetCAM
    3.0.1.87: Z-Wave
    Attached Files
    Last edited by jlawson05; May 11, 2017, 10:38 PM.

    #2
    https://the.earth.li/~sgtatham/putty...er7.html#plink

    looks like you are missing the leading "-" in your 1st parameter. should be
    With Parameters: -ssh pi@192.168.10.18 sudo reboot

    You will need some setup to avoid being prompted for a password. The manual page details how to do that.

    edit: looks like you can specify the password on the command line, i.e.
    -ssh pi@192.168.10.18 -pw password sudo reboot

    if you're not worried about the security.

    Comment


      #3
      Unfortunately that did not work either.
      I tried: -ssh pi@192.168.10.18 -pw chevydog3 sudo reboot

      This is is on a PI-HS3 box. I wonder if plink doesn't work on raspberry PI via HS3 events using "run program".? The command works when I SSH into the PI-HS3, and then issue a command to reboot the remote PI.

      Is there another way to execute a command via an event like you would in a SSH terminal?

      See below:
      Attached Files

      Comment


        #4
        What works for me is using sh and a script with the commands in it on an event line.

        sh 18Reboot.sh

        I also copy the SSH keys from Homeseer to the device so I do not need to use a password. It is a one device to one device key copy thing.

        Here is the script line to run commands remotely that I use.

        ssh root@homeseer1 "cat > /HomeSeer/Data/1-wire/temp.txt"

        ssh root@192.168.10.18 reboot or "reboot"

        Try it manually.

        put the above in a script and call it 18Reboot.sh and run it from Homeseer.

        You can make the script do multiple cameras with parameters if you want.
        Last edited by Pete; May 11, 2017, 11:34 PM.
        - Pete

        Auto mator
        Homeseer 3 Pro - 3.0.0.548 (Linux) - Ubuntu 18.04/W7e 64 bit Intel Haswell CPU 16Gb
        Homeseer Zee2 (Lite) - 3.0.0.548 (Linux) - Ubuntu 18.04/W7e - CherryTrail x5-Z8350 BeeLink 4Gb BT3 Pro
        HS4 Lite - Ubuntu 22.04 / Lenovo Tiny M900 / 32Gb Ram

        HS4 Pro - V4.1.18.1 - Ubuntu 22.04 / Lenova Tiny M900 / 32Gb Ram
        HSTouch on Intel tabletop tablets (Jogglers) - Asus AIO - Windows 11

        X10, UPB, Zigbee, ZWave and Wifi MQTT automation-Tasmota-Espurna. OmniPro 2, Russound zoned audio, Alexa, Cheaper RFID, W800 and Home Assistant

        Comment


          #5
          Originally posted by jlawson05 View Post
          Unfortunately that did not work either.
          I tried: -ssh pi@192.168.10.18 -pw chevydog3 sudo reboot

          This is is on a PI-HS3 box. I wonder if plink doesn't work on raspberry PI via HS3 events using "run program".? The command works when I SSH into the PI-HS3, and then issue a command to reboot the remote PI.

          Is there another way to execute a command via an event like you would in a SSH terminal?

          See below:
          I think the problem may be that HS3 is running as root, and root hasn't yet accepted the connection as ok. Try this:
          Code:
          sudo -i
          Now you should have a root command line. Try the putty command from here and see if it works. If I'm right, you will be prompted to accept the connection. Then try it again and hopefully you won't be prompted again.

          When done, type exit to return to user pi command line.
          Last edited by zwolfpack; May 12, 2017, 12:38 AM.

          Comment


            #6
            Thanks Guys. I could not get plink to execute the command but Pete's sh idea worked.

            I added a .sh file to HS3 and run the event "run program option." Now I can use events to reboot pi. In this case it will be to start OMXplayer to start my cameras remotely on the bedroom monitor sreen if an alarm goes off!

            Thanks! This worked great. The "sudo reboot" was the test command.

            Below is my sh file and the event setup.
            Attached Files

            Comment


              #7
              Originally posted by jlawson05 View Post

              This is is on a PI-HS3 box. I wonder if plink doesn't work on raspberry PI via HS3 events using "run program".?
              Just for clarification as you now have a ssh solution, plink is for windows platform only.
              Len


              HomeSeer Version: HS3 Pro Edition 3.0.0.435
              Linux version: Linux homeseer Ubuntu 16.04 x86_64
              Number of Devices: 633
              Number of Events: 773

              Enabled Plug-Ins
              2.0.54.0: BLBackup
              2.0.40.0: BLLAN
              3.0.0.48: EasyTrigger
              30.0.0.36: RFXCOM
              3.0.6.2: SDJ-Health
              3.0.0.87: weatherXML
              3.0.1.190: Z-Wave

              Comment


                #8
                Originally posted by lveatch View Post
                Just for clarification as you now have a ssh solution, plink is for windows platform only.
                plink is available for Linux as well - install the putty-tools package.

                Comment


                  #9
                  Originally posted by jlawson05 View Post
                  Thanks Guys. I could not get plink to execute the command but Pete's sh idea worked.

                  I added a .sh file to HS3 and run the event "run program option." Now I can use events to reboot pi. In this case it will be to start OMXplayer to start my cameras remotely on the bedroom monitor sreen if an alarm goes off!

                  Thanks! This worked great. The "sudo reboot" was the test command.

                  Below is my sh file and the event setup.
                  Cool - how did you get around the password prompt?

                  Comment


                    #10
                    Originally posted by zwolfpack View Post
                    Cool - how did you get around the password prompt?

                    I followed the passwordless instructions on raspberrypi.org:
                    https://www.raspberrypi.org/document...asswordless.md

                    It worked perfect.

                    Comment


                      #11
                      This is pretty cool. Currently, I run scripts from my HT windows 10 PC to reboot my network infrastructure every 60 days. I would like to revamp the way I run my automated reboots and do it using this method and BLLAN. I am close.

                      Currently, I have created my key using:

                      ssh-keygen
                      /home/<serviceaccount>/.ssh/id_rsa
                      <No pwd>
                      ssh-copy-id -i /home/<serviceaccount>/.ssh/id_rsa <usn>@<remoteserver>
                      enter pwd for<usn>@<remoteserver>


                      -From terminal i created a new sh file using:

                      nano /home/homeadmin/hs3/HomeSeer/CustomShell/rebootnetadmin.sh


                      #!/bin/bash
                      #
                      ssh <usn>@<remoteserver> sudo reboot



                      -then save the file (ctrl 0) and exit nano (ctrl x)


                      -at this point i needed to add the execute command to

                      the new sh file.



                      chmod +x /home/homeadmin/hs3/HomeSeer/CustomShell/rebootnetadmin.sh


                      I can run "./rebootnetadmin.sh" from shell terminal and it does reboot the remote system.



                      I had a little trouble with my BLLAN plugin as it was giving me an error message saying something about "not being set to an instance of an application" i disabled and re-enabled the plugin and it did start working again. I then noticed that the remote system I am testing on appears to reboot before BLLAN detects it as down. on the BLLAN device in question, I had to adjust ping failures to 1 and ping interval to 5 seconds because the system that I am currently testing with reboots in 10 seconds.



                      Once I have the events working i will configure the event trigger to run every Sunday and have a condition that the BLLAN device have an uptime of greater than 50 days.



                      I am, currently, stuck attempting to get the sh script to run from the event. I have attempted to set it to run another program and set it to run "/bin/sh" and then added the command parameters that point to the file "/home/homeadmin/hs3/HomeSeer/CustomShell/rebootnetadmin.sh". it does not seem to work. Any ideas on what I am doing wrong?

                      Comment


                        #12
                        Originally posted by Kerat View Post
                        I am, currently, stuck attempting to get the sh script to run from the event. I have attempted to set it to run another program and set it to run "/bin/sh" and then added the command parameters that point to the file "/home/homeadmin/hs3/HomeSeer/CustomShell/rebootnetadmin.sh". it does not seem to work. Any ideas on what I am doing wrong?
                        Your script is a bash script (#!/bin/bash) but you're invoking it with /bin/sh. The two have to match.

                        Comment


                          #13
                          Looks like once you get the keys set up, you can eliminate the script and invoke ssh directly. Try command
                          ssh <usn>@<remoteserver> sudo reboot
                          If that works, setup the event with program /usr/bin/ssh and parameters <usn>@<remoteserver> sudo reboot

                          Comment


                            #14
                            Nice, so I have tried removing:

                            #!/bin/bash
                            #

                            From the .sh file before. It does work from terminal but it does not seem to work when:
                            1. calling my .sh script directly from the event action.
                            2. Calling /bin/sh and using my .sh script as a parameter.
                            3. Calling /usr/bin/ssh and using my .sh script as a parameter.

                            It may be worthwhile to have the event action output to a log file so I can get a better idea as to why it isn't working.


                            Sent from my iPhone using Tapatalk

                            Comment


                              #15
                              You might try /bin/bash instead of /bin/sh, since bash is the primary Linux shell. Shouldn't matter but worth a try.

                              To capture ssh error output to a file, try changing the script to

                              #!/bin/sh (<== or /bin/bash)
                              /usr/bin/ssh <usn>@<remoteserver> sudo reboot 2> /tmp/ssh.errout

                              When the script executes, the file /tmp/ssh.errout will be created. It will be empty unless error messages are generated.

                              I'm thinking that since HS3 is run under user root, that the private keys for ssh aren't being properly provided to the ssh command. When you are testing, are you running as root or as a normal user?

                              Perhaps try adding identity option to ssh: -i /home/<user>/.ssh/id_rsa

                              event program /usr/bin/ssh and parameters <usn>@<remoteserver> -i /home/<user>/.ssh/id_rsa sudo reboot

                              Comment

                              Working...
                              X