SSH Into Windows Service

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
xDBInformatique
Posts: 4
Joined: Sat 27 Aug 2022 16:20

SSH Into Windows Service

Post by xDBInformatique » Mon 05 Sep 2022 21:24

Hello Everyone,

I have been using another sFTP component for a few years, which I have to replace because downloading a lot of files was a problem for me.

I chose Devart, first of all I find it very difficult to use, when it comes to downloading several files in the same directory... Forced to fill out a list and then go through it!! It's a consumer of neurons...

I encounter a difficulty that I cannot solve. I can't use it in a Windows service! That's where I'm at..

Code: Select all

Function TServiceCde.DownLoadCSV:Integer;
Var
 SSH : TScSSHClient;
  SFTPClient : TScSFTPClient;
  FileStorage : TScFileStorage;
  Handle : TScSFTPFileHandle;
  RootDir : string;
begin
  SSH := TScSSHClient.Create(nil);
  FileStorage := TScFileStorage(nil);
  Try
    Try
      SSH.KeyStorage := FileStorage;
      SSH.HostName := FTPHost;
      SSH.Port := FTPPort;
      SSH.User := FTPLogin;
      SSH.Authentication := atPassword;
      SSH.Password := FTPPassword;
      SSH.HostKeyAlgorithms.AsString :='ssh-rsa,ssh-dss';
      SSH.OnServerKeyValidate := ScSSHClientServerKeyValidate;

      Log('1: configuration connexion SSH');
      SSH.Connect;

      SFTPClient := TScSFTPClient.Create(nil);
      Try
        Try
          SFTPClient.SSHClient := SSH;
          SFTPClient.Initialize;

          Log('3: SFTP initié');

          if FTPRepertoire1 <> '' then
          begin
            RootDir := SFTPClient.RetrieveAbsolutePath(Trim(FTPRepertoire1));
            Handle := SFTPClient.OpenDirectory(RootDir);
            if FTPRepertoire2 <> '' then
            begin
              RootDir := SFTPClient.RetrieveAbsolutePath(Trim(FTPRepertoire2));
              Handle := SFTPClient.OpenDirectory(RootDir);
              if FTPRepertoire3 <> '' then
              begin
                RootDir := SFTPClient.RetrieveAbsolutePath(Trim(FTPRepertoire3));
                Handle := SFTPClient.OpenDirectory(RootDir);
                if FTPRepertoire4 <> '' then
                begin
                  RootDir := SFTPClient.RetrieveAbsolutePath(Trim(FTPRepertoire4));
                  Handle := SFTPClient.OpenDirectory(RootDir);
                end;
              end;
            end;
          end;
          Log('4: Chemin daccès attribué');

          iCompteurFtpDevart := 0; SetLength(FileDownLoadsFtp,iCompteurFtpDevart);
          While not SFTPClient.EOF(Handle) do
            SFTPClient.ReadDirectory(Handle);

// just for view
          for i := Low(FileDownLoadsFtp) to High(FileDownLoadsFtp) do
            Log(' file : ' + FileDownLoadsFtp[i].NameFile + ' - taille : ' + IntToStr(FileDownLoadsFtp[i].TailleFile));

        Except
          ON E:Exception do
          begin
            Log('  ***  Erreur : sFTPClient ');
            Log('  ***  Erreur : Message '+ E.Message);
          end;
        End;
      Finally
        SFTPClient.Disconnect;
        if Assigned(SFTPClient) then FreeAndNil(SFTPClient);
      end;
    Except
      ON E:Exception do
      begin
        Log('  ***  Erreur : Connexion sur le serveur '+FTPHost);
        Log('  ***  Erreur : Message '+ E.Message);
      end;
    End;
  Finally
    SSH.Disconnect;
    if Assigned(SSH) then FreeAndNil(SSH);
  End;
end;

Code: Select all

procedure TServiceCde.ScSSHClientServerKeyValidate(Sender: TObject;
  NewServerKey: TScKey; var Accept: Boolean);
begin
  Accept:=True;
end;
The problem is that I always have this error (LOG file extract)

*** Erreur : Connexion sur le serveur xx.xxx.xxx.xxx
*** Erreur : Message Storage is no set

Aucun fichier à récupérer.
Surely I must be using TscFileStorage incorrectly?

Thank you for your help
Last edited by xDBInformatique on Wed 07 Sep 2022 06:47, edited 2 times in total.

xDBInformatique
Posts: 4
Joined: Sat 27 Aug 2022 16:20

Re: SSH Into Windows Service

Post by xDBInformatique » Mon 05 Sep 2022 21:56

I found what was missing. I added a procedure DOServerKeyvalidfate.

It's really very complicated Devart.

I continue my investigations.

Thx

Post Reply