sftpget.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* <DESC>
  23. * Gets a file using an SFTP URL.
  24. * </DESC>
  25. */
  26. #include <stdio.h>
  27. #include <curl/curl.h>
  28. /* define this to switch off the use of ssh-agent in this program */
  29. #undef DISABLE_SSH_AGENT
  30. /*
  31. * This is an example showing how to get a single file from an SFTP server.
  32. * It delays the actual destination file creation until the first write
  33. * callback so that it won't create an empty file in case the remote file
  34. * doesn't exist or something else fails.
  35. */
  36. struct FtpFile {
  37. const char *filename;
  38. FILE *stream;
  39. };
  40. static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
  41. void *stream)
  42. {
  43. struct FtpFile *out = (struct FtpFile *)stream;
  44. if(!out->stream) {
  45. /* open file for writing */
  46. out->stream = fopen(out->filename, "wb");
  47. if(!out->stream)
  48. return -1; /* failure, can't open file to write */
  49. }
  50. return fwrite(buffer, size, nmemb, out->stream);
  51. }
  52. int main(void)
  53. {
  54. CURL *curl;
  55. CURLcode res;
  56. struct FtpFile ftpfile = {
  57. "yourfile.bin", /* name to store the file as if successful */
  58. NULL
  59. };
  60. curl_global_init(CURL_GLOBAL_DEFAULT);
  61. curl = curl_easy_init();
  62. if(curl) {
  63. /*
  64. * You better replace the URL with one that works!
  65. */
  66. curl_easy_setopt(curl, CURLOPT_URL,
  67. "sftp://user@server/home/user/file.txt");
  68. /* Define our callback to get called when there's data to be written */
  69. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
  70. /* Set a pointer to our struct to pass to the callback */
  71. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
  72. #ifndef DISABLE_SSH_AGENT
  73. /* We activate ssh agent. For this to work you need
  74. to have ssh-agent running (type set | grep SSH_AGENT to check) or
  75. pageant on Windows (there is an icon in systray if so) */
  76. curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_AGENT);
  77. #endif
  78. /* Switch on full protocol/debug output */
  79. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  80. res = curl_easy_perform(curl);
  81. /* always cleanup */
  82. curl_easy_cleanup(curl);
  83. if(CURLE_OK != res) {
  84. /* we failed */
  85. fprintf(stderr, "curl told us %d\n", res);
  86. }
  87. }
  88. if(ftpfile.stream)
  89. fclose(ftpfile.stream); /* close the local file */
  90. curl_global_cleanup();
  91. return 0;
  92. }