2014-12-18

SSH port forwarding when port forwarding is disabled

Sometimes you might run into a situation when SSH server has port forwarding disabled but you would need to access a resource on the server from your Mac/Linux workstation anyway.

This small snippet comes to the rescue:

$ mkfifo httpresponse
$ nc -l 8080 < httpresponse | ssh my.server.address.com "nc localhost 8080" > httpresponse

It uses a small unix utility called netcat to listen to the localport on your workstation. The incoming request is then piped using ssh to the remote server and to a port on the server using again the same utility, netcat. The response from the server is directed to a fifo socket on the workstation and that fifo socket content is further forwarded back to the netcat listening on your local machine and the client software will get a response back.

I noticed that the basic script above might shutdown after connection closes so you might want to run it in a loop:

$ mkfifo httpresponse
$ while :; do nc -l 8080 < httpresponse | ssh my.server.address.com "nc localhost 8080" > httpresponse; done

Happy hacking!