Drafts via Workflow or Shortcuts to Raspberry PI - via SSH

I’d like to drive my Raspberry Pi via SSH from Drafts.

I already have a trivial ls -l SSH workflow from Workflow.

I’d like to drive it from Drafts, though. (Workflow / Shortcuts is just an enabler in this.)

Can someone tell me how to get Workflow to return something from (eg) the SSH action to Drafts. I would guess it’s using x-success in x-callback-url. But I see a couple of problems:

  1. How to get the output returned from an SSH session into something useful - like a variable - in Workflow.
  2. How to return an answer to Drafts via x-success.

My guess to the answer to 1 is that the SSH action automatically outputs the output from the session. Thus the Set Variable action can store it - as usual.

My guess for question 2 is that x-success - via the callbackResponse property of the of CallbackURL object - will contain the answer and I just need somehow to get the data out of Workflow from the variable.

I can probably work my way through all this but just hoped someone had been there ahead of me. Of course, if I do figure it out I’ll update this thread.

1 Like
  1. Call the workflow using the Workflow action step in Drafts and set it to wait for response.
  2. Unless you want to produce any intermediate processing within Workflow, ensure the Run Script Over SSH action in the workflow is the last step. Whatever you want returned to Drafts simply needs to be the standard output of the workflow.
  3. Use [[workflow_result]] as the template tag in your Drafts action to output the return from Workflow.

With the Workflow/Shortcut action you need to make sure “wait for result” is enabled in Drafts; and the output of the workflow (if the SSH action is the last one, that will be whatever it output), is available via the [[workflow_result]] shortcode in Drafts (e.g. in an insert text action or in a script). Does that help?

1 Like

Thanks. I don’t think I would’ve guessed workflow_result. :slight_smile:

So, yes it does. It will be interesting to see what a full SSH session (with more than one command) produces as output. Then parsing that in Drafts will also be an interesting exercise :slight_smile: beyond the scope (maybe) of my question.

There is documentation for each of the action steps here (that’s a link to the Workflow one) - lots of things have return parameters you can dig into!

Related to this, Panic’s Prompt app has URL schemes so that prompt-ssh:// can be used to drive SSH (and telnet). The point of this comment, apart from that, is to note that the URL scheme doesn’t allow you to specify auto connect. So it opens the app ready for you to press Connect.

There might be a good reason for this, but it stops automation dead in its tracks.

(I discovered this URL scheme from Launch Center Pro, by the way.)

Ah, I’d forgotten about that and was thinking of doing Workflow the hard way. :slight_smile: No need, perhaps, for JavaScript at all. I say “perhaps” as previous automations soon ran into the need for more control.

Note that the Workflow action is running a script not a command. All of the output from the commands in the script are returned. If you run multiple instances of the Workflow call then these will be separate connections being opened and closed.

In the main I keep my scripts on the machine rather than on iOS as then I can execute them from any client I like. If there are common scripts then I replicate them between machines. As a result I tend to perhaps use a cd then the relevant script execution command; rather than lots of multiline script commands on iOS.

2 Likes

I agree. I’d like to encapsulate logic in scripts as much as possible. But sometimes some interaction is required - which would cause a round trip.

Talking of round trips, as a Performance guy, I’d like to minimise them. Whether they involve actual network interactions or just crossing app boundaries.

So any eventual “Production” application would need to get this right.

So here’s a javascript function I rustled up (and added to my library).

	baseURL="workflow://x-callback-url/run-workflow"
	var cb=CallbackURL.create()
	cb.baseURL=baseURL
	cb.addParameter("name",workflowName)
	cb.addParameter("input",input)
	var success=cb.open()
	if(success){
		return(cb.callbackResponse["result"])
	}
}

As its name suggests it can run an arbitrary Workflow workflow, with text that is passed into the first step.

I successfully used this with a single-step SSH workflow - into my Raspberry Pi.

This method works quite but here’s a limitation I haven’t worked around…

I tried the following command:

sudo apt upgrade

but it wants the user to answer a Y/n question. I’ve yet to find a way of supplying the answer. And still worse would be trying to decide the answer based on the output of the SSH session.

But still, it’s progress: I can talk to my Pi from Drafts and indeed created a “take the current draft text as a set of commands and send it via SSH to the Pi and replace the draft text with the result of these commands” action.

Drafts Shell (dshell) anyone? :slight_smile:

Try this: sudo apt upgrade -y

1 Like

Thank you. It does, of course, presuppose I know the answer to the question.

I wonder if there’s a way to suspend and resume an SSH session.

You have to start some sort of persistent background session and then access that from your SSH sesson.

You could perhaps use screen (Man Page - Bash - SS64.com) for that. I think you would be okay with the SSH disconnecting as terminating the screen connection, but if not there should be a way to add a clean exit scipt/command or rebind via the .screenrc file to something easier to trigger programmatically. I’ve only used it with a directly interactive client.

Hope that helps.

1 Like

Not sure if this helps, but seems related, I’ve tinkered with this on my phone with some success, I was able to reboot a remote box with it.

https://www.infrageeks.com/project/ssh-oneshot

1 Like

I just downloaded the Lite version. (The full is not terribly expensive, though.) It worked quite well - but note it’s an iPhone app, not a iPad one - so not for landscape orientation.

Actually I used VNC to reboot my Pi last night and it didn’t come back. Partly because it failed to reconnect to the same WiFi but also because the VNC app on the Pi furnished a pop up I couldn’t see.

So, careful with remotely rebooting anything.

1 Like