Alternative to xsel or xclip *without* X11 installed?

11,439

Solution 1

The clipboard itself is a feature of X11, so you will not be able to use it without having any X11 server instance running anywhere. More about that below.

However, xclip/xsel don't have the X11 server as dependency, they only need some client libraries installed. You should install xsel though if you want to have as few packages as possible, as it has significantly less dependencies than xclip (compare the output of apt depends --recurse --important xsel and apt depends --recurse --important xclip).

Back to the X11 server. You said you don't want to install a full graphical X11 server stack on your headless Ubuntu server, which is not necessary. That requires you to have an X11 server running on your local client instead though, if you want to use its features like the clipboard.

On a Linux desktop client this is no problem, as you already have X11 running there usually, but on Windows you don't. Luckily there seems to be a small X11 server version for Windows too, called Xming.

Once you have that running and configured, you should be able to forward your local X11 session via SSH (using PuTTY) to your headless server. There you can then use e.g. xsel to interact with it and access your local clipboard.

More details about how to set up Xming, PuTTY and the SSH server for X11 forwarding can be found in these posts:

Solution 2

script logs the whole dialogue

The compiled program script is a program in the package bsdutils in the repository 'main' in Ubuntu. It saves the whole dialogue in a text screen, terminal window or remote connection. You can run script in your server.

The default name of the log file is typescript in the directory where you start script, but you can select another name if you start script with a parameter (alias argument)

script my-script.log

Demo example

Start script and run some commands:

sudodus@bionic64 ~/test $ LANG=C script my-script.log
Script started, file is my-script.log
sudodus@bionic64 ~/test $ echo 'Hello World'
Hello World
sudodus@bionic64 ~/test $ pwd
/home/sudodus/test
sudodus@bionic64 ~/test $ ls
my-script.log
sudodus@bionic64 ~/test $ exit
exit
Script done, file is my-script.log

Check the log file:

sudodus@bionic64 ~/test $ cat my-script.log 
Script started on 2019-01-21 19:50:22+0100
sudodus@bionic64 ~/test $ echo 'Hello World'
Hello World
sudodus@bionic64 ~/test $ pwd
/home/sudodus/test
sudodus@bionic64 ~/test $ ls
my-script.log
sudodus@bionic64 ~/test $ exit
exit

Script done on 2019-01-21 19:50:52+0100
sudodus@bionic64 ~/test $ 

Solution 3

If using a modified PuTTY is an option to you, you might want to look at this patch to PuTTY, which adds custom escape sequences to copy to and paste from Windows clipboard. It also ships a small winclip shell script that can be used to access the Windows clipboard from the target Linux/Unix host.

The patch is for PuTTY 0.53b (over ten years old), there's a rebase to PuTTY 0.60 here but that's still an old version. You might need some work to adapt it to latest PuTTY (0.70 at time of this writing) but it might just apply without any changes since PuTTY codebase has been mostly stable at this point. So perhaps this might be useful to you.

The patch is unlikely to be accepted to official PuTTY, since the escape sequences are custom and not really part of any standards, so unlikely to be officially supported by PuTTY in its official distribution.

Share:
11,439
I grok it
Author by

I grok it

Updated on September 18, 2022

Comments

  • I grok it
    I grok it over 1 year

    When I use putty from a Windows box to linux (without X11 installed), is there an alternative to xsel or xclip? xclip/xsel need X11. I would like to pipe the complete shell command output to the clipboard which I then paste on the Windows box to an application. I would like not to have to install all the X11 dependencies xsel and xclip need on those linux servers. Is this possible? Dan

  • dessert
    dessert over 5 years
    screen and tmux have copy-paste buffers: Does non-X Linux have a clipboard?
  • Byte Commander
    Byte Commander over 5 years
    @dessert I did not know that. From how I understand it though, those "buffers" are not really a system-wide or sharable or remotely accessible clipboard-equivalent, but just a local feature of that respective application, so I'm not sure if these would help to transfer data from a pipe on the server to the client clipboard, as described in the question. I'd guess not, unless you could explain that in a separate answer?
  • I grok it
    I grok it over 5 years
    I actually only want to mimic the windows/cmd feature provided by clip.exe on any linux server I access with ssh. c:\> dir | clip would put the output of dir into the copy buffer and I could use it anywhere to paste. usually I am using ubuntu and terminator, thus xclip/xsel work fine. But whn I am abroad, I access servers via ssh from a Windows box.
  • Byte Commander
    Byte Commander over 5 years
    Yeah, well, the difference is that clip.exe locally on Windows or xsel/xclip locally on an Ubuntu desktop are command-line clients that interact with their respective local GUI/input stack which provides the shared clipboard functionality on that system. Each alone works well, but they are not directly compatible with each other. X11 supports quite easy forwarding via ssh, which PuTTY can make use of, so if you have an X11 server and client on one end of the ssh connection each, you can access a remote clipboard. No magic involved. But remotely and cross-platform isn't that easy.