I never know what to call this process, so I’m inventing the term <em>supertunnels</em> via SSH for now. A lot of my work these days involves using clusters built on <a href=”http://aws.amazon.com/ec2/”>Amazon EC2</a> cloud environment. There, I have some servers that are externally accessible, i.e. web servers. Then there are support servers that are only accessible “internally” to those web servers and not accessible from the outward facing public side of the network, i.e. <a href=”http://hadoop.apache.org/”>Hadoop clusters</a>, databases, etc.
To help log into the “internal” machines, I have pretty much one choice – using SSH <em>through the public machine first</em>. No problem here, any server admin knows how to use SSH – I’ve been using it forever. However, I didn’t really use some of the more advanced features that are very helpful. Here are two…
<h3>Remote command chaining</h3>
Most of my SSH usage is for running long sessions on a remote machine. But you can also pass a command as an argument and the results come directly back to your current terminal:
<code>$ ssh user@host “ls /usr/lib”
Take this example one step further and you can actually <strong>inject another SSH command</strong> that gets into the “internal” side of the network.
This is starting to really sound like tunneling, though it’s somewhat manual and doesn’t redirect traffic from your client side, we’ll get to that later.
As an aside, in EC2-land you often use certificate files during SSH login, so you don’t need to have an interactive password exchange. You specify the certificate with another argument. If that’s how you run your servers (or with authorized_keys files) then you can push in multiple levels of additional SSH commands easily.
For example, here I log into <strong>ext-host1</strong>, then from there log into <strong>int-host2</strong> and run a command:
<code>$ ssh -i ~/mycert.pem user@ext-host1 “ssh -i ~/mycert.pem user@int-host2 ‘ls /usr/lib'”
That is a bit of a long line for just getting a file listing, but it’s easy to understand and gets the job done quickly. It also works great in shell scripts, in fact you could wrap it up with a simple script to make it shorter.
Another way to make your command shorter and simpler is to add some proxy rules to the ~/.ssh/config file. I didn’t even know this file existed, so was thrilled to find out how it can be used.
To talk about this, let’s use the external and internal hosts as examples. And let’s assume that the internal host is 10.0.1.1. Obviously these don’t need to be specifically public or private SSH endpoints, but it serves its purpose for this discussion.
If we are typically accessing int-host2 via ext-host1 then we can setup a Proxy rule in the config file:
ProxyCommand ssh -i ~/mycert.pem user@ext-host1 -W %h:%p
This rule watches for <b>any</b> requests on the 10.0… network and automatically pushes the requests through the ext-host1 as specified above. Furthermore, the -W option tells it to stream all output back to the same terminal you are using. (Minor point, but if you miss it you may go crazy trying to find out where your responses go.)
Now I can do a simple login request on the <b>internal</b> host and not even have to think about how to get there.
ssh -i ~/mycert.pem user@int-host2
I think that’s a really beautiful thing – hope it helps!
Another time I’ll have to write more about port forwarding…