Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Here's my usage case:

  • I'm often connected to other computers over SSH for work and I often need to copy and paste documents/text from the server to locally running editors for writing examples and sharing text.
  • Often, if the text is small enough, I'll simply copy the output from my terminal program (gnome-terminal at the moment) and paste it.
  • However, when it comes to entire documents, my options are quite limited. I can either copy the document chunk-by-chunk, or scp it to the local machine.

Is there a way to use a program such as xclip which will allow me to copy remote stdin to the local X server's clipboard? Something to the effect of:

cat myconffile.conf | sed {...} | copy-over-ssh-to-local-clipboard

would be awesome. Does something exist to make this possible?

share|improve this question

6 Answers 6

up vote 8 down vote accepted

If you run ssh with X forwarding, this is transparent: remote commands (including xclip) have access to your X server (including its keyboard). Make sure you have ForwardX11 yes in your ~/.ssh/config and X11Forwarding yes in the server sshd_config (depending on your distributions, these options may be on or off by default).

<myconffile.conf sed {...} | xclip -i

There are other ways of working on remote files that may be more convenient, for example mounting remote directories on your local machine with SSHfs, or opening remote files in Emacs with Tramp. If you have ssh and FUSE set up and SSHfs installed, SSHfs is as easy as mkdir ~/net/myserver; sshfs myserver:/ ~/net/myserver. If you have ssh set up and Emacs installed, Tramp is as easy as opening /myserver:/path/to/file.

share|improve this answer
1  
Any security vulnerabilities to running things this way by default in the conf file? –  Naftuli Tzvi Kay Jul 15 '11 at 1:15
    
No. All the x11 traffic is forwarded through the SSH channel. –  Shadur Jul 15 '11 at 4:09
1  
@TKKocheran Not really. On the server side, none. On the client side, a little, in that it gives the server administrator a way into your client, but in 99% of cases the server admin has a way anyway; see If someone is signed into SSH on my computer, can I access their computer? –  Gilles Jul 15 '11 at 9:33

You don't need anything special; since xclip works over stdin, just

ssh remotehost xclip < myconf.conf

Why would it need to be modified with sed? ssh is transparent to data when not used as a terminal, and is commonly used in pipelines such as

tar cfz - somedir | ssh remotehost 'cd destdir; tar xfz -'`
share|improve this answer
    
Is there a way to do it from within an existing SSH session? I'd really prefer not to have to open up another terminal window if I already have one open to the machine in question. –  Naftuli Tzvi Kay Jul 14 '11 at 23:56
    
And sed` is just there as an example, if I'd like to perform any processing on the stdin before copying it to the clipboard, I'd like that option. –  Naftuli Tzvi Kay Jul 14 '11 at 23:57
    
No, there's no way to inject data into an existing ssh session (it wouldn't be very secure if you could). You can set up the ssh ControlMaster stuff to multiplex additional connections over an existing one, but it's not really something for beginners and you still do it the same way afterward, by switching to another local terminal and running the command above (which does not open a new terminal window). –  geekosaur Jul 15 '11 at 0:03

Here's how you would do it on OSX from your local machine

ssh remotehost.com "<some/file.txt" | pbcopy
share|improve this answer

I have similar issue with LUbuntu's lxterminal (tested various terminals) pasting clipboard to an ssh session. If clipboard has more than about 100 bytes the session timeouts and fails.

If I connect via ssh to almost any CentOS 5.x server and then connect to the target server, clipboard paste works without any issue for any reasonable data size.

share|improve this answer

if your local machine is running windows(7+) you can just use this from the CommandLine:

ssh user@server cat /home/user/file | clip
share|improve this answer

https://secure.wikimedia.org/wikipedia/en/wiki/Base64

You can convert you're clipboard data with Base64 to ASCII text and then you can push that through in an already existing SSH connection.

share|improve this answer
    
Why convert to anything? It's already text in this case and even if it was binary there would be no reason you couldn't send it across a pipeline like this. –  Caleb Aug 2 '11 at 10:41
    
clipboard could contain binary data, e.g.: a compiled application –  LanceBaynes Aug 2 '11 at 14:34
    
Yes it could, but why would that be a problem? Binary data can be transferred through a pipe and ssh tunnel. –  Caleb Aug 2 '11 at 14:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.