Search code examples
delphisftpindy

Is there a way to have a SFTP (SSH FTP) connection with delphi nowadays?


For severals days I'm looking for a way to have a SFTP connection with Delphi. I know that before it was not possible freely (SecureBlackBox, ect...), that for example Indy could not support SFTP but it was old messages. Is it now possible ? I need to read a file using SFTP. Thank you in advance !

EDIT :

I could do what I wanted to do with SecureBridge and , the code below if anyone needs it :


procedure TForm4.ScSSHClientServerKeyValidate(Sender: TObject; NewServerKey: TScKey; var Accept: Boolean);
begin
  Accept := True;
end;

procedure TForm4.SFTPConnection(Sender: TObject);
var
  ScSSHClient: TScSSHClient;
  ScFileStorage: TScFileStorage;
  ScSFTPClient: TScSFTPClient;
begin
  ScFileStorage := TScFileStorage.Create(nil);
  ScSSHClient := TScSSHClient.Create(nil);
  ScSSHClient.KeyStorage := ScFileStorage;
  ScSSHClient.OnServerKeyValidate := ScSSHClientServerKeyValidate;
  ScSSHClient.HostName := ServeurEditText.Text;
  ScSSHClient.User := UtilisateurEditText.Text;
  ScSSHClient.Password := PasswordEditText.Text;
  try
    begin
      ScSSHClient.Connect;
      ScSFTPClient := TScSFTPClient.Create(nil);
      ScSFTPClient.SSHClient := ScSSHClient;
      ScSFTPClient.Initialize;
      ScSFTPClient.DownloadFile('/etc/asterisk/sip_additional.conf','..\Debug\sip_additional.conf',True);
      ScSFTPClient.DownloadFile('/etc/asterisk/extensions_additional.conf','..\Debug\extensions_additional.conf',True);
      ScSSHClient.Disconnect;
      ShowMessage('Connexion effectuée !');
      Form4.Close;
    end;
  except
    Raise Exception.Create('La connexion a echouée...');
  end;
end;

EDIT :

I could do what I wanted to do freely with the use of libssh2.dll thanks to @Rik. The code below if anyone needs it :


procedure TChercherAppelsFrame.RecuperationFichiersConfSFTPConnexion();
var
  Mode: TAuthModes;
  FS: TFileStream;
  FS2: TFileStream;
begin
  try
    begin
      SFTP := TSFTPClient.Create(Self);
      SFTP.UserName := 'user';
      SFTP.Password := 'password';
      SFTP.Host := 'host';
      SFTP.Port := StrToIntDef('22', 22);
      SFTP.IPVersion := IPv4;
      Mode := [];
      Mode := Mode + [amPassword];
      SFTP.AuthModes := Mode;
      SFTP.Connect;

      FS := TFileStream.Create(ExtractFilePath(Application.ExeName) + ConstNomFichierContenantRepondeurs, fmCreate);
      SFTP.Get(ConstCheminFichierRepondeurs,FS, True);
      FS2 := TFileStream.Create(ExtractFilePath(Application.ExeName) + ConstNomFichierContenantPostesEtNoms, fmCreate);
      SFTP.Get(ConstCheminFichierPostesPrenoms,FS2, True);

      FS.Free;
      FS2.Free;

      SFTP.Disconnect;
    end;
  except
     Raise Exception.Create('La connexion avec le serveur SFTP a echouée...');
  end;


Solution

  • If you are open to using third-party components, I've had good success with SecureBridge with several different versions of Delphi over the years.