aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/CMakeLists.txt6
-rw-r--r--examples/scp_download.c145
2 files changed, 149 insertions, 2 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index b0d40d9..3dbf615 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -1,7 +1,6 @@
project(libssh-examples C)
set(examples_SRCS
- libssh_scp.c
authentication.c
knownhosts.c
connect_ssh.c
@@ -11,6 +10,9 @@ include_directories(
${LIBSSH_PUBLIC_INCLUDE_DIRS}
)
-add_executable(libssh_scp ${examples_SRCS})
+add_executable(libssh_scp libssh_scp.c ${examples_SRCS})
+add_executable(scp_download scp_download.c ${examples_SRCS})
target_link_libraries(libssh_scp ${LIBSSH_SHARED_LIBRARY})
+target_link_libraries(scp_download ${LIBSSH_SHARED_LIBRARY})
+
diff --git a/examples/scp_download.c b/examples/scp_download.c
new file mode 100644
index 0000000..f719515
--- /dev/null
+++ b/examples/scp_download.c
@@ -0,0 +1,145 @@
+/* scp_download.c
+ * Sample implementation of a tiny SCP downloader client
+ */
+
+/*
+Copyright 2009 Aris Adamantiadis
+
+This file is part of the SSH Library
+
+You are free to copy this file, modify it in any way, consider it being public
+domain. This does not apply to the rest of the library though, but it is
+allowed to cut-and-paste working code from this file to any license of
+program.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+#include <libssh/libssh.h>
+#include "examples_common.h"
+
+int verbosity=0;
+const char *createcommand="rm -fr /tmp/libssh_tests && mkdir /tmp/libssh_tests && cd /tmp/libssh_tests && date > a && date > b && mkdir c && date > d";
+char *host=NULL;
+static void usage(const char *argv0){
+ fprintf(stderr,"Usage : %s [options] host\n"
+ "sample tiny scp downloader client - libssh-%s\n"
+ "This program will create files in /tmp and try to fetch them\n",
+// "Options :\n",
+// " -r : use RSA to verify host public key\n",
+ argv0,
+ ssh_version(0));
+ exit(0);
+}
+
+static int opts(int argc, char **argv){
+ int i;
+ while((i=getopt(argc,argv,"v"))!=-1){
+ switch(i){
+ case 'v':
+ verbosity++;
+ break;
+ default:
+ fprintf(stderr,"unknown option %c\n",optopt);
+ usage(argv[0]);
+ return -1;
+ }
+ }
+ host = argv[optind];
+ if(host == NULL)
+ usage(argv[0]);
+ return 0;
+}
+
+static void create_files(ssh_session session){
+ ssh_channel channel=channel_new(session);
+ char buffer[1];
+ if(channel == NULL){
+ fprintf(stderr,"Error creating channel: %s\n",ssh_get_error(session));
+ exit(EXIT_FAILURE);
+ }
+ if(channel_open_session(channel) != SSH_OK){
+ fprintf(stderr,"Error creating channel: %s\n",ssh_get_error(session));
+ exit(EXIT_FAILURE);
+ }
+ if(channel_request_exec(channel,createcommand) != SSH_OK){
+ fprintf(stderr,"Error executing command: %s\n",ssh_get_error(session));
+ exit(EXIT_FAILURE);
+ }
+ while(!channel_is_eof(channel)){
+ channel_read(channel,buffer,1,1);
+ write(1,buffer,1);
+ }
+ channel_close(channel);
+ channel_free(channel);
+}
+
+
+static int fetch_files(ssh_session session){
+ int size;
+ char buffer[16384];
+ int mode;
+ char *filename;
+ int r;
+ ssh_scp scp=ssh_scp_new(session, SSH_SCP_READ, "/tmp/libssh_tests/*");
+ if(ssh_scp_init(scp) != SSH_OK){
+ fprintf(stderr,"error initializing scp: %s\n",ssh_get_error(session));
+ return -1;
+ }
+ printf("Trying to download 3 files (a,b,d) and 1 directory (c)\n");
+ do {
+
+ r=ssh_scp_pull_request(scp);
+ switch(r){
+ case SSH_SCP_REQUEST_NEWFILE:
+ size=ssh_scp_request_get_size(scp);
+ filename=strdup(ssh_scp_request_get_filename(scp));
+ mode=ssh_scp_request_get_permissions(scp);
+ printf("downloading file %s, size %d, perms 0%o\n",filename,size,mode);
+ free(filename);
+ ssh_scp_accept_request(scp);
+ r=ssh_scp_read(scp,buffer,sizeof(buffer));
+ if(r==SSH_ERROR){
+ fprintf(stderr,"Error reading scp: %s\n",ssh_get_error(session));
+ return -1;
+ }
+ printf("done\n");
+ break;
+ case SSH_ERROR:
+ fprintf(stderr,"Error: %s\n",ssh_get_error(session));
+ if(ssh_get_error_code(session)!=SSH_REQUEST_DENIED)
+ return -1;
+ break;
+ case SSH_SCP_REQUEST_NEWDIR:
+ filename=strdup(ssh_scp_request_get_filename(scp));
+ mode=ssh_scp_request_get_permissions(scp);
+ printf("downloading directory %s, perms 0%o\n",filename,mode);
+ free(filename);
+ ssh_scp_accept_request(scp);
+ break;
+ case SSH_SCP_REQUEST_EOF:
+ printf("End of requests\n");
+ goto end;
+ }
+ } while (1);
+ end:
+ return 0;
+}
+
+int main(int argc, char **argv){
+ ssh_session session;
+ if(opts(argc,argv)<0)
+ return EXIT_FAILURE;
+ session=connect_ssh(host,NULL,verbosity);
+ if(session == NULL)
+ return EXIT_FAILURE;
+ create_files(session);
+ fetch_files(session);
+ ssh_disconnect(session);
+ ssh_finalize();
+ return 0;
+}