Page 1 of 2

simplest possible code for sftp file upload/download

Posted: Thu 18 Aug 2011 19:27
by Hans Heintz
I need to include sFTP ASAP in my delphi 2006 app. I googled for components, found sbridge10, installed it and compiled and run the example code succesfully.
I thought it would be simple to integrate the non user interface code into my own app but the user-interface-code and the functional code are extremely interwoven in the example.
Is there a basic code example for doing a file upload of file 'cfile.txt' to secure server 'secure.firmax.com' using username 'x' and password 'y'. I just haven't the time to unravel the functional code from the SSHClientApp example.

Posted: Fri 19 Aug 2011 08:46
by Dimon
You can look at the SFTPClientFrame demo as a simpler demo of using the SFTP protocol.

Here is a sample for uploading file:

Code: Select all

procedure TForm1.UploadFile;
var
  ScSSHClient: TScSSHClient;
  ScFileStorage: TScFileStorage;
  ScSFTPClient: TScSFTPClient;
begin
  ScFileStorage := TScFileStorage.Create(nil);
  ScSSHClient := TScSSHClient.Create(nil);
  ScSSHClient.KeyStorage := ScFileStorage;
  ScSSHClient.HostName := 'secure.firmax.com';
  ScSSHClient.User := 'x';
  ScSSHClient.Password := 'y;
  ScSSHClient.Connect;

  ScSFTPClient := TScSFTPClient.Create(nil);
  ScSFTPClient.SSHClient := ScSSHClient;
  ScSFTPClient.Initialize;
  ScSFTPClient.UploadFile('cfile.txt', 'cfile.txt', False);
end;

Posted: Sun 21 Aug 2011 15:43
by Hans Heintz
Thanks.

With this code I get
exception class : EScError
exception message : Host key not verified.

So despite the fact there is no more info I can give it (filename,hostname,username and password) it is not working.

The demoframe example is full of GUI code so I cannot implement any of it directly. I really only want to buy a library to start sending files to and from SFTP server, nothing more.

ps no more than a simple password is needed
if I type the url
sftp://username:password@hostname/
in IE I'm on the server without any problems
Why is it such a via dolorosa in delphi???

Posted: Mon 22 Aug 2011 07:33
by Dimon
This error occurs if the key received from the server and the key specified in HosKeyName do not match. You should handle the TScSSHClient.OnServerKeyValidate event. You can find more detailed information about this event in the SecureBridge help.

Code: Select all

procedure TForm1.ScSSHClientServerKeyValidate(Sender: TObject;
  NewServerKey: TScKey; var Accept: Boolean);
var
  Key: TScKey;
  fp, msg: string;
begin
  Key := ScSSHClient.KeyStorage.Keys.FindKey(ScSSHClient.HostName);
  if (Key = nil) or not Key.Ready then begin
    NewServerKey.GetFingerPrint(haMD5, fp);
    msg := 'The authenticity of server can not be verified.'#13#10 +
           'Fingerprint for the key received from server: ' + fp + '.'#13#10 +
           'Key length: ' + IntToStr(NewServerKey.BitCount) + ' bits.'#13#10 +
           'Are you sure you want to continue connecting?';

    if MessageDlg(msg, mtConfirmation, [mbOk, mbCancel], 0) = mrOk then begin
      Key := TScKey.Create(nil);
      try
        Key.Assign(NewServerKey);
        Key.KeyName := ScSSHClient.HostName;
        ScSSHClient.KeyStorage.Keys.Add(Key);
      except
        Key.Free;
        raise;
      end;

      Accept := True;
    end;
  end;
end;
Or you can just set the Accept parameter to True.

Posted: Mon 22 Aug 2011 07:39
by Dimon
Note: to use this code, you should add the ScBridge and ScSSHUtil units to the uses clause of your unit.

Posted: Mon 22 Aug 2011 09:42
by Hans Heintz
Thanks for helping but meanwhile I'm on the way with Chilkat which works without much ado and costs about the same.

Posted: Mon 22 Aug 2011 14:30
by Dimon
Did you compare performance of SecureBridge and Chilkat components on downloading and uploading files?

Posted: Mon 22 Aug 2011 21:16
by Hans Heintz
1. I didn't get round to taming ur code
2. The files I'm sending are like max 40kB big, so performance? who cares
Personally I think that separating logic and GUI code is a good idea, certainly for the sake of code reusability and double certainly if you write code to be used by others.

Posted: Tue 23 Aug 2011 09:06
by Dimon
Bellow you can see a very simple and completely working code for uploading file. Do you have any questions on this code, and is it useful for you?

Code: Select all

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

procedure TForm1.UploadFile;
var
  ScSSHClient: TScSSHClient;
  ScFileStorage: TScFileStorage;
  ScSFTPClient: TScSFTPClient;
begin
  ScFileStorage := TScFileStorage.Create(nil);
  ScSSHClient := TScSSHClient.Create(nil);
  ScSSHClient.KeyStorage := ScFileStorage;
  ScSSHClient.OnServerKeyValidate := ScSSHClientServerKeyValidate;
  ScSSHClient.HostName := 'secure.firmax.com';
  ScSSHClient.User := 'x';
  ScSSHClient.Password := 'y';
  ScSSHClient.Connect;

  ScSFTPClient := TScSFTPClient.Create(nil); 
  ScSFTPClient.SSHClient := ScSSHClient; 
  ScSFTPClient.Initialize; 
  ScSFTPClient.UploadFile('cfile.txt', 'cfile.txt', False); 
end;

Re: simplest possible code for sftp file upload/download

Posted: Sun 24 Jul 2016 19:20
by javageek63
I'm in a similar situation. I have been using FTP transfers for years in my apps, but I need to switch to sftp. I recently downloaded your trial to see if I can integrate it into my code. Will the code posted work with the newer releases?

Thanks,

Marty

Re: simplest possible code for sftp file upload/download

Posted: Mon 25 Jul 2016 08:55
by ViktorV
Yes, the code above will work with new SecureBridge releases.

Re: simplest possible code for sftp file upload/download

Posted: Mon 25 Jul 2016 11:56
by javageek63
The server we have trying to connect to only allows a limited number of open connections. Should there be a

ScSSHClient.Disconnect

after the ScSFTPClient.UploadFile call?

Thanks,

Marty

Re: simplest possible code for sftp file upload/download

Posted: Mon 25 Jul 2016 13:07
by ViktorV
Yes, in this case, it is better to call the ScSSClient.Disconnect method after execution of all required operations on the server.

Re: simplest possible code for sftp file upload/download

Posted: Mon 25 Jul 2016 21:08
by javageek63
I forgot to ask if you have a c++ version of this code. My apps are written in C++.

Thanks,

Marty

Re: simplest possible code for sftp file upload/download

Posted: Tue 26 Jul 2016 10:09
by ViktorV
Unfortunately, we haven't this code in ะก++, so you have to port it to C++ yourself.