Options for Pasting STDOUT to clipboard on a remote host

339 Views Asked by At

I have a very simple script aliased in my .bashrc on a remote host that I frequently SSH into for work. It enables me to enter "pth" as a command, and that aliases to "bash /user/your/home/pathprinter.sh", which is as follows:

#!/bin/bash

NOWDIR=`pwd`
echo 'scp  [email protected]:'"$NOWDIR" 

This enables me to fairly quickly scp a file from my local machine to the remote host or vice versa without having to type very much, because it echos the output, which I then click and drag to highlight, then copy and paste into another terminal window on my local machine.

If this script were stored on my local machine, I could write:

NOWDIR=`pwd`
echo 'scp  [email protected]:'"$NOWDIR" | pbcopy

and it would go straight to the clipboard. But if I most frequently am on the remote host when executing this command, and if I do not have sudo powers on remote host (I can still install things on a home directory or elsewhere, however) are there still ways to accomplish this? Anything to save a few seconds!

1

There are 1 best solutions below

0
Alvaro Gutierrez Perez On

Instead of echoing the command, the script could run it, so you do not need to copy paste anything.

You can have two scripts, one to copy from remote to local

#!/bin/bash
remote_path="${1:?Missing remote path}"
local_path="${2:?Missing local path}"
scp "[email protected]:$remote_path" "$local_path"

and another to copy from local to remote

#!/bin/bash
local_path="${1:?Missing local path}"
remote_path="${2:?Missing remote path}"
scp "$local_path" "[email protected]:$remote_path"

You can execute them as

copy-remote-to-local <path copied from the other terminal> .
copy-local-to-remote file.txt <whatever path you want on remote system>

Or, even better, configure ssh to have a short name for that host

Host serv
HostName remote.host.institution.name
User myname

and then you can use directly scp like this

scp serv:<remote path> <local path>
scp <local path> serv:<remote path>

With this, if you have public key login with that host, the remote path will be completed when you press tab, just as if it was a local path!