Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@blurayne
Last active August 3, 2022 07:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save blurayne/3f4426a25d565074e949e25d2f48a8cd to your computer and use it in GitHub Desktop.
Save blurayne/3f4426a25d565074e949e25d2f48a8cd to your computer and use it in GitHub Desktop.
SSH Remote Forwarded Clipboard
##
# SSH Remote Forwarded Clipboard
#
# Advantages
# You can copy stuff on a remote machine to your local clipboard (e.g. you could use the script in a shell theme)
# It is not intended to do it the other way round (makes no sense since you usually will use your local clipboard)
#
# Security Concerns
# Someone else on the remote host could spam or exploit your clipboard exploit it (xsel, xclipbaord, parcellite etc.)
#
# Requiremnts:
# xsel has to be present (package xsel on most systems)
# functions of this script added to your favorite shell
# Your SSH config should look like:
# Host *
# RemoteForward 8787 localhost:8787
#
##
# Remote Forward Clipboard Wrapper for Pasting to SSH Client via port 8787
# - put this function in local rc file
# - put this function in remote rc file
#
# Uses nc, local-file cache and &>/dev/tcp/127.0.0.1/8787 as fallback
#
cb() {
local clipFile
local isRemote=0
local buffer=""
if [[ -n "${SSH_CONNECTION:-}" ]]; then
clipFile="/tmp/clip-$(echo -n ${SSH_CONNECTION%% *})"
isRemote=1
fi
if [ ! -t 0 ]; then
# copy
IFS= buffer="$(cat /dev/stdin)"
if [[ "$isRemote" -eq 1 ]] || ! xset q &>/dev/null; then
IFS= command echo -n "$buffer" > "$clipFile"
if [[ "$isRemote" -eq 1 ]]; then
if which nc 1>/dev/null 2>&1; then
IFS= command echo -n "$buffer" | nc localhost 8787
else
IFS= command echo -n $buffer &>/dev/tcp/127.0.0.1/8787
fi
fi
elif which xsel 1>/dev/null 2>&1; then
IFS= command echo -n "$buffer" | xsel -ib
elif which xclip 1>/dev/null 2>&1; then
IFS= command echo -n "$buffer" | xclip -selection c
fi
else
if [[ "$isRemote" -eq 1 ]] || ! xset q &>/dev/null; then
command cat "$clipFile"
elif which xsel 1>/dev/null 2>&1; then
xsel -ob
elif which xclip 1>/dev/null 2>&1; then
xclip -selection c -o
fi
fi
}
##
# Clipboard server for our X-Desktop
# - put this function in local rc file
#
# Just listens to port 8787 on localhost and forwards everything to your system clipboard
#
cb-server() {
while :; do nc localhost -l 8787 | cb || break; done;
}
##
# SSH Wrapper function to ensure clipboard-server is running
# - put this function in local rc file
#
# Note: Some ZSH config may leave jobs running in background!
# - use &! to disown and run forever
# - http://zsh.sourceforge.net/Doc/Release/Options.html#Description-of-Options
ssh() {
local opts=()
if [[ -n "${ZSH_VERSION:-}" ]]; then
opts=($(setopt))
setopt nonotify nomonitor
fi
cb-server 1>/dev/null 2>&1 &
sleep 0.1
if [[ -n "${ZSH_VERSION:-}" ]]; then
setopt "${opts[@]}"
fi
command ssh "$@"
# cb-server will run as long a shell is active
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment