Python – Does paramiko close ssh connection on a non-paramiko exception

paramikopythonssh

I'm debugging some code, which is going to result in me constantly logging in / out of some external sftp servers. Does anyone know if paramiko automatically closes a ssh / sftp session on the external server if a non-paramiko exception is raised in the code? I can't find it in the docs and as the connections have to be made fairly early in each iteration I don't want to end up with 20 open connections.

Best Answer

SSHClient() can be used as a context manager, so you can do

with SSHClient() as ssh:
   ssh.connect(...)
   ssh.exec_command(...)

and not close manually.

Related Topic