Beginner to scripting, how to "pass link as variable and execute ssh + wget"

I consume almost all my media through podcasts and audiobooks. I host a private podcast through a shared server, so now I can use ‘wget’ to send any media i find online to the server, and then listen later on podcast. It’s great.

ssh -p '21098' 'admin@server.com' cd /home/admin/public_html/Path/to/Media wget [input media url here]

But at the moment I’m copying the link I find, opening the terminal, finding the script saved in another document, copying that to terminal, etc…

I have Keyboard Maestro, but it keeps giving error “Pseudo-terminal will not be allocated because stdin is not a terminal” when I try to use the execute script action there.

Adding -t -t doesn’t do much, it just hangs.

How would you set this up?
Ideally, I would have a bookmarklet or a share-to extension in Safari so when I see the link I push one button and this all happens in the background. Next best would be copying the link, and then executing the script with a shortcut through KM.

I know Shortcuts does this well, it works on my iphone, but I’m not ready to upgrade from Catalina on my mac :grimacing:

Thanks for any help!

Several things here

  1. Do you have ssh set up so that you do not have to enter a password when you connect? If not, you’ll have to enter your password every time, which is going to get tedious.

  2. Rather than a separate cd step, you can use -P or --directory-prefix= like this

wget -P '/home/admin/public_html/Path/to/Media'  

or

wget --directory-prefix='/home/admin/public_html/Path/to/Media'  

doing this over ssh requires layers of quoting, so you want to do something like this:

ssh user@server.com ' wget -P "public_html/path/to/media/" "https://URL.here" '

Note that the single quotes wrap the entire command, and the double quotes wrap the other items.

Here’s a basic example of how you could pass a variable from Keyboard Maestro to a script to be executed on another machine over SSH (assuming you are using password-less access with SSH keys).

In this example I am taking some text input in Keyboard Maestro (defaults to ls -al $HOME - a command to give a detailed directory listing of my home directory). This is utilised int he next step, not as stdin, but by direct insertion as a variable. To make things easier to follow, I’ve actually broken it down into a function and the script is passing the variable as a parameter to that function.

The script exectutes an SSH connection to a machine on my local network (called styx, with my user account specified for the connection. The content of strCommand is passed as a command to the SSH session. That variable simply specifies to output some starting text, the passed in instruction (default is the home directory listing noted above), and then an ending string.

The output is displayed in a Keyboard Maestro window - it is an example after all.

When run, the default input results in a directory listing for the remote machine.

For your particular use case, you can swap in your own instructions, including the wget for the example strCommand content, and the input from the Keyboard Maestro variable, you want to get set as the URL you want to grab the content of.

As you noted, you could do this by copying the URL - then passing in the clipboard content to the shell script. You could do that from Keyboard Maestro, or from pbpaste.

It might also be possible to use Automator to create a Quick Action/Service to process the URL from with a similar approach. I’m not sure if it’s easy for Automator to get the link details or not. As a result, I’d probably go with the Keyboard Maestro + URL on clipboard approach. However, you can also automate the copying to the clipboard with Keyboard Maestro.

In this macro (created for use in Safari (15.1)), I right click at the current pointer position, wait for a moment for the context menu to appear, then press “c” to select the “Copy Link” item and “return” to execute it. There’s a pause for the clipboard to update, and then the final step, just for illustration purposes, displays the URL on the clipboard.

I tied this to a hot key so when the mouse pointer is over a link I could trigger it. For your use, once happy with how it works, you would drop the final step and replace it with your script step taking in the clipboard. Using the suggested simplification from @Tjluoma, and some appropriate use of additional quotation marks, I think that you would have something of this format.

#!/bin/zsh

ssh stephen@styx.local 'wget -P "public_html/path/to/media/" "'$(pbpaste)'"'

The combined result I think should give you a keyboard shortcut triggered (or Stream Deck, etc. as you wish) Keyboard Maestro macro that would trigger the copy of the URL to the clipboard and pass that off to an SSH-key enabled SSH connection tot he remote machine, and initiate the process there.

You may also wish to consider options such as running triggering the command and disconnecting (a la nohup), or having the process self contained with, for example, a monitored iCloud folder (Automator folder action, Keyboard Maestro Folder changed trigger, Hazel (the uber tool for such things)), and a download file that is used to specify the download and save to locations. That might give you a single consistent process regardless of trigger location or action.

Hope that helps.