I have a proxy server which I connect to like so
ssh -L 2001:localhost:8888 -N myserver.com -p 443
I then open up Firefox
firefox -p "SSHProxy"
(It starts Firefox configured to use port 2001). I browse a bit, then close Firefox and then stop the tunnel.
What I would like to do is automate it: open the tunnel, then open Firefox. When I close Firefox, I'd like to close the tunnel too.
Is this possible in a simple script?
I have tried
ssh -L 2001:localhost:8888 -N myserver.com -p 443 && firefox -p "SSHProxy"
but only the SSH bit executes, Firefox does not open. If I reverse the order, then SSH only opens after I close Firefox.
With help below I have used this simplified script:
#!/bin/bash -e
#Start SSH
ssh -L 2001:localhost:8888 -Nf myserver.com -p 443 &
#Start Firefox
firefox -p "SSHProxy" &
#Kill the SSH port 2001 when Firefox stops
trap "ps aux | grep ssh | grep 2001 | awk '{print \$2}' | xargs kill" EXIT SIGINT SIGTERM
wait