Background process for ssh port forwarding

Jesse Rosenthal jr2075 at
Tue Oct 4 22:37:34 CEST 2005

On Tue, 04 Oct 2005 12:40:27 -0700, John Hazen wrote:

> I think what's happening is that when you return from 'hostforward', the
> connection is being closed because of garbage collection.  Python uses
> (among other stuff) reference counting to tell it when to delete
> objects.  After hostforward returns from execution, there are no longer
> any references to 'connection', so it gets deleted, which cleans up the
> connection.
> You probably want to add:
>     return connection
> at the end of hostforward, and call it like:
> connection = hostforward()
> my_rsync_function()
> connection.close()   # or whatever the approved pexpect cleanup is

Thanks, John. This makes a lot of sense -- I'll have to give it a try.

I ended up taking an uglier approach. First I set up keys pairs
with my forwarding server so I don't have to log in. So no more need for
pexpect at this point. I then set up an ssh connection with a "-f sleep
10" option. This backgrounds the connection to the forwarding server and
runs "sleep 10" on the forwarding server, which gives me 10 seconds to
log in to my forwarded port (localhost 2022). 10 seconds is actually
probably excessive, since logging in is the next command in the script.
The connection then stays open so long as I'm logged in.

It works, but it seems rather kludgey. So I'll definitely give your
approach a try.

Thanks again

More information about the Python-list mailing list