How to send data to local clipboard from a remote SSH session

前端 未结 11 1347
别那么骄傲
别那么骄傲 2020-12-07 07:29

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/red

相关标签:
11条回答
  • 2020-12-07 07:51

    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"
    0 讨论(0)
  • 2020-12-07 07:51

    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

    0 讨论(0)
  • 2020-12-07 07:51

    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.

    0 讨论(0)
  • 2020-12-07 07:57

    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.

    0 讨论(0)
  • 2020-12-07 07:58

    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.

    0 讨论(0)
提交回复
热议问题