1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
CURL *curl;
CURLcode res;
std::string PrivateKey="/XXX/id_rsa";
std::string PublicKey="/XXX/id_rsa.pub";
std::string username="appldev";
std::cout << std::string(username).c_str() << std::endl;
//generate_key();
struct FtpFile ftpfile={
"/XXX/file.txt", /* name to store the file as if successful */
NULL
};
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
/*
* You better replace the URL with one that works!
*/
curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_easy_setopt(curl, CURLOPT_URL,
"sftp://10.0.0.100");
/* Define our callback to get called when there s data to be written */
curl_easy_setopt(curl, CURLOPT_USERPWD, "appldev:password");
//curl_easy_setopt(curl,CURLOPT_KEYPASSWD,"password"); //used if set pass phrase
curl_easy_setopt(curl, CURLOPT_USERNAME, std::string(username).c_str());
curl_easy_setopt(curl, CURLOPT_PORT, 22);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1000);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 10);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
curl_easy_setopt(curl, CURLOPT_SSH_PUBLIC_KEYFILE,std::string(PublicKey).c_str());
curl_easy_setopt(curl, CURLOPT_SSH_PRIVATE_KEYFILE,std::string(PrivateKey).c_str());
curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES,CURLSSH_AUTH_PUBLICKEY);
//curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES,CURLSSH_AUTH_PASSWORD);
#ifndef DISABLE_SSH_AGENT
/* We activate ssh agent. For this to work you need
to have ssh-agent running (type set | grep SSH_AGENT to check) or
pageant on Windows (there is an icon in systray if so) */
//curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_AGENT);
#endif
/* Switch on full protocol/debug output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
if(CURLE_OK != res) {
/* we failed */
fprintf(stderr, "curl told us %d\n", res);
}
}
if(ftpfile.stream)
fclose(ftpfile.stream); /* close the local file */
curl_global_cleanup(); |
Partager