I have a script that needs a tunnel to a remote server. Something like this:
ssh -fNL 8080:localhost:80 my_server
How can I close the tunnel after my local script ends?
The above example is just one of the combinations I tried. I have seen how to make a blocking ssh call, executing a remote command and a self closing the tunnel with 'sleep', but I just want a way to provide the tunnel only during the execution of my script.
My last approach was forking ssh into the background and let it get killed when the script dies, but with the above command the ssh process is still alive after ending the script. Also I am exploring how to obtain the PID to manually kill the background ssh.