Netcat: A Simple Way to Transfer Text over SSH"
Netcat, invoked by the nc
command, is often called the "Swiss Army knife" of network tools due to its versatility. Today, I'll show how to use netcat to transfer text from a remote machine to your local clipboard over SSH.
The Basics of Netcat Listen Mode
On your local machine, run:
nc -l 8377
This sets up netcat in listen mode on port 8377. In this mode, netcat:
- Acts as a simple TCP server
- Accepts a single client connection
- Outputs received data to stdout
- Exits after the connection closes (use -k option to keep the connection open)
This allows us to pipe the received data to clipboard utilities like pbcopy (macOS) or xclip (Linux).
Aside: want to understand pipes better? Check out this post.
Setting Up the Remote-to-Local Pipeline
- On your local machine:
nc -l 8377 | pbcopy # Use xclip instead of pbcopy on Linux
- Set up SSH with port forwarding:
ssh -R 8377:localhost:8377 user@remote-host
- On the remote machine, send data:
echo -n "Hello, Netcat!" | nc localhost 8377
This creates a simple, one-time use "channel" for sending a message from the remote machine to your local clipboard.
How It Works
- The SSH command forwards the remote port 8377 to your local port 8377.
- When the remote script sends data to its localhost:8377, it's forwarded through SSH to your local port 8377.
- Your local netcat listener receives the data and pipes it to your clipboard utility.
Security Note
While convenient, this method transmits data in plain text. It's suitable for non-sensitive information, but exercise caution with confidential data.
This simple technique demonstrates the flexibility of netcat and SSH port forwarding, opening up numerous possibilities for creative network solutions.
© Mike Surowiec