From a8f37c8d238b433cd9131b1fe90d1aa9ce56d63a Mon Sep 17 00:00:00 2001 From: WhatTheServer Date: Mon, 6 Apr 2020 11:17:36 -0400 Subject: [PATCH] Update handler.py Add SSH Agent key support. Allows for SSH Agent use and key support if the local OS is setup for it thanks to Paramikos support of this. Courtesy of this excerpt https://github.com/paramiko/paramiko/blob/ae3d0febef17a8ece5268bbf6c210a30573ce800/demos/demo.py#L41-L59 and my mod adding "allow_agent=True, look_for_keys=True," to the string ssh.connect(*args, allow_agent=True, look_for_keys=True, timeout=options.timeout) This is working perfectly for me on Linux project and should work in Windows as well. http://docs.paramiko.org/en/stable/api/agent.html This will allow for easy switching of servers and key management for those embedding into custom apps. --- webssh/handler.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/webssh/handler.py b/webssh/handler.py index 74c4bb7a..29322a47 100644 --- a/webssh/handler.py +++ b/webssh/handler.py @@ -67,6 +67,21 @@ def _auth(self, username, password, pkey, *args): allowed_types = set() two_factor_types = {'keyboard-interactive', 'password'} + agent = paramiko.Agent() + agent_keys = agent.get_keys() + if len(agent_keys) == 0: + return + + for key in agent_keys: + logging.info("Trying ssh-agent key %s" % hexlify(key.get_fingerprint())) + try: + self._transport.auth_publickey(username, key) + logging.info("... success!") + return + except paramiko.SSHException as e: + logging.info("... nope.") + saved_exception = e + if pkey is not None: logging.info('Trying publickey authentication') try: @@ -438,7 +453,7 @@ def get_default_encoding(self, ssh): if result: return result - logging.warning('Could not detect the default ecnoding.') + logging.warning('Could not detect the default encoding.') return 'utf-8' def ssh_connect(self, args): @@ -447,7 +462,7 @@ def ssh_connect(self, args): logging.info('Connecting to {}:{}'.format(*dst_addr)) try: - ssh.connect(*args, timeout=options.timeout) + ssh.connect(*args, allow_agent=True, look_for_keys=True, timeout=options.timeout) except socket.error: raise ValueError('Unable to connect to {}:{}'.format(*dst_addr)) except paramiko.BadAuthenticationType: