Skip to content

Instantly share code, notes, and snippets.

@si9ma
Last active February 27, 2019 12:43
Show Gist options
  • Save si9ma/80b1c6d8ac2557417d428c6f12ccc2f8 to your computer and use it in GitHub Desktop.
Save si9ma/80b1c6d8ac2557417d428c6f12ccc2f8 to your computer and use it in GitHub Desktop.
send data to local clipboard from a remote SSH session
export SSH_FORWARD_PORT=2222
export SSH_CB_CONF='.ssh.cb.client.user' # ssh clipboard configuration
# ssh wrapper
ssh(){
# must -t, in order to run zsh again
# refer : https://unix.stackexchange.com/questions/119894/single-command-to-login-to-ssh-and-run-program
# 这里有BUG,多客户端登录服务器的时候,配置会被覆盖
# TODO 修复BUG
[ "`uname`" = "Darwin" ] && /usr/bin/env ssh -t "$@" -R ${SSH_FORWARD_PORT}:localhost:22 "echo $USER > \$HOME/$SSH_CB_CONF && zsh" && exit # just work on macos
/usr/bin/env ssh $@
}
# copy file to client clipboard
# refer : https://stackoverflow.com/questions/1152362/how-to-send-data-to-local-clipboard-from-a-remote-ssh-session
cb() {
client_user=`cat $HOME/$SSH_CB_CONF`
ssh-copy-id $client_user@localhost -p $SSH_FORWARD_PORT > /dev/null 2>&1
ssh $client_user@localhost -p $SSH_FORWARD_PORT pbcopy > /dev/null 2>&1
}
## demo
ssh root@remote
cat main.go | cb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment