[SOLVED] Writing an SFTP client

Everything else that doesn't fall into one of the other PB categories.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

[SOLVED] Writing an SFTP client

Post by Oso »

We need to develop an SFTP client as part of a sales order processing system. When I first found PureBasic last year, the first thing I noticed was that it already provides FTP, straight out of the box. Within about 30 minutes, I had written an ftp transfer routine :) . Due to the fact that this new requirement is for an invoice data transfer, it falls to me to provide (or develop) an SFTP solution. Is this possible to do in PB?

I have seen something on a website called Chilkat with examples of PB code and I'm not sure if others are aware of this.
Last edited by Oso on Mon Jan 30, 2023 7:37 pm, edited 1 time in total.
swhite
Enthusiast
Enthusiast
Posts: 726
Joined: Thu May 21, 2009 6:56 pm

Re: Writing an SFTP client

Post by swhite »

Hi

I use Chillkat often and it works well with PB. So it should be fairly simple to create an SFTP app with PB and Chillkat.

Simon
Simon White
dCipher Computing
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Writing an SFTP client

Post by Oso »

swhite wrote: Wed Jan 25, 2023 8:16 pm I use Chillkat often and it works well with PB. So it should be fairly simple to create an SFTP app with PB and Chillkat. Simon
Thanks for the reply, Simon. Do they provide all those modules shown on their website as a single package? It wasn't entirely clear to me how to choose the required module.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Writing an SFTP client

Post by infratec »

You don't need to by something.

It is possible with libcurl.

For this you need an external libcurl.dll (or compile your own static lib) which includes sftp.
And my libcurl.pbi.

Then you can use:
https://curl.se/libcurl/c/sftpget.html
https://curl.se/libcurl/c/sftpuploadresume.html

My libcurl.pbi is here:
viewtopic.php?t=80118

In the libcurl.dll, which is in my zip file, sftp is included.

you need to set

Code: Select all

#LibCurl_ExternalDLL = #True
before your IncludeFile.
And copy the corresponding dll to your file directory.
(I always set the option 'create temporary executable in the sourcecode directory' in compiler options)
--- Info ---
Version: 7.86.0
Host: i686-w64-mingw32
SSL: OpenSSL/3.0.7 (Schannel)
Libz: 1.2.13
libSSH: libssh2/1.10.0
Protocols:
dict
file
ftp
ftps
gopher
gophers
http
https
imap
imaps
ldap
ldaps
mqtt
pop3
pop3s
rtsp
scp
sftp
smb
smbs
smtp
smtps
telnet
tftp
ws
wss
--- Info ---
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Writing an SFTP client

Post by Oso »

infratec wrote: Wed Jan 25, 2023 11:29 pm It is possible with libcurl. For this you need an external libcurl.dll (or compile your own static lib) which includes sftp. And my libcurl.pbi.

Then you can use: https://curl.se/libcurl/c/sftpget.html https://curl.se/libcurl/c/sftpuploadresume.html

My libcurl.pbi is here: viewtopic.php?t=80118 In the libcurl.dll, which is in my zip file, sftp is included.
This is great Infratec, thanks very much indeed. The two links that you included for the C code at curl.se/libcurl/c... — I assume this is just for reference? In the C code is #include <curl/curl.h> so I'm guessing your libcurl.pbi is an equivalent of that C header file. Am I understanding it correctly?
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Writing an SFTP client

Post by infratec »

Just now I added an example for SFTP in my ZIP file.
You need also the modified LibCurl.pbi

You can (more or less) directly convert the C examples. The calls to libcurl are nearly identical.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Writing an SFTP client

Post by Oso »

Thanks Infratec, I've just been looking at it the SFTP. The rebex.net ftp test is useful, that saves the need to set up an sftp server on another machine.

Incidentally, where is the function that determines whether we are using 'get' or 'put'? I looked at your two ftps examples earlier and I see that one is sending, the other receiving, but I couldn't see where it sets that mode.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Writing an SFTP client

Post by infratec »

curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Writing an SFTP client

Post by Oso »

infratec wrote: Thu Jan 26, 2023 11:35 am
curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
Ah okay, I see, it isn't set in the download version. Thanks for that. I'll try this later.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Writing an SFTP client

Post by infratec »

Added examples:

SFTP_upload.pb
SFTP_uploadresume.pb
swhite
Enthusiast
Enthusiast
Posts: 726
Joined: Thu May 21, 2009 6:56 pm

Re: Writing an SFTP client

Post by swhite »

Yes all the modules are included in the download.

Simon

Oso wrote: Wed Jan 25, 2023 9:15 pm
swhite wrote: Wed Jan 25, 2023 8:16 pm I use Chillkat often and it works well with PB. So it should be fairly simple to create an SFTP app with PB and Chillkat. Simon
Thanks for the reply, Simon. Do they provide all those modules shown on their website as a single package? It wasn't entirely clear to me how to choose the required module.
Simon White
dCipher Computing
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Writing an SFTP client

Post by Oso »

infratec wrote: Thu Jan 26, 2023 12:35 pm Added examples:
SFTP_upload.pb
SFTP_uploadresume.pb
They work very well indeed :) Thank you for adding these Infratec. I tested using a 13MB file which was transferred perfectly. I will continue tomorrow, as it's late here now. I looked at 'resume' and I see that it has the capability to send from the last point onwards. Thanks again for doing this. I will think about it further and have some more ideas.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Writing an SFTP client

Post by Oso »

swhite wrote: Thu Jan 26, 2023 3:39 pm Yes all the modules are included in the download.

Simon
Thanks again Simon, I think I'm going to be okay with the libcurl solution but there are some other interesting modules in that list.
Post Reply