diff --git a/autosubmit/platforms/paramiko_platform.py b/autosubmit/platforms/paramiko_platform.py index e8327b9ad2e125319b3e86d055ac53c97d76f3b4..5cb5d88c717a39bff175acba27bd69c765e14677 100644 --- a/autosubmit/platforms/paramiko_platform.py +++ b/autosubmit/platforms/paramiko_platform.py @@ -1,4 +1,6 @@ import locale +from binascii import hexlify +from contextlib import suppress from time import sleep import sys import socket @@ -177,7 +179,18 @@ class ParamikoPlatform(Platform): raise AutosubmitCritical( 'Cant connect to this platform due an unknown error', 7050, str(e)) - + def agent_auth(self,port): + """ + Attempt to authenticate to the given SSH server using the most common authentication methods available. This will always try to use the SSH agent first, and will fall back to using the others methods if that fails. + :parameter port: port to connect + :return: True if authentication was successful, False otherwise + """ + try: + self._ssh.connect(self._host_config['hostname'], port=port, username=self.user, timeout=60, banner_timeout=60) + except BaseException as e: + Log.warning(f'Failed to authenticate with ssh-agent due to {e}') + return False + return True def connect(self, reconnect=False): """ Creates ssh connection to host @@ -193,7 +206,6 @@ class ParamikoPlatform(Platform): self._ssh = paramiko.SSHClient() self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self._ssh_config = paramiko.SSHConfig() - self._user_config_file = os.path.expanduser("~/.ssh/config") if os.path.exists(self._user_config_file): with open(self._user_config_file) as f: @@ -204,36 +216,33 @@ class ParamikoPlatform(Platform): self._host_config['hostname'] = random.choice( self._host_config['hostname'].split(',')[1:]) else: - self._host_config['hostname'] = self._host_config['hostname'].split(',')[ - 0] + self._host_config['hostname'] = self._host_config['hostname'].split(',')[0] if 'identityfile' in self._host_config: self._host_config_id = self._host_config['identityfile'] - #pkey = paramiko.Ed25519Key.from_private_key_file(self._host_config_id[0]) port = int(self._host_config.get('port',22)) - if 'proxycommand' in self._host_config: - self._proxy = paramiko.ProxyCommand( - self._host_config['proxycommand']) - try: - self._ssh.connect(self._host_config['hostname'], port, username=self.user, - key_filename=self._host_config_id, sock=self._proxy, timeout=120 , banner_timeout=120) - except Exception as e: - self._ssh.connect(self._host_config['hostname'], port, username=self.user, - key_filename=self._host_config_id, sock=self._proxy, timeout=120, - banner_timeout=120,disabled_algorithms={'pubkeys': ['rsa-sha2-256', 'rsa-sha2-512']}) - else: - try: - self._ssh.connect(self._host_config['hostname'], port, username=self.user, - key_filename=self._host_config_id, timeout=60 , banner_timeout=60) - except Exception as e: - self._ssh.connect(self._host_config['hostname'], port, username=self.user, - key_filename=self._host_config_id, timeout=60 , banner_timeout=60,disabled_algorithms={'pubkeys': ['rsa-sha2-256', 'rsa-sha2-512']}) + # Agent Auth + if not self.agent_auth(port): + # Public Key Auth + if 'proxycommand' in self._host_config: + self._proxy = paramiko.ProxyCommand(self._host_config['proxycommand']) + try: + self._ssh.connect(self._host_config['hostname'], port, username=self.user, + key_filename=self._host_config_id, sock=self._proxy, timeout=60 , banner_timeout=60) + except Exception as e: + self._ssh.connect(self._host_config['hostname'], port, username=self.user, + key_filename=self._host_config_id, sock=self._proxy, timeout=60, + banner_timeout=60,disabled_algorithms={'pubkeys': ['rsa-sha2-256', 'rsa-sha2-512']}) + else: + try: + self._ssh.connect(self._host_config['hostname'], port, username=self.user, + key_filename=self._host_config_id, timeout=60 , banner_timeout=60) + except Exception as e: + self._ssh.connect(self._host_config['hostname'], port, username=self.user, + key_filename=self._host_config_id, timeout=60 , banner_timeout=60,disabled_algorithms={'pubkeys': ['rsa-sha2-256', 'rsa-sha2-512']}) self.transport = self._ssh.get_transport() - #self.transport = paramiko.Transport((self._host_config['hostname'], 22)) - #self.transport.connect(username=self.user) - window_size = pow(4, 12) # about ~16MB chunks - max_packet_size = pow(4, 12) - #self._ftpChannel = self._ssh.open_sftp() - self._ftpChannel = paramiko.SFTPClient.from_transport(self.transport,window_size=window_size,max_packet_size=max_packet_size) + self.transport.banner_timeout = 60 + + self._ftpChannel = paramiko.SFTPClient.from_transport(self.transport,window_size=pow(4, 12) ,max_packet_size=pow(4, 12) ) self._ftpChannel.get_channel().settimeout(120) self.connected = True except SSHException as e: