0

I based my code on Download file using libcurl in C/C++ but I'm getting an ERROR: "Unsupported protocol" for SFTP. However, for the same SFTP, file download and upload works with the command line.

Code:

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
    CURL *curl;
    FILE *fp;
    CURLcode res;
    //char *url = "http://stackoverflow.com";
    char *url = "sftp://dheerajr@10.4.1.156/home/dheerajr/temp/download.txt";
    char outfilename[FILENAME_MAX] = "temp.txt";
    curl = curl_easy_init();                                                                                                                                                                                                                                                           
    if (curl)
    {   
        fp = fopen(outfilename,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        if(res == CURLE_OK)
            printf("Download Successfull\n");
        else
            printf("ERROR: %s\n", curl_easy_strerror(res));
        curl_easy_cleanup(curl);
        fclose(fp);
    }   
    return 0;
}

checked with curl -V command which supports SFTP. curl -V output:

curl 7.72.0-DEV (x86_64-pc-linux-gnu) libcurl/7.72.0-DEV OpenSSL/1.1.1 zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) libssh2/1.8.2 nghttp2/1.30.0 librtmp/2.3
Release-Date: [unreleased]
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS HTTP2 HTTPS-proxy IDN IPv6 Largefile libz NTLM NTLM_WB PSL SSL TLS-SRP UnixSockets

Can anyone suggest a solution for this?

mateuszb
  • 1,072
  • 13
  • 26

1 Answers1

0

You need to compile ssh2 library first and that reconfigure curl library with --with-libssh2="path" and recompile. then you will have the ssh function call working with curl api's.

Good Luck ;-)

VamsiKrishna Neelam
  • 103
  • 1
  • 1
  • 11