simplest possible code for sftp file upload/download
-
- Posts: 4
- Joined: Thu 18 Aug 2011 19:14
simplest possible code for sftp file upload/download
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.
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.
You can look at the SFTPClientFrame demo as a simpler demo of using the SFTP protocol.
Here is a sample for uploading file:
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;
-
- Posts: 4
- Joined: Thu 18 Aug 2011 19:14
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???
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???
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.
Or you can just set the Accept parameter to True.
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;
Last edited by Dimon on Mon 22 Aug 2011 07:40, edited 1 time in total.
-
- Posts: 4
- Joined: Thu 18 Aug 2011 19:14
-
- Posts: 4
- Joined: Thu 18 Aug 2011 19:14
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;
-
- Posts: 3
- Joined: Sun 24 Jul 2016 18:38
Re: simplest possible code for sftp file upload/download
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
Thanks,
Marty
Re: simplest possible code for sftp file upload/download
Yes, the code above will work with new SecureBridge releases.
-
- Posts: 3
- Joined: Sun 24 Jul 2016 18:38
Re: simplest possible code for sftp file upload/download
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
ScSSHClient.Disconnect
after the ScSFTPClient.UploadFile call?
Thanks,
Marty
Re: simplest possible code for sftp file upload/download
Yes, in this case, it is better to call the ScSSClient.Disconnect method after execution of all required operations on the server.
-
- Posts: 3
- Joined: Sun 24 Jul 2016 18:38
Re: simplest possible code for sftp file upload/download
I forgot to ask if you have a c++ version of this code. My apps are written in C++.
Thanks,
Marty
Thanks,
Marty
Re: simplest possible code for sftp file upload/download
Unfortunately, we haven't this code in С++, so you have to port it to C++ yourself.