A simple tool to copy data from a remote server back to your local machine's clipboard over SSH. Perfect for when you're working on a remote server and need to copy command output, file contents, or any text back to your local clipboard.
- π Copy data from remote server to local clipboard over SSH
- π¦ Handles large outputs with chunked data transmission
- π₯οΈ Cross-platform support (Linux, macOS, Android/Termux)
- π Can replace platform-specific clipboard tools (xclip, pbcopy) for cross-platform scripts
- π Secure transmission over existing SSH connection
- β‘ Fast and lightweight with minimal dependencies
The system consists of two Python scripts:
- Server (
clip_server.py): Runs on your local machine, receives data and sets your local clipboard - Client (
clip_copy.py): Runs on the remote server, sends data back to your local machine
Data is transmitted in chunks to handle large clipboard contents efficiently, using a length-prefixed protocol over TCP sockets tunneled through SSH.
Example workflow:
- Start
clip_server.pyon your local machine - SSH to remote server (with RemoteForward configured)
- On remote server:
echo "hello" | clip_copy.py - "hello" appears in your local clipboard!
- Python 3.6+
- Clipboard utilities:
- Linux (X11):
xclip - Linux (Wayland):
wl-clipboard - macOS:
pbcopy(built-in) - Android/Termux:
termux-api
- Linux (X11):
- Python 3.6+
- SSH access from your local machine
- Clone this repository on both your local machine and remote server:
git clone https://github.com/yourusername/ssh-clipboard-sync.git
cd ssh-clipboard-sync- On your local machine, copy the server script:
cp clip_server.py ~/.local/bin/
chmod +x ~/.local/bin/clip_server.py- On the remote server, copy the client script:
cp clip_copy.py ~/.local/bin/
chmod +x ~/.local/bin/clip_copy.pyAdd the following to your SSH client configuration (~/.ssh/config):
Host *
ForwardX11 yes
ForwardX11Trusted yes
RemoteForward 9997 localhost:9999Or for specific hosts:
Host myserver
HostName example.com
User myuser
ForwardX11 yes
ForwardX11Trusted yes
RemoteForward 9997 localhost:9999- Port 9999: Server listens on this port (local machine)
- Port 9997: Client connects to this port on remote server (tunneled back to local port 9999)
The SSH RemoteForward creates a tunnel so that when the remote server connects to its localhost:9997, it actually connects back to your local machine's port 9999.
- On your local machine, start the clipboard server:
python3 clip_server.py-
SSH to your remote server (the RemoteForward will be established automatically)
-
On the remote server, copy data to your local clipboard:
echo "Hello from the server!" | clip_copy.py
cat /var/log/syslog | clip_copy.py
ls -la | clip_copy.pyThe data will appear in your local machine's clipboard!
Add to your ~/.bashrc or ~/.zshrc on the remote server:
alias clip_copy='python3 ~/.local/bin/clip_copy.py'Usage:
cat important-file.txt | clip_copy
grep "error" /var/log/app.log | clip_copy
docker logs container-name | clip_copyAdd to your ~/.vimrc on the remote server:
" Send current line to local clipboard
nnoremap <leader>cl :.w !python3 ~/.local/bin/clip_copy.py<CR>
" Send visual selection to local clipboard
vnoremap <leader>cl :w !python3 ~/.local/bin/clip_copy.py<CR>
" Send entire file to local clipboard
nnoremap <leader>ca :%w !python3 ~/.local/bin/clip_copy.py<CR>[keys.normal.space]
y = ":pipe-to clip_copy"
One of the key benefits is using clip_copy.py as a universal clipboard tool in your scripts, replacing platform-specific commands:
Instead of:
# Platform-specific approach
if [[ "$OSTYPE" == "darwin"* ]]; then
echo "data" | pbcopy
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo "data" | xclip -selection clipboard
fiUse:
# Works on any remote server, sends to your local clipboard
echo "data" | clip_copyThis makes your scripts portable across different server environments while always sending output to your local machine where you can easily access it.
Port 9999 is already in use. Exiting.
Solution: Kill the existing process or change the port in both scripts.
Error sending data: [Errno 111] Connection refused
Solutions:
- Ensure the server script is running on the remote machine
- Check SSH port forwarding is working:
ssh -v yourhost(look for "Remote forwarding" messages) - Verify firewall settings allow the connection
Linux: Install required clipboard utilities:
# X11
sudo apt install xclip
# Wayland
sudo apt install wl-clipboardMissing X11 Forwarding: Ensure you have X11 forwarding enabled and working:
ssh -X yourhost
echo $DISPLAY # Should show something like localhost:10.0Add debug output by modifying the scripts to include verbose logging:
import logging
logging.basicConfig(level=logging.DEBUG)- All data is transmitted over your existing SSH connection, leveraging SSH's encryption
- The clipboard receiver only accepts connections from localhost (SSH tunnel)
- No data is stored persistently; everything stays in memory
- Consider the sensitivity of clipboard contents before using over untrusted networks
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Inspired by the need for seamless clipboard sharing in remote development workflows
- Thanks to the SSH port forwarding feature that makes this possible