Friday, October 26, 2012

macopen, a Bash Script to Open and Edit Linux Files on Your Mac Using SSH/OSXFuse/sshfs and OS X's 'open' Command

Note: a user on the textmate IRC channel mentioned rmate, which already does much of what I was trying to accomplish.  It appears to be TextMate-specific though.

Okay it's late and I don't have time for a long description, but this is my first true "hack" and I need to post about it quickly.

Do you ever find yourself logged into a remote UNIX machine from your Mac via SSH, wishing you could say (for instance) "TextMate foo.txt" and have foo.txt from the remote filesystem open in TextMate just like it would in an X Window app?  Turns out this isn't all that hard to set up.  There are good tools for mounting remote filesystems via SSH; all that's missing is a way to auto-mount them and launch Mac apps from the remote end.  I considered writing a fancy server until I realized everything was already there with a reverse SSH connection.  So I wrote a script that logs *back* into my Mac from the remote machine, mounts the remote filesystem via OSXFuse and sshfs, and launches the file via OS X's "open" command.

To make it work, install OSXFuse and SSHFS, both available for download from this page.  Then enable remote login on your Mac and configure your Mac and the remote machines for password-less SSH using ssh-keygen as described here.  Then download the macopen script, install it on the remote machine, and enjoy.

There are some rough edges, but I'll work those out over time.  Note that the machines have to be able to route to one another, so no NAT support right now.  I read a little about SSH tunneling/reverse SSH, which might be able to help when your Mac is behind NAT.

I'm not really a UNIX/shell scripting guy, so probably I'm doing all sorts of things wrong.  This is the longest bash script I've ever written, if that's any indication.  So feel free to fork, clean it up, issue pull requests, and what-have-you.

Good luck!


  1. I can honestly say that I do not know what this is, but I love the man who does.

    1. Aww, that just gave me warm fuzzies. This is really upping the ante though--you commenting on a blog post you don't even get leaves me with no excuse for not commenting on your blog. So here goes...