Thursday, November 14, 2013

Accessing files remotely

Accessing files remotely

Many developers get comfortable using tools on their own development machine. So much so that when asked to do some development on a remote machine, it may feel quite painful. "Gee, if I could only open this file and edit it with my favorite editor, I could get this job done a lot faster!" And it's the truth.

I tried setting up xterm on OSX 1.8, but I don't have control over the server I want to connect with, so xhost isn't going to work for me.

If you get the message "error: Can't open display: DISPLAY is not set," this usually means the administrator of the remote computer needs to enable the X11 Forwarding option in the sshd_config file before you log in.

http://stackoverflow.com/questions/5321594/using-macvim-over-ssh

I don't have access to this file and I don't want to make a big stink with the sys admins to turn it on. All I want to do is edit files with my favorite GUI editor -- MacVim!

So if not xhost and xterm, what other options do I have? First I found this little gem:

:e scp://username@host.com/path/to/file
Cody

I did not know you could do that. As stated by a few in the SO thread, you can only do this with one file at a time. Closer, yes. But I really want a little more flexibility. I often open files using mvim -p file1 file2 file3, so is there any other options?

Then I found an even more valuable gem -- SSHFS:

$ mkdir gallery_src
$ sshfs dev:projects/gallery/src gallery_src
$ cd gallery_src
$ ls
Smylers

Wow! Very cool.

This mounts a remote directory as a local directory that I can interact with as if it were on my local machine even though it really isn't.

Here is a solution that I can do without requiring any special service running on the remote server or additional configuration. I just need to include my ssh key in the remotes authorized_keys file and install a couple things on my local development machine -- OXSFuse (formerly known as MacFuse) and then SSHFS.

Voila! Now I'm in business. I start my VPN client, I've included my ssh public key in the remote's authorized_keys (chmod 600) and I have also mounted the directory of the remote project and it's business as usual, editing files on the server as if I were editing them locally.

mkdir ~/Documents/remote-project
cd ~/Documents
sshfs remote-username@remote.server.org:remote-project remote-project

Ok. And to unmount the remote directory:

cd ~/Documents
umount -f remote-project

# for some, this works too
#fusermount -u remote-project

Ok. Now, assuming I currently have the remote directory mounted, I want to find some files and open them in MacVim. I warn you now, this can be painfully slow.

cd ~/Documents/remote-project
find . -name "pom.xml" | xargs mvim -p

Yes, too slow for my taste. What can I do? Part of that answer lies with one of the earlier suggestions -- tunneling our request via ssh.

cd ~/Documents/remote-project
ssh remote-username@remote.server.org find remote-project -name "pom.xml"

Since my project uses Maven, this lists the pom.xml files in the Maven project. And the performance of this call is snappy! Now I want to pipe the resulting files into my MacVim editor.

cd ~/Documents/remote-project
ssh remote-username@remote.server.org find remote-project -name "pom.xml" | xargs mvim -p

Huh. Happy moment over. All I got was a bunch of newly created files. The left of the pipe is run on the remote. The right of the pipe is run locally. Since I don't have files at the reported location, I'm getting exactly what I asked for instead of what I was intending. If we set up the path to the directory used for the remote mount to mirror the path from the user account home, we should be able to achieve the desired result.

In the case of this example, instead of changing directories to *inside* the mounted directory, we go up one directory so that we mirror the path to the directory on the remote server from the user's home directory.

local:
~/Documents/remote-project

remote:
          ~/remote-project

On my local machine, if I am in the ~/Documents directory, I am in the same position relative to being in the user's home directory on the remote.

cd ~/Documents
ssh remote-username@remote.server.org find remote-project -name "pom.xml" | xargs mvim -p

In the find results are file references that include the path relative to the user's home directory. If this matches up with the path relative to our current local directory, then the desired files in the mounted remote directory are opened. Happy moment restored.

I'm not sure how much of a performance benefit it offers, but I found an additional tool that might improve performance when opening multiple remote ssh connections.

Performance enhancements with OpenSSH 4.0 and higher
  • ControlMaster
  • ControlPath
  • ControlPersist

No comments:

Post a Comment