sftpget.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * Gets a file using an SFTP URL.
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <curl/curl.h>
  30. /* define this to switch off the use of ssh-agent in this program */
  31. #undef DISABLE_SSH_AGENT
  32. /*
  33. * This is an example showing how to get a single file from an SFTP server. It
  34. * delays the actual destination file creation until the first write callback
  35. * so that it does not create an empty file in case the remote file does not
  36. * exist or something else fails.
  37. */
  38. struct FtpFile {
  39. const char *filename;
  40. FILE *stream;
  41. };
  42. static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
  43. void *stream)
  44. {
  45. struct FtpFile *out = (struct FtpFile *)stream;
  46. if(!out->stream) {
  47. /* open file for writing */
  48. out->stream = fopen(out->filename, "wb");
  49. if(!out->stream)
  50. return 0; /* failure, cannot open file to write */
  51. }
  52. return fwrite(buffer, size, nmemb, out->stream);
  53. }
  54. int main(void)
  55. {
  56. CURL *curl;
  57. CURLcode res;
  58. struct FtpFile ftpfile = {
  59. "yourfile.bin", /* name to store the file as if successful */
  60. NULL
  61. };
  62. curl_global_init(CURL_GLOBAL_DEFAULT);
  63. curl = curl_easy_init();
  64. if(curl) {
  65. /*
  66. * You better replace the URL with one that works!
  67. */
  68. curl_easy_setopt(curl, CURLOPT_URL,
  69. "sftp://user@server/home/user/file.txt");
  70. /* Define our callback to get called when there is data to be written */
  71. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
  72. /* Set a pointer to our struct to pass to the callback */
  73. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
  74. #ifndef DISABLE_SSH_AGENT
  75. /* We activate ssh agent. For this to work you need
  76. to have ssh-agent running (type set | grep SSH_AGENT to check) or
  77. pageant on Windows (there is an icon in systray if so) */
  78. curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_AGENT);
  79. #endif
  80. /* Switch on full protocol/debug output */
  81. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  82. res = curl_easy_perform(curl);
  83. /* always cleanup */
  84. curl_easy_cleanup(curl);
  85. if(CURLE_OK != res) {
  86. /* we failed */
  87. fprintf(stderr, "curl told us %d\n", res);
  88. }
  89. }
  90. if(ftpfile.stream)
  91. fclose(ftpfile.stream); /* close the local file */
  92. curl_global_cleanup();
  93. return 0;
  94. }