Search code examples
pythonsshsftpparamiko

Paramiko close connection doesn't work


I'm having trouble trying to close a Paramiko SFTP connection. Even though I call close the connection is still hanging, I check by running netstat (Windows):

netstat -an | find ":22"

and the python code:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy() )
ftp = ssh.open_sftp()
time.sleep(5)
ftp.close()

What is the proper way to close Paramiko SFTP connection that works?

thanks


Solution

  • This is the correct way to go about it

    import paramiko
    
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect()
    
    ftp = ssh.open_sftp()
    
    ftp.close()
    ssh.close()
    

    You need to close the ssh instance as well as the sftp.