Skip to content

Commit 54f47b5

Browse files
author
Pan
committed
Clean up after paramiko client objects as they do not close correctly by themselves. Updated changelog.
1 parent 13ba5ed commit 54f47b5

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

Changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Fixes
1515

1616
* Remote path for SFTP operations was created incorrectly on Windows - #88 - thanks @moscoquera
1717
* Parallel client key error when openssh config with a host name override was used - #93
18+
* Clean up after paramiko clients
1819

1920
1.1.1
2021
++++++

pssh/ssh_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ def __init__(self, host,
138138
else:
139139
self._connect(self.client, real_host, self.port, **paramiko_kwargs)
140140

141+
def __del__(self):
142+
try:
143+
self.client.close()
144+
except Exception:
145+
pass
146+
141147
def _connect_tunnel(self, host, **paramiko_kwargs):
142148
"""Connects to SSH server via an intermediate SSH tunnel server.
143149
client (me) -> tunnel (ssh server to proxy through) ->

pssh/utils.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,17 @@ def load_private_key(_pkey):
6060
:type pkey: file/str"""
6161
if not hasattr(_pkey, 'read'):
6262
_pkey = open(_pkey)
63-
for keytype in [RSAKey, DSSKey, ECDSAKey]:
64-
try:
65-
pkey = keytype.from_private_key(_pkey)
66-
except SSHException:
67-
_pkey.seek(0)
68-
continue
69-
else:
70-
return pkey
63+
try:
64+
for keytype in [RSAKey, DSSKey, ECDSAKey]:
65+
try:
66+
pkey = keytype.from_private_key(_pkey)
67+
except SSHException:
68+
_pkey.seek(0)
69+
continue
70+
else:
71+
return pkey
72+
finally:
73+
_pkey.close()
7174
logger.error("Failed to load private key using all available key types "
7275
"- giving up..")
7376

0 commit comments

Comments
 (0)