Posted by: chibimagic on: February 7, 2011
At work we do all our development on a remote server, but local tools are frequently better. I got tired of looking up the same path over and over in both my ssh session and TextWrangler’s FTP browser, and I wanted an option other than emacs/vim. I wanted to run a command on the remote machine to open that file in my local TextWrangler. I actually found a Stack Overflow question asking the exact same thing, but no one had a real answer. So I spent some time setting it and then wrote it up as an answer: http://stackoverflow.com/questions/1364826/opening-a-remote-file-with-textwrangler/4919234#4919234
How to open remote files in TextWrangler:
There’s 2 required and 3 optional parts to this:
You need to be able to ssh from local to remote to run the commands, and you need to be able to ssh from remote to local so it can send commands to TextWrangler.
To set up the ssh tunnel, you need to run a command on your local machine like:
ssh -f -N -R 10022:localhost:22 [username on remote machine]@[remote machine hostname]
The -f and -N flags put ssh into the background and leave you on your machine. The -R flag binds a port on the remote computer to a port on your local computer. Anything contacting the remote machine on port 10022 will be sent to port 22 on your local computer. The remote port can be anything you want, but you should choose a port > 1024 to avoid conflicts and so you don’t have to be root. I chose 10022 because it’s similar to ssh’s default port of 22. Replace the brackets with your username and machine name.
You’ll need to run that once after you log in. To make the command easier on yourself, you can add an alias in your bash profile. Add the following to your local ~/.bash_profile:
alias open-tunnel='ssh -f -N -R 10022:localhost:22 [username on remote machine]@[remote machine hostname]'
Of course, you can choose whatever alias name you like.
Once you’ve set up the tunnel, you can use a command like this on the remote machine:
ssh -p 10022 [username on local machine]@localhost "edit sftp://[username on remote machine]@[remote machine hostname]//absolute/path/to/file.txt"
The -p flag says to use port 10022 (or whichever port you chose earlier). This will cause the remote machine to connect to your local machine and execute the command in the double quotes without opening an interactive ssh session. The command in the quotes is the command you would run on your local machine to open the remote file in TextWrangler.
To make the command easier on yourself, you can add a function in your bash profile. Add the following to your remote ~/.bash_profile:
function edit { if [[ ${1:0:1} = "/" ]]; then abs_path="$1"; else abs_path="`pwd`/$1"; fi; ssh -p 10022 [username on local machine]@localhost "edit sftp://[username on remote machine]@[remote machine hostname]/$abs_path"; }
This is assuming that you don’t have the TextWrangler command line tools installed on the remote machine. If you do, you should name the function something other than edit. For example, tw. Here, ${1:0:1} looks at the first character of the first parameter of the function, which should be the file path. If it doesn’t begin with /, we figure out the absolute path by adding the current working directory (pwd) to the beginning. Now, if you’re on the remote machine in /home/jdoe/some/directory/ and you run edit some/other/directory/file.txt, the following will be executed on your local machine:
edit sftp://[username on remote machine]@[remote machine hostname]//home/jdoe/some/directory/some/other/directory/file.txt
Lastly, you should set up ssh keys in both directions so you’re not prompted for a password every single time. Here’s a guide someone else wrote: http://pkeck.myweb.uga.edu/ssh/
1 | htam
February 7, 2011 at 7:20 am
Why do you set up a port forwarding instead of just making the “tw” command do an SSH to the local machine directly?
I’m actually curious. Because the only times I do port forwarding is basically when machine R cannot contact machine L via SSH (say L is behind a firewall, etc) so I would use L to initiate a connection to R and run a tunnel back to L.
But in your post you said that both of them need to be able to SSH one another, which is not true since you never let the remote machine SSH the local machine.