simplest possible code for sftp file upload/download

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Hans Heintz
Posts: 4
Joined: Thu 18 Aug 2011 19:14

simplest possible code for sftp file upload/download

Post by Hans Heintz » Thu 18 Aug 2011 19:27

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.

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Fri 19 Aug 2011 08:46

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;

Hans Heintz
Posts: 4
Joined: Thu 18 Aug 2011 19:14

Post by Hans Heintz » Sun 21 Aug 2011 15:43

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???

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Mon 22 Aug 2011 07:33

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.
Last edited by Dimon on Mon 22 Aug 2011 07:40, edited 1 time in total.

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Mon 22 Aug 2011 07:39

Note: to use this code, you should add the ScBridge and ScSSHUtil units to the uses clause of your unit.

Hans Heintz
Posts: 4
Joined: Thu 18 Aug 2011 19:14

Post by Hans Heintz » Mon 22 Aug 2011 09:42

Thanks for helping but meanwhile I'm on the way with Chilkat which works without much ado and costs about the same.

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Mon 22 Aug 2011 14:30

Did you compare performance of SecureBridge and Chilkat components on downloading and uploading files?

Hans Heintz
Posts: 4
Joined: Thu 18 Aug 2011 19:14

Post by Hans Heintz » Mon 22 Aug 2011 21:16

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.

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Tue 23 Aug 2011 09:06

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;

javageek63
Posts: 3
Joined: Sun 24 Jul 2016 18:38

Re: simplest possible code for sftp file upload/download

Post by javageek63 » Sun 24 Jul 2016 19:20

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

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: simplest possible code for sftp file upload/download

Post by ViktorV » Mon 25 Jul 2016 08:55

Yes, the code above will work with new SecureBridge releases.

javageek63
Posts: 3
Joined: Sun 24 Jul 2016 18:38

Re: simplest possible code for sftp file upload/download

Post by javageek63 » Mon 25 Jul 2016 11:56

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

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: simplest possible code for sftp file upload/download

Post by ViktorV » Mon 25 Jul 2016 13:07

Yes, in this case, it is better to call the ScSSClient.Disconnect method after execution of all required operations on the server.

javageek63
Posts: 3
Joined: Sun 24 Jul 2016 18:38

Re: simplest possible code for sftp file upload/download

Post by javageek63 » Mon 25 Jul 2016 21:08

I forgot to ask if you have a c++ version of this code. My apps are written in C++.

Thanks,

Marty

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: simplest possible code for sftp file upload/download

Post by ViktorV » Tue 26 Jul 2016 10:09

Unfortunately, we haven't this code in С++, so you have to port it to C++ yourself.

Post Reply