Coder Perfect

How to copy data from a distant SSH session to the local clipboard

Problem

I’m not sure if this is a ServerFault question, but I’m working on some shell scripts, so I’ll start here:)

Most *nixes include a command that allows you to pipe/redirect output to the local clipboard/pasteboard and retrieve it. These commands are available on OS X.

pbcopy, pbpaste 

Is it possible to duplicate this capability while SSHed into a different server? That is to say,

Yes, I could just (shudder) use my mouse to pick the text from the command, but I’ve been so accustomed to the workflow of copying output to the clipboard that I want the same for my remote sessions.

Although code is useful, broader techniques are also appreciated.

Asked by Alan Storm

Solution #1

ssh [remote-machine] “cat log.txt” | xclip -selection c is my preferred method. When you don’t want to (or can’t) ssh from remote to local, this is the best option.

ssh [remote-machine] “cat log.txt” > /dev/clipboard on Cygwin

Edit: A helpful comment from nbren12:

Answered by Dominykas Mostauskis

Solution #2

This thread has been resurrected because I’ve been hunting for a similar solution and have found one that works for me. It’s only a little tweak to an OSX Daily proposal.

In my scenario, I connect to a linux server over SSH using Terminal on my local OSX system. I, too, wanted to be able to copy small chunks of text from the terminal to my local clipboard using only the keyboard.

The solution’s essence is as follows:

commandThatMakesOutput | ssh desktop pbcopy

This command, when run in an ssh session to a distant computer, takes the output of commandThatMakesOutput (e.g. ls, pwd) and streams it to the local computer’s clipboard (the name or IP of “desktop”). In other words, it employs nested ssh: you connect to the distant computer via one ssh session, run the command there, and the remote machine connects to your desktop via another ssh session, where the content is copied to your clipboard.

It needs that your desktop be set up as an ssh server (which I leave to you and google). It’ll be a lot easier if you’ve set up ssh keys to make quick ssh connections, preferably with a per-session passphrase, or whatever your security needs are.

Other examples:

ls  | ssh desktopIpAddress pbcopy
pwd |  ssh desktopIpAddress pbcopy

I’ve built a bash file to abbreviate the text required after the pipe for your convenience:

#!/bin/bash
ssh desktop pbcopy

In my case, I’m utilising a key with a unique name.

I named it cb (my mnemonic) and saved it (ClipBoard). Put the script on your path, make it executable, and there you have it:

ls | cb

Answered by rhileighalmgren

Solution #3

I discovered a fantastic technique that does not necessitate a reverse ssh connection!

On the OSX system, you can use xclip on the remote host, as well as ssh X11 forwarding and XQuartz.

To get started, follow these steps:

Answered by TrinitronX

Solution #4

All available solutions require one of the following:

There’s another way to do that, but you’ll have to change the way you ssh into your computer.

I’ve started using it, and it’s not quite as scary as it appears, so give it a shot.

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

(Hint: create a keybinding for this so you don’t have to type it)

nc -l 2000 | pbcopy

Note: If you don’t have pbcopy, tee it to a file instead.

cat some_useful_content.txt | nc localhost 2000

Actually, you can establish a tunnel even if you’re in the middle of an ssh session, but I don’t want to scare people away from what isn’t as horrible as it appears. But if there’s enough interest, I’ll add the information later.

Answered by Sridhar Sarnobat

Solution #5

X11 selections can be accessed via a variety of tools, including xclip and XSel. It’s worth noting that X11 offers several selections by default, and most programs are aware of both the clipboard and the primary selection (which are not the same). Emacs can also work with the secondary selection, but this is uncommon, because no one really understands what cut buffers are…

$ 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 brief, depending on what you want, 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.

Answered by ephemient

Post is based on https://stackoverflow.com/questions/1152362/how-to-send-data-to-local-clipboard-from-a-remote-ssh-session