225

Borderline ServerFault question, but I'm programming some shell scripts, so I'm trying here first :)

Most *nixes have a command that will let you pipe/redirect output to the local clipboard/pasteboard, and retrieve from same. On OS X these commands are

pbcopy, pbpaste 

Is there anyway to replicate this functionality while SSHed into another server? That is,

  1. I'm using Computer A.
  2. I open a terminal window
  3. I SSH to Computer B
  4. I run a command on Computer B
  5. The output of Computer B is redirected or automatically copied to Computer A's clipboard.

And yes, I know I could just (shudder) use my mouse to select the text from the command, but I've gotten so used to the workflow of pipping output directly to the clipboard that I want the same for my remote sessions.

Code is useful, but general approaches are appreciated as well.

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • I answered a similar question [here](https://unix.stackexchange.com/a/673164/209677), alike to [@TrinitronX](https://stackoverflow.com/a/15962516/4970442) one but without XQuartz, on Linux. – Pablo Bianchi Oct 14 '21 at 07:03

15 Answers15

166

My favorite way is ssh [remote-machine] "cat log.txt" | xclip -selection c. This is most useful when you don't want to (or can't) ssh from remote to local.

Edit: on Cygwin ssh [remote-machine] "cat log.txt" > /dev/clipboard.

Edit: A helpful comment from nbren12:

It is almost always possible to setup a reverse ssh connection using SSH port forwarding. Just add RemoteForward 127.0.0.1:2222 127.0.0.1:22 to the server's entry in your local .ssh/config, and then execute ssh -p 2222 127.0.0.1 on the remote machine, which will then redirect the connection to the local machine. – nbren12

Dominykas Mostauskis
  • 7,797
  • 3
  • 48
  • 67
  • 6
    +1: So much easier than other solutions when a reverse SSH connection isn't possible! – John Dibling Jan 10 '14 at 18:09
  • 9
    You deserve more likes for that extremely simple solution! – Kamil Dziedzic Apr 17 '14 at 12:06
  • 43
    For those of us on Mac OSX, `ssh [remote-machine] "cat log.txt" | pbcopy` – chowey May 06 '14 at 01:43
  • 1
    As per this report [here](https://cygwin.com/ml/cygwin/2003-08/msg01400.html), I had to use `cat` to get this to work on cygwin Windows 8: `ssh [remote-machine] "cat log.txt" | cat > /dev/clipboard` (of course, my remote command wasn't `cat`; it was something else inside a vagrant virtual machine so I actually ran `vagrant ssh -c "command" | cat > /dev/clipboard`) – tiby Jan 09 '16 at 19:35
  • Top answer, better then reverse ssh. – Sumit Jain Feb 11 '16 at 14:40
  • 8
    It is _almost_ always possible to setup a reverse ssh connection using SSH port forwarding. Just add `RemoteForward 127.0.0.1:2222 127.0.0.1:22` to the servers entry in your local `.ssh/config`, and then execute `ssh -p 2222 127.0.0.1` on the remote machine, which will then redirect the connection to the local machine. – nbren12 Feb 16 '16 at 16:55
  • awesome! this is great. just note that the log.txt is the file on the remote host, i am unsure if you can specify the file path but most likely – Dap Aug 26 '16 at 20:51
  • But what to do when the file is very deep like inside two nested sshs which require passwords and then inside a user account which again require a password ? – Bharat Oct 03 '19 at 06:05
  • 1
    On WSL: `ssh [remote-machine] "cat log.txt" | clip.exe` – Jamy Mahabier Apr 06 '20 at 15:53
  • Added: `cb.zerkon() { ssh zerkon "$1" | xclip -selection zerkon -in }` to my .zshrc file after seeing this, where zerkon is a host I commonly ssh into. – oxagast Mar 30 '23 at 11:01
105

I'm resurrecting this thread because I've been looking for the same kind of solution, and I've found one that works for me. It's a minor modification to a suggestion from OSX Daily.

In my case, I use Terminal on my local OSX machine to connect to a linux server via SSH. Like the OP, I wanted to be able to transfer small bits of text from terminal to my local clipboard, using only the keyboard.

The essence of the solution:

commandThatMakesOutput | ssh desktop pbcopy

When run in an ssh session to a remote computer, this command takes the output of commandThatMakesOutput (e.g. ls, pwd) and pipes the output to the clipboard of the local computer (the name or IP of "desktop"). In other words, it uses nested ssh: you're connected to the remote computer via one ssh session, you execute the command there, and the remote computer connects to your desktop via a different ssh session and puts the text to your clipboard.

It requires your desktop to be configured as an ssh server (which I leave to you and google). It's much easier if you've set up ssh keys to facilitate fast ssh usage, preferably using a per-session passphrase, or whatever your security needs require.

Other examples:

ls  | ssh desktopIpAddress pbcopy
pwd | ssh desktopIpAddress pbcopy

For convenience, I've created a bash file to shorten the text required after the pipe:

#!/bin/bash
ssh desktop pbcopy

In my case, i'm using a specially named key

I saved it with the file name cb (my mnemonic (ClipBoard). Put the script somewhere in your path, make it executable and voila:

ls | cb
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
rhileighalmgren
  • 1,186
  • 1
  • 8
  • 4
  • 4
    I often to this in vim. To do so, select what you want to copy in visual mode, then type: `:'<,'>w !ssh desktop pbcopy` – Mike Brennan Nov 30 '12 at 05:04
  • @MikeBrennan When you have the text selected, why don't you just press cmd+C (or whatever copy key you have set)? ... – Petr Peller Jun 27 '13 at 09:28
  • 51
    Like this it only really works when your desktop has a static IP and is reachable from the box you ssh'd into. That's never the case for me. – the Aug 07 '14 at 21:36
  • 8
    Wouldn't the other way around be easier? ssh user@remote 'commandThatMakesOutput' |pbcopy – rulio Sep 04 '14 at 14:19
  • I needed the `-e none` ssh flag described here: https://unix.stackexchange.com/questions/210615/how-to-copy-file-contents-to-the-local-clipboard-from-a-file-in-a-remote-machine – Pat Myron Jun 19 '18 at 01:03
  • In case you want to use it on Windows, (I know the OP didn't ask), you can also use `commandThatMakesOutput | clip` (eg; `ssh -t root@$host "cat /your/file" | clip`). – Christian Jul 25 '18 at 15:32
  • It works for me perfectly. I suggest ssh'ing from your client machine to host machine first, before doing `| ssh desktop pbcopy`. – Alan Jun 24 '19 at 05:30
  • For anyone having trouble getting this to work on MacOS, you may need to use `reattach-to-user-namespace pbcopy` instead of just `pbcopy`. https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard – n8henrie Aug 17 '19 at 19:13
33

Found a great solution that doesn't require a reverse ssh connection!

You can use xclip on the remote host, along with ssh X11 forwarding & XQuartz on the OSX system.

To set this up:

  1. Install XQuartz (I did this with soloist + pivotal_workstation::xquartz recipe, but you don't have to)
  2. Run XQuartz.app
  3. Open XQuartz Preferences (kb_command+,)
  4. Make sure "Enable Syncing" and "Update Pasteboard when CLIPBOARD changes" are checked XQuartz Preferences window example
  5. ssh -X remote-host "echo 'hello from remote-host' | xclip -selection clipboard"
TrinitronX
  • 4,959
  • 3
  • 39
  • 66
  • 1
    How is this any different from `ssh remote-host "echo 'hello from remote-host' | pbcopy`? This would be an excellent answer if it would work something like this: `ssh -X remote-host` then on the remote host: `echo 'hello from remote-host' | xclip -selection clipboard` – the Aug 07 '14 at 21:42
  • 6
    @KasperSouren. I believe it does work how you are hoping. The ` | xclip` is inside the double quotes. – gdw2 Apr 20 '15 at 16:31
  • 1
    In 2016, this is obviously THE best, no-frill solution. Forget other, reverse-ssh based solutions. – shivams Jun 25 '16 at 03:01
  • 1
    Is there a way to do this w/o it just being a one-off command? I want to be logged into my remote server in an interactive session over ssh and then be able to send to my local clipboard arbitrarily. Does that require reverse ssh? – Kvass Jan 05 '17 at 01:46
  • 3
    @Kvass: As long as you're using `-X` for X11 Forwarding over SSH, X11 programs will be able to update your clipboard via the XQuartz settings above. The one-off command is to demonstrate that sending something via `echo` to the remote host's clipboard via `xclip` does work & should sync to your local SSH client host. Note as @gdw2 says, "The `| xclip` is inside the double quotes". As such, it will execute as a command on the remote server to send something to it's clipboard. – TrinitronX Jan 05 '17 at 07:57
  • This solution works, but XQuartz *hammers* the CPU. Burns a lot of extra power all the time for the occasional benefit of copying to the clipboard. – Br.Bill Sep 10 '21 at 22:22
  • 1
    @Br.Bill Yes, of course, the X11 Forwarding + XQuartz setup is providing more than just clipboard sync capability. (_Hint for readers_: Try running an X11 program like `xeyes` on the remote system) This could be desired or not, depending on your use case. If you don't require full X11 client + server support, and just want a no-frills local LAN network-based clipboard you might try [`remotestash`](https://github.com/roznet/remotestash) instead. – TrinitronX Jan 28 '22 at 21:29
31

Reverse tunnel port on ssh server

All the existing solutions either need:

  • X11 on the client (if you have it, xclip on the server works great) or
  • the client and server to be in the same network (which is not the case if you're at work trying to access your home computer).

Here's another way to do it, though you'll need to modify how you ssh into your computer.

I've started using this and it's nowhere near as intimidating as it looks so give it a try.

Client (ssh session startup)

ssh username@server.com -R 2000:localhost:2000

(hint: make this a keybinding so you don't have to type it)

Client (another tab)

nc -l 2000 | pbcopy

Note: if you don't have pbcopy then just tee it to a file.

Server (inside SSH session)

cat some_useful_content.txt | nc localhost 2000

Other notes

Actually even if you're in the middle of an ssh session there's a way to start a tunnel but i don’t want to scare people away from what really isn’t as bad as it looks. But I'll add the details later if I see any interest

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
  • 3
    +1 -- that's pretty neat. However -- the `cat some_useful_content.txt | nc localhost 2000` command seems to hang when I do this. I need to manually ctr-c to stop it, and it's only then that the content gets to my client clip/pasteboard – Alana Storm Apr 18 '18 at 15:57
  • 1
    It will wait until someone dequeues the data at the other end. – Sridhar Sarnobat Apr 18 '18 at 16:27
  • Actually I think I misunderstood your question. I'm not sure why that happens. – Sridhar Sarnobat May 02 '18 at 17:53
  • `nc -l -p` works for me, but just `nc -l` does not. And I have the problem of @AlanStorm, too. – HappyFace Sep 16 '18 at 21:31
  • 1
    I solved the problem of hanging netcat by specifying `-c`. I use GNU netcat installed by brew. – HappyFace Sep 16 '18 at 22:06
  • Was recently trying and using pbcopy worked fine for me `ssh "cat " | pbcopy ` – Pramit Nov 08 '18 at 22:42
  • Like I said, you can’t do that if you’re at work sshing into your home computer. – Sridhar Sarnobat Nov 24 '18 at 23:07
  • 2
    Thank you! I was able to automate most of this in my `~/.ssh/config`! Just add these lines under the host:`RemoteForward 2000 localhost:2000 \n LocalCommand /bin/bash -c 'nc -z localhost 2000 && echo "pasteboard port 2000 listener already active" || while true; do nc -l localhost 2000 | pbcopy; echo updated_pasteboard; done' & \n PermitLocalCommand yes`. That starts the port forwarding automatically when you ssh to the host and runs the listener in the background. The listener never exits by itself but that seems okay and it checks to prevent duplicates listeners. – Chris Nov 26 '21 at 19:42
  • what if there is no `nc` on the remote? – bloody Apr 07 '23 at 18:32
10

If you use iTerm2 on the Mac, there is an easier way. This functionality is built into iTerm2's Shell Integration capabilities via the it2copy command:

Usage: it2copy
          Copies to clipboard from standard input
       it2copy filename
          Copies to clipboard from file

To make it work, choose iTerm2-->Install Shell Integration menu item while logged into the remote host, to install it to your own account. Once that is done, you'll have access to it2copy, as well as a bunch of other aliased commands that provide cool functionality.

The other solutions here are good workarounds but this one is so painless in comparison.

Br.Bill
  • 637
  • 5
  • 12
9

There are various tools to access X11 selections, including xclip and XSel. Note that X11 traditionally has multiple selections, and most programs have some understanding of both the clipboard and primary selection (which are not the same). Emacs can work with the secondary selection too, but that's rare, and nobody really knows what to do with cut buffers...

$ xclip -help
Usage: xclip [OPTION] [FILE]...
Access an X server selection for reading or writing.

  -i, -in          read text into X selection from standard input or files
                   (default)
  -o, -out         prints the selection to standard out (generally for
                   piping to a file or program)
  -l, -loops       number of selection requests to wait for before exiting
  -d, -display     X display to connect to (eg localhost:0")
  -h, -help        usage information
      -selection   selection to access ("primary", "secondary", "clipboard" or "buffer-cut")
      -noutf8      don't treat text as utf-8, use old unicode
      -version     version information
      -silent      errors only, run in background (default)
      -quiet       run in foreground, show what's happening
      -verbose     running commentary

Report bugs to <astrand@lysator.liu.se>
$ xsel -help
Usage: xsel [options]
Manipulate the X selection.

By default the current selection is output and not modified if both
standard input and standard output are terminals (ttys).  Otherwise,
the current selection is output if standard output is not a terminal
(tty), and the selection is set from standard input if standard input
is not a terminal (tty). If any input or output options are given then
the program behaves only in the requested mode.

If both input and output is required then the previous selection is
output before being replaced by the contents of standard input.

Input options
  -a, --append          Append standard input to the selection
  -f, --follow          Append to selection as standard input grows
  -i, --input           Read standard input into the selection

Output options
  -o, --output          Write the selection to standard output

Action options
  -c, --clear           Clear the selection
  -d, --delete          Request that the selection be cleared and that
                        the application owning it delete its contents

Selection options
  -p, --primary         Operate on the PRIMARY selection (default)
  -s, --secondary       Operate on the SECONDARY selection
  -b, --clipboard       Operate on the CLIPBOARD selection

  -k, --keep            Do not modify the selections, but make the PRIMARY
                        and SECONDARY selections persist even after the
                        programs they were selected in exit.
  -x, --exchange        Exchange the PRIMARY and SECONDARY selections

X options
  --display displayname
                        Specify the connection to the X server
  -t ms, --selectionTimeout ms
                        Specify the timeout in milliseconds within which the
                        selection must be retrieved. A value of 0 (zero)
                        specifies no timeout (default)

Miscellaneous options
  -l, --logfile         Specify file to log errors to when detached.
  -n, --nodetach        Do not detach from the controlling terminal. Without
                        this option, xsel will fork to become a background
                        process in input, exchange and keep modes.

  -h, --help            Display this help and exit
  -v, --verbose         Print informative messages
  --version             Output version information and exit

Please report bugs to <conrad@vergenet.net>.

In short, you should try xclip -i/xclip -o or xclip -i -sel clip/xclip -o -sel clip or xsel -i/xsel -o or xsel -i -b/xsel -o -b, depending on what you want.

ephemient
  • 198,619
  • 38
  • 280
  • 391
  • 7
    To add to @ephemient 's post: In order to use xclip & xsel with a remote host, you can use X11 Forwarding like so: `ssh -C -X user@remote-host`. If running xclip on the remote side doesn't seem to be working, make sure `X11Forwarding yes` is set in the remote `/etc/ssh/sshd_config` file. Also make sure `xauth` is installed on the remote side. For a headless machine, you can install `xauth` and `xvfb`. – TrinitronX May 09 '11 at 01:11
  • Here's a [description](https://defuse.ca/blog/2012/04/clipboard-over-ssh-with-vim/) with extra details plus vim key bindings. – Tim Bunce Oct 15 '12 at 13:32
  • 1
    @TrinitronX oh wow - banging my head against a wall, thanks for the `xvfb` mention. I was running a headless server, so X11 must not have been running! – Cam Jul 08 '16 at 02:41
8

This is my solution based on SSH reverse tunnel, netcat and xclip.

First create script (eg. clipboard-daemon.sh) on your workstation:

#!/bin/bash
HOST=127.0.0.1
PORT=3333

NUM=`netstat -tlpn 2>/dev/null | grep -c " ${HOST}:${PORT} "`
if [ $NUM -gt 0 ]; then
    exit
fi

while [ true ]; do
    nc -l ${HOST} ${PORT} | xclip -selection clipboard
done

and start it in background.

./clipboard-daemon.sh&

It will start nc piping output to xclip and respawning process after receiving portion of data

Then start ssh connection to remote host:

ssh user@host -R127.0.0.1:3333:127.0.0.1:3333

While logged in on remote box, try this:

echo "this is test" >/dev/tcp/127.0.0.1/3333

then try paste on your workstation

You can of course write wrapper script that starts clipboard-daemon.sh first and then ssh session. This is how it works for me. Enjoy.

6

Allow me to add a solution that if I'm not mistaken was not suggested before.

It does not require the client to be exposed to the internet (no reverse connections), nor does it use any xlibs on the server and is implemented completely using ssh's own capabilities (no 3rd party bins)

It involves:

  1. Opening a connection to the remote host, then creating a fifo file on it and waiting on that fifo in parallel (same actual TCP connection for everything).
  2. Anything you echo to that fifo file ends up in your local clipboard.
  3. When the session is done, remove the fifo file on the server and cleanly terminate the connections together.

The solution utilizes ssh's ControlMaster functionality to use just one TCP connection for everything so it will even support hosts that require a password to login and prompt you for it just once.

Edit: as requested, the code itself:

Paste the following into your bashrc and use sshx host to connect.

On the remote machine echo SOMETHING > ~/clip and hopefully, SOMETHING will end up in the local host's clipboard.

You will need the xclip utility on your local host.

_dt_term_socket_ssh() {
    ssh -oControlPath=$1 -O exit DUMMY_HOST
}
function sshx {
    local t=$(mktemp -u --tmpdir ssh.sock.XXXXXXXXXX)
    local f="~/clip"
    ssh -f -oControlMaster=yes -oControlPath=$t $@ tail\ -f\ /dev/null || return 1
    ssh -S$t DUMMY_HOST "bash -c 'if ! [ -p $f ]; then mkfifo $f; fi'" \
        || { _dt_term_socket_ssh $t; return 1; }
    (
    set -e
    set -o pipefail
    while [ 1 ]; do
        ssh -S$t -tt DUMMY_HOST "cat $f" 2>/dev/null | xclip -selection clipboard
    done &
    )
    ssh -S$t DUMMY_HOST \
        || { _dt_term_socket_ssh $t; return 1; }
    ssh -S$t DUMMY_HOST "rm $f"
    _dt_term_socket_ssh $t
}

More detailed explanation is on my website:

https://xicod.com/2021/02/09/clipboard-over-ssh.html

xicod
  • 91
  • 1
  • 2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/28307525) – radrow Feb 12 '21 at 17:53
  • @radrow thanks for the comment, I updated the answer – xicod Feb 12 '21 at 19:29
5

The simplest solution of all, if you're on OS X using Terminal and you've been ssh'ing around in a remote server and wish to grab the results of a text file or a log or a csv, simply:

1) Cmd-K to clear the output of the terminal

2) cat <filename> to display the contents of the file

3) Cmd-S to save the Terminal Output

You'll have the manually remove the first line and last line of the file, but this method is a bit simpler than relying on other packages to be installed, "reverse tunnels" and trying to have a static IP, etc.

tw0000
  • 475
  • 1
  • 7
  • 13
  • +1 for a useful technique I've used myself, bit it does force you to lose your terminal history and (as mentioned) the first/last line problem is a little fiddly. – Alana Storm Jul 17 '18 at 15:57
  • Agreed @AlanStorm – tw0000 Jul 17 '18 at 17:05
  • 1
    The more significant problem with this approach is that it will only save the number of lines that your terminal buffer retains. Often that’s not a problem but if you’re trying to grab the contents of a log file then you may run out of capacity, – Sridhar Sarnobat Nov 09 '18 at 17:11
  • That's true @Sridhar-Sarnobat , it's not a very good solution in general. I just use `scp`! – tw0000 Nov 28 '18 at 21:09
  • Replace step 3 with `Cmd-A` (select-all) and `Cmd-C` to copy to clipboard. At first, I think `Cmd-A` will just select visible screen's text, but it does select all instead. – Johnny Wong Oct 05 '21 at 05:07
4

This answer develops both upon the chosen answer by adding more security.

That answer discussed the general form

<command that makes output> | \
    ssh <user A>@<host A> <command that maps stdin to clipboard>

Where security may be lacking is in the ssh permissions allowing <user B> on host B> to ssh into host A and execute any command.

Of course B to A access may already be gated by an ssh key, and it may even have a password. But another layer of security can restrict the scope of allowable commands that B can execute on A, e.g. so that rm -rf / cannot be called. (This is especially important when the ssh key doesn't have a password.)

Fortunately, ssh has a built-in feature called command restriction or forced command. See ssh.com, or this serverfault.com question.

The solution below shows the general form solution along with ssh command restriction enforced.

Example Solution with command restriction added

This security enhanced solution follows the general form - the call from the ssh session on host-B is simply:

cat <file> | ssh <user-A>@<host A> to_clipboard

The rest of this shows the setup to get that to work.

Setup of ssh command restriction

Suppose the user account on B is user-B, and B has an ssh key id-clip, that has been created in the usual way (ssh-keygen).

Then in user-A's ssh directory there is a file

/home/user-A/.ssh/authorized_keys

that recognizes the key id-clip and allows ssh connection.

Usually the contents of each line authorized_keys is exactly the public key being authorized, e.g., the contents of id-clip.pub.

However, to enforce command restriction that public key content is prepended (on the same line) by the command to be executed.
In our case:

command="/home/user-A/.ssh/allowed-commands.sh id-clip",no-agent-forwarding,no-port-forwarding,no-user-rc,no-x11-forwarding,no-pty <content of file id-clip.pub>

The designated command "/home/user-A/.ssh/allowed-commands.sh id-clip", and only that designated command, is executed whenever key id-clip is used initiate an ssh connection to host-A - no matter what command is written the ssh command line.

The command indicates a script file allowed-commands.sh, and the contents of that that script file is

#/bin/bash
#
# You can have only one forced command in ~/.ssh/authorized_keys. Use this
# wrapper to allow several commands.

Id=${1}

case "$SSH_ORIGINAL_COMMAND" in
    "to-clipboard")
          notify-send "ssh to-clipboard, from ${Id}"
        cat | xsel --display :0 -i -b
          ;;
    *)
        echo "Access denied"
        exit 1
        ;;
esac

The original call to ssh on machine B was

... | ssh <user-A>@<host A> to_clipboard

The string to-clipboard is passed to allowed-commands.sh by the environment variable SSH_ORIGINAL_COMMAND. Addition, we have passed the name of the key, id-clip, from the line in authorized_keyswhich is only accessed by id-clip.

The line

          notify-send "ssh to-clipboard, from ${Id}"

is just a popup messagebox to let you know the clipboard is being written - that's probably a good security feature too. (notify-send works on Ubuntu 18.04, maybe not others).

In the line

cat | xsel --display :0 -i -b

the parameter --display :0 is necessary because the process doesn't have it's own X display with a clipboard, so it must be specificied explicitly. This value :0 happens to work on Ubuntu 18.04 with Wayland window server. On other setups it might not work. For a standard X server this answer might help.

host-A /etc/ssh/sshd_config parameters

Finally a few parameters in /etc/ssh/sshd_config on host A that should be set to ensure permission to connect, and permission to use ssh-key only without password:

PubkeyAuthentication yes
PasswordAuthentication no
ChallengeResponseAuthentication no
AllowUsers user-A

To make the sshd server re-read the config

sudo systemctl restart sshd.service

or

sudo service sshd.service restart

conclusion

It's some effort to set it up, but other functions besides to-clipboard can be constructed in parallel the same framework.

Craig Hicks
  • 2,199
  • 20
  • 35
  • 1
    The Xsel with remoteforwarding was the only answer that worked for me on Ubuntu 18.04 running i3 locally. No X11 forwarding needed. – jay Jun 18 '19 at 20:55
3

Not a one-liner, but requires no extra ssh.

  • install netcat if necessary
  • use termbin: cat ~/some_file.txt | nc termbin.com 9999. This will copy the output to the termbin website and prints the URL to your output.
  • visit that url from your computer, you get your output

Of course, do not use it for sensitive content.

maxbellec
  • 16,093
  • 10
  • 36
  • 43
3

For anyone googling their way to this: The best solution in this day and age seem to be lemonade

Various solutions is also mentioned in the neovim help text for clipboard-tool

TheMadsen
  • 196
  • 1
  • 11
2

@rhileighalmgren solution is good, but pbcopy will annoyingly copy last "\n" character, I use "head" to strip out last character to prevent this:

#!/bin/bash
head -c -1 |  ssh desktop pbcopy

My full solution is here : http://taylor.woodstitch.com/linux/copy-local-clipboard-remote-ssh-server/

Taylor Hawkes
  • 641
  • 7
  • 10
  • `pbcopy` is not meant to make judgments about what it copies, so it makes sense that it includes everything you give it. – Br.Bill Sep 10 '21 at 22:24
2

Far Manager Linux port supports synchronizing clipboard between local and remote host. You just open local far2l, do "ssh somehost" inside, run remote far2l in that ssh session and get remote far2l working with your local clipboard.

It supports Linux, *BSD and OS X; I made a special putty build to utilize this functionality from windows also.

unxed
  • 196
  • 1
  • 3
  • 6
1

If you're working over e.g. a pod in a Kubernetes cluster and not direct SSH, so that there is no way for your to do a file transfer, you could use cat and then save the terminal output as text. For example in macOS you can do Shell -> Export as text.

Sina Madani
  • 1,246
  • 3
  • 15
  • 27