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

前端 未结 11 1350
别那么骄傲
别那么骄傲 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:48

    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.

提交回复
热议问题