I have ran into trouble while starting SSH tunnel from HTTP RPC server written in Python.
There is a simple HTTP RPC server written in Python based on Python's BaseHTTPServer. As a part of one of the services I would like to start a SSH tunnel from the RPC server to a remote machine. I used os.system to start the SSH tunnel in the Python script invoked by the RPC call
os.system("ssh -f -n -N -L 127.0.0.1:%d:localhost:%d user@%s" % (6800, 9000, "remote.machine"))
At first sight all seems to be well as the tunnel is started and I can use it, but there is one thing I noticed. In addition to listening on the port 6800 SSH started listening on port 8001 as well (the port that the HTTP RPC server runs on).
Here is output of lsof regarding the RPC server and SSH:
rpc.py 27763 usern 5u IPv4 102130428 TCP 127.0.0.1:8001 (LISTEN)
ssh 1951 usern 14u IPv4 102149728 TCP 127.0.0.1:6800 (LISTEN)
ssh 1951 usern 5u IPv4 102130428 TCP 127.0.0.1:8001 (LISTEN)
Everything works until RPC server's restart. During restart the RPC server is forced to close his connection to the listening socket but the SSH's connection remains open and RPC server can not start on the same port again.
It seems that the SSH tunnel also somehow associates itself with the fd of the RPC server's listening socket.
Could anybody give hints how to set up the SSH tunnel from the script with it only listening on the supposed port (6800 in this example).