def start_transport(transport, username, key):
""" Begin a transport client and authenticate it
:param transport: the transport object to start
:type transport: paramiko.Transport
:param username: SSH username
:type username: str
:param key: key object used for authentication
:type key: paramiko.RSAKey
:return: the transport object passed
:rtype: paramiko.Transport
"""
transport.start_client()
agent = paramiko.agent.Agent()
keys = itertools.chain((key,) if key else (), agent.get_keys())
for test_key in keys:
try:
transport.auth_publickey(username, test_key)
break
except paramiko.AuthenticationException as e:
pass
else:
raise ValueError('No valid key supplied')
return transport
# SSH connection will be auto-terminated at the conclusion of this operation, causing
# a race condition; the try/except block attempts to close the channel and/or transport
# but does not issue a failure if it has already been closed.
评论列表
文章目录