Home

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:

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

  1. On your local machine:
nc -l 8377 | pbcopy  # Use xclip instead of pbcopy on Linux
  1. Set up SSH with port forwarding:
ssh -R 8377:localhost:8377 user@remote-host
  1. 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

  1. The SSH command forwards the remote port 8377 to your local port 8377.
  2. When the remote script sends data to its localhost:8377, it's forwarded through SSH to your local port 8377.
  3. 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