Using Python to run remote shell script

Posted on
Wed Jun 10, 2020 12:38 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Using Python to run remote shell script

I'm running an app on a remote Mac that has to be manually set to a certain state before it can quit, and the remote Mac can shut down. In discussions with the apps creator, its not possible in any way to automate this changing of state, so I've managed to cobble together a shell script to simply kill the running app. I can manually run that script via Terminal from the Indigo server, but that requires my entering a password.

So what I'm hoping for is a way to do this in Indigo, by either a python script that can be run on the Indigo server that can kill the app on the remote Mac, or by a python script that can execute the shell script on the remote Mac.

Don't know if this is even possible given the apparent need to enter the user password from Indigo. Any ideas?

As always, thanks in advance.

Posted on
Wed Jun 10, 2020 1:18 pm
FlyingDiver offline
User avatar
Posts: 7211
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Using Python to run remote shell script

If you use ssh with Public Key Authentication, then you don't need to enter your password to log into the remote system. You just create the keys and store them on both computers. You can also use rsh to execute the remote command easily.

https://serverpilot.io/docs/how-to-use- ... ntication/

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Jun 10, 2020 3:21 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Using Python to run remote shell script

Actually, I was on that exact webpage, but couldn't get the first command to run at all on the target server. It was on Sierra, so don't know if that was the problem.

However, now I'm mostly there.
1. From the Indigo sever, I've created the keys
2. I've successfully copied the key to the target server
3. I can log in from the Terminal in Indigo w/o a password
Code: Select all
ssh 'qlc@192.168.5.110'

4. Once that is done, I can run commands directly from the terminal on the Indigo server to the target server
Code: Select all
pkill -9 "Safari"

5, And from the terminal in Indigo, can run a remote script stored on the target server
Code: Select all
./Documents/killsafari.sh


My problem is constructing the shell script for Indigo to run. I've tried this
Code: Select all
c#!/bin/bash
ssh 'qlc@192.168.5.110'
./Documents/killsafari.sh
but when I try to run from the terminal on the Indigo server, nothing happens, much less trying this from Indigo. I'm guessing the script needs something extra to make it run but it is similar to the scripts on the target server which I can run locally... I did chmod on the file, but am half way remembering I probably need something different than 700.

Any suggestions ?

Posted on
Wed Jun 10, 2020 3:29 pm
FlyingDiver offline
User avatar
Posts: 7211
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Using Python to run remote shell script

From Terminal on the client, try:

Code: Select all
ssh qlc@192.168.5.110 './Documents/killsafari.sh'


Then if that works, create a shell script file:

Code: Select all
#!/bin/bash
ssh qlc@192.168.5.110 './Documents/killsafari.sh'


And execute that from an Indigo action group or trigger.
Last edited by FlyingDiver on Wed Jun 10, 2020 8:25 pm, edited 1 time in total.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Jun 10, 2020 6:36 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Using Python to run remote shell script

So this was a huge help. Can't thank you enough.

I'll spare you all the gory details, but as you can image, Safari wasn't the target app. Turns out the app has one name in the finder, another name in the Activity Monitor, and yet a 3rd name in the Package Contents. After I got that figure out, the rest was bordering on easy.

Originally I had the app and a file open via Automator at start up, but that was causing some issues with the "kill" command... gave me an error message as it seems Automator was still running, which may have cause scheduled shutdown issues..(didn't get that far). But because this worked so well, I now have Indigo sending the command to a different shell script to open the file and the app at the scheduled time.

I could NOT have done this without your patient help. I am grateful.

For anyone else trying to make this work, the first step is to create the shell script on the target computer, make sure it runs successfully and does what you want, then set up the SSH Public Key Authentication from Indigo, and then create a shell script on the Indigo Server that points to your script on the target server as a Scheduled event, the action of a Trigger, or as an Action Group. If I can do it, I'm sure anyone can!

Posted on
Sat Jul 18, 2020 11:42 am
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Using Python to run remote shell script

So NOW the question is how do I get Python to run a local shell script? I can easily have an Action Group run the shell script, and then have Python run the Action Group, but is there a way to have Python run the shell script directly?

Of course, I'll have the shell script as a .sh file on the local computer, but am curious if there is a way to have Python execute that directly..

Posted on
Sat Jul 18, 2020 11:50 am
FlyingDiver offline
User avatar
Posts: 7211
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Using Python to run remote shell script


joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Jul 19, 2020 9:59 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Using Python to run remote shell script

I had actually looked at that page, but didn't try any of the examples... but did try this and it worked just fine
Code: Select all
import subprocess
subprocess.call(['/Users/bradford/Desktop/MusicStudioOpen.sh'])

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests