HISPACTA Hell

Monday, June 29, 2009

Double port forwarding

I had a problem today that involved a couple levels of SSH port forwarding...

I was evaluating an email vendor who had asked what machine they should expect my test emails to be sent from. I replied with our dev02 server, thinking that I'd just create a little web page on our reportapp that you could "click button to send mime email" or something.

In the end, I wound up writing the email test as a JUnit test case, which I could execute on demand via NetBeans on my localhost.

So the problem was the vendor was expecting traffic from dev02, but I was only generating traffic from localhost. Complicating the matter was that I couldn't get directly to dev02 from localhost...I had to go through another server named "gateway".

  1. Tunnel my localhost's port 5555 to gateway's port 5555:
    ssh -AL 5555:localhost:5555 my.name@gateway.mycompany.com

    Technically, I think the "-A" flag isn't necessary (it just forwards my authentication token, which I have set up so I don't need to log in)

  2. The first command got me out to gateway with the 5555 tunnel open. Now tunnel gateway's 5555 port to dev02 and have dev02 forward that port to the email vendor's machine and port (in bold):
    ssh -L 5555:1.2.3.4:9936 my.name@dev02.mycompany.com

  3. Then in my unit test, I can send mail to a JavaMailSenderImpl setup with host "localhost" and post "5555" and it ends up being sent to 1.2.3.4 on port 9936, and the vendor sees the traffic as dev02's IP and lets it through the firewall.

Note that I'm pretty sure you can combine both steps 1 and 2 into a single step by using the fact that the ssh port forwarding syntax command can take a command to run on the remote box (the remote command to run is itself an ssh port forwarding command). Something like this. . . though use at your own risk:

ssh -AnfL 5555:localhost:5555 my.name@gateway.mycompany.com \
"ssh -L 5555:1.2.3.4:9936 my.name@dev02.mycompany.com"


Maybe try adding a "sleep 30" at the end of that command (inside the double quote) if the -nf flags try to close it immediately

0 Comments:

Post a Comment

<< Home