sftpget.c 3.2 KB

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