10.4: Set up a write-only networked clipboard

Sep 16, '05 09:57:00AM

Contributed by: atverd

Tiger only hintEver wanted to send text snippets from one machine to another via the Terminal? Save this XML code to a file named pbcopy.plist in your user's Library -> LaunchAgents folder (note that you'll probably have to create the LaunchAgents folder).

Next, execute this command in Terminal (or just logout and login):

$ launchctl load ~/Library/LaunchAgents/pbcopy.plist
Now you'll be able send any text over the network right to the clipboard on this machine via nc (netcat) or bash, like this:
$ echo test | nc -w1 1.2.3.4 2224    [or]
$ echo test >/dev/tcp/1.2.3.4/2224
Note that the second example is a bash feature and may be disabled on some systems. In both examples, replace 1.2.3.4 with the IP address of the receiving Mac (the one running the XML code), and make sure your firewall allows access to port 2224.

This is very convenient, but also very insecure. However, if you add the following lines to the pbcopy.plist file, between the dict tags in the Listeners section, then it will listen on only the loopback interface:
<key>SockNodeName</key>
<string>127.0.0.1</string>
Once that's done, you may access the service from a remote machine on 127.0.0.1:2224 if you ssh there with this option in the .ssh/config file:
RemoteForward 2224 127.0.0.1:2224
This one is a default for all my ssh connections, because very often I need to transfer a lot of text output to my local machine from many kinds of unixes. Unfortunately, Terminal's cut-n-paste can't handle this without breaking lines or truncating the text to the size of the scroll buffer.

Another trick -- if you installed SubEthaEdit and its command line tool called see (/usr/bin/see), you may put that program in the .plist (instead of /usr/bin/pbcopy), and then the editor's window will pop-up with the text content that was sent.

[robg adds: I tested this, and it worked quite well ... very well. I was able to accept text from anywhere on the net, as tested by friends in The Netherlands and Montreal. I suggest that if you implement this, you use the ssh restriction, as it does seem somewhat insecure. To remove the agent, use launchctl unload ~/Library..., then delete the file.]

Comments (11)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050914005310534