现在有没有办法与 delphi 建立 SFTP (SSH FTP) 连接?

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

几天来,我一直在寻找一种与 Delphi 建立 SFTP 连接的方法。我知道之前它不可能自由(SecureBlackBox,等等......),例如 Indy 不能支持 SFTP 但它是旧消息。现在可以吗?我需要使用 SFTP 读取文件。提前致谢!

编辑:

我可以用 SecureBridge 和下面的代码做我想做的事,如果有人需要的话:


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;

编辑:

感谢@Rik,我可以使用 libssh2.dll 自由地做我想做的事。 下面的代码,如果有人需要的话:


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;

如果您愿意使用第三方组件,这些年来我在 SecureBridge 和几个不同版本的 Delphi 方面取得了很好的成功。

tgputtylib 提供可用于连接 sftp 连接的免费 dll。 使用 tgputtylib 文件和另一个可以从任何版本 delphi 项目调用的 dll 创建了一个示例项目。检查此 repository 看看是否对您有帮助。

参考资料:https://github.com/superflexible/TGPuttyLib