Transfer Clipboard Text & Source Code From One Mac To Another Over SSH

May 5, 2011 - 6 Comments

Transfer Source Code and Text 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
Enable remote login and SSH in Mac OS X 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.

Example
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 steve@192.168.50.175 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.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: David Mendez in Command Line, Mac OS X

6 Comments

» Comments RSS Feed

  1. musicalpedant says:

    That’s pretty cool – up to the point where you say you need Steve’s password, at which point I have to stand up and call you all sorts of unflattering names.
    Don’t share passwords. Don’t give them out.

    As a tech blog, you should know better than to promote techniques that involve sharing your password. Shame on you.

  2. GnuGnu says:

    Great tip but the user password thing is an obstacle for random user to random user as musicalpendant suggests On the other hand, from transferring from your own two machines remotely this is really quite nice.

    One suggestion, if you’re sending source code, I would recommend dumping the code into a text file on the remote machine and not the clipboard. Use this command:

    pbpaste | ssh user@host ‘cat > ~/Desktop/source.c’

    If you append the proper suffix it will automatically code highlight and then open with the default application too. Might make a good tip on it’s own…

  3. David says:

    Great feedback and tips everyone, thanks for the ideas.

  4. Ian says:

    Why use pbcopy/pbpaste with ssh at all? For large files why not use scp or sftp and transfer the source file? Just throwing that out there.

  5. nobidon says:

    I like this and I often copy text strings from one computer to another at my same desk. Instead of using an XMPP text message client on both computers I’ll start using this instead. I put the command into a scriptName.sh file and then I created a command line alias on both computers to call that script so I just type a few characters on the command line and it sends the clip board content over to the other computer nearly instantly. Very nice, and very fast, and I am the same user in both places so I don’t have the sharing the password problem with myself.

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates