python and ssh

Christopher T King squirrel at WPI.EDU
Thu Jul 22 09:38:23 EDT 2004


On Wed, 21 Jul 2004 jepler at unpythonic.net wrote:

> You would have to ask the authors of ssh that question.
> 
> I *suspect* that sshd creates special file descriptors that communicate
> with the process started on the remote machine as the user.  Then it
> enters a select loop.  Once all the programs with access to that file
> descriptor close it (for instance, by exiting), sshd detects this because
> the file descriptors are "readable" according to select, but a read gets
> 0 bytes.  When that happens, sshd closes down communication with ssh,
> which exits.

Building off of that, in order to get ssh to end the connection, the 
spawned process will need to close the descriptors it inherited from 
ssh:

import sys
sys.stdin.close()
sys.stdout.close()
sys.stderr.close()

Basically, ssh is staying open because it knows that it's possible for the 
spawned process to write to these streams, which would normally be 
redirected over the ssh connection.  By closing them, you're telling ssh 
"I don't need to do any more input or output to/from the console", and ssh 
(should) respond to this by closing the connection.  (I say should because 
I haven't tested this!)




More information about the Python-list mailing list