Transfer Clipboard Text & Source Code From One Mac To Another Over SSH
Do you have some source code, text, or a command string that you want to securely transfer from one Mac to another? Combining the command line clipboard tools pbcopy and pbpaste with SSH we can do exactly that, and it doesn’t matter where the other Mac is located: be it LAN, WAN, or anywhere.
Here is the basic command syntax:
pbpaste | ssh username@ipaddress pbcopy
For transferring data on a LAN, you can use another Macs hostname or IP address.
This is highly useful because using this method is secure, plus it will not mangle special characters or code in the manner that many email and IM clients will.
Receiving Mac Needs SSH Enabled
In order for this to work, the recipients Mac must have “Remote Login” enabled, this allows anyone to connect to the machine securely with SSH and a password. This is easy for anyone to do:
- Launch System Preferences, click on “Sharing”
- Select the checkbox next to “Remote Login”
Now that is Remote Login is enabled, I copy the the command I want to transfer to my Macs clipboard and use the syntax structure shown above.
For example, I want to send a complex command line string to a friend of mine who is a novice computer user. Let’s say it’s a variation of lsof to monitor internet access of certain apps because it’s relatively short, but it could be anything and I typically use this for source code. Here’s my sample string to send:
lsof -nPi | cut -f 1 -d " "| uniq
I select that text and copy it to my local clipboard. Now, let’s assume my friends Mac is located in another building but on the same network, a common scenario in large corporate or educational campuses. I know his IP address is 192.168.50.175 and his Macs username is “Steve”, I’ll also need Steves password so that I can connect to his Mac and access his clipboard.
pbpaste | ssh email@example.com pbcopy
I’ll enter Steves password when requested, and his clipboard will immediately have the string I selected on my clipboard on his. Now he can paste that command into the Terminal to execute it, or do whatever else.
Securely Transfers Large Text Blocks & Source Code Between Macs
This also works with very large blocks of text which makes it perfect for sending lengthy clips of source code or other text data that you either don’t want to email for security reasons, or that would otherwise get mangled from sending over Instant Messenger clients.
This seems to only work with plain text data, but if you can get it working with anything else let us know in the comments.