Ejemplo n.º 1
0
 def connect(self) -> None:
     """
     Connect to a SFTP server using an SSH RSA key.
     :return: None
     """
     # Public part of the user's RSA keys must be in the right place on the proftpd SFTP server.
     # The SFTP server has its own RSA private and public (host) key used to authenticate itself to the client.
     # The public SFTP key must be added to known hosts using ssh key scan.
     try:
         if self._passphrase:
             key = paramiko.RSAKey.from_private_key_file(
                 self._priv_key, password=self._passphrase)
             del self._passphrase
         else:
             key = paramiko.RSAKey.from_private_key_file(self._priv_key)
     except paramiko.PasswordRequiredException as e:
         raise paramiko.PasswordRequiredException(e)
     except paramiko.SSHException as _:
         raise AccessException(Strings.warning_rsa_passphrase_wrong)
     # Connect SSH client.
     self._ssh_connection = paramiko.SSHClient()
     self._ssh_connection.load_system_host_keys()
     self._ssh_connection.set_missing_host_key_policy(
         paramiko.AutoAddPolicy())
     self._ssh_connection.connect(self._ip,
                                  self._port,
                                  self._user,
                                  pkey=key,
                                  timeout=Numbers.connection_timeout)
     self._sftp_connection = self._ssh_connection.open_sftp()
     # Keep a reference to the SSH client in the SFTP client as to prevent the former from being garbage
     # collected and the connection from being closed.
     self._sftp_connection.sshclient = self._ssh_connection
Ejemplo n.º 2
0
 def __read_private_key_from_file(self, filename, password):
     for pkey_class in self.available_pkey_classes:
         try:
             self.__key = pkey_class.from_private_key_file(filename, password)
         except paramiko.SSHException as e:
             print("{}".format(e))
         except paramiko.PasswordRequiredException:
             raise paramiko.PasswordRequiredException('Password is requred')
Ejemplo n.º 3
0
 def __read_private_key(self, pkey_str, password):
     for pkey_class in self.available_pkey_classes:
         try:
             file_obj = io.StringIO(pkey_str)
             self.__key = pkey_class.from_private_key(file_obj, password)
         except paramiko.SSHException as e:
             print("{}".format(e))
         except paramiko.PasswordRequiredException:
             raise paramiko.PasswordRequiredException('Password is requred')
         finally:
             file_obj.close()
Ejemplo n.º 4
0
 def connect_with_password(self):
     """Try connecting with password."""
     LOG.debug("Trying to connect with password")
     if self.interactive and not self.password:
         LOG.debug("Prompting for password (interactive=%s)",
                   self.interactive)
         try:
             self.password = getpass.getpass("Enter password for %s:" %
                                             self.username)
         except KeyboardInterrupt:
             LOG.debug("User cancelled at password prompt")
     if not self.password:
         raise paramiko.PasswordRequiredException("Password not provided")
     return self._connect(password=self.password,
                          look_for_keys=False,
                          allow_agent=False)