How To Round-Trip Data Via SSH
By Adrian Sutton
I keep forgetting this so for the record, you can use SSH to round trip data out to a remote server and back to your own box. This is most useful for adding latency and bandwidth limitations to a connection when doing website testing. The trick is to use both local and remote port forwards:
ssh -L 9090:localhost:9091 -R 9091:localhost:80 remote.server.com
The -L argument starts listening on port 9090 locally and sends all that traffic to port 9091 on the remote server (the domain name, localhost, is resolved by the remote server so it refers to the remote server, not your local machine). Then the -R argument listens on port 9091 of the remote server and forwards all that traffic back to your machine’s port 80 (here localhost is resolved on your local machine).
You don’t have to use localhost as the domain name. For example, if the site you want to test is deployed on your intranet at testmachine.intranet which remote.server.com doesn’t have access to, you could use:
ssh -L 9090:localhost:9091 -R 9091:testmachine.intranet:80 remote.server.com
Or if the test site is publicly available you can do it all without the -R argument:
ssh -L 9090:testmachine.com:80 remote.server.com
In all these cases, you connect to localhost:9090 to utilise the tunnel.