2
* Sample implementation of a tiny SCP downloader client
6
Copyright 2009 Aris Adamantiadis
8
This file is part of the SSH Library
10
You are free to copy this file, modify it in any way, consider it being public
11
domain. This does not apply to the rest of the library though, but it is
12
allowed to cut-and-paste working code from this file to any license of
22
#include <libssh/libssh.h>
23
#include "examples_common.h"
26
const char *createcommand="rm -fr /tmp/libssh_tests && mkdir /tmp/libssh_tests && cd /tmp/libssh_tests && date > a && date > b && mkdir c && date > d";
28
static void usage(const char *argv0){
29
fprintf(stderr,"Usage : %s [options] host\n"
30
"sample tiny scp downloader client - libssh-%s\n"
31
"This program will create files in /tmp and try to fetch them\n",
33
// " -r : use RSA to verify host public key\n",
39
static int opts(int argc, char **argv){
41
while((i=getopt(argc,argv,"v"))!=-1){
47
fprintf(stderr,"unknown option %c\n",optopt);
58
static void create_files(ssh_session session){
59
ssh_channel channel=channel_new(session);
62
fprintf(stderr,"Error creating channel: %s\n",ssh_get_error(session));
65
if(channel_open_session(channel) != SSH_OK){
66
fprintf(stderr,"Error creating channel: %s\n",ssh_get_error(session));
69
if(channel_request_exec(channel,createcommand) != SSH_OK){
70
fprintf(stderr,"Error executing command: %s\n",ssh_get_error(session));
73
while(!channel_is_eof(channel)){
74
channel_read(channel,buffer,1,1);
77
channel_close(channel);
78
channel_free(channel);
82
static int fetch_files(ssh_session session){
88
ssh_scp scp=ssh_scp_new(session, SSH_SCP_READ | SSH_SCP_RECURSIVE, "/tmp/libssh_tests/*");
89
if(ssh_scp_init(scp) != SSH_OK){
90
fprintf(stderr,"error initializing scp: %s\n",ssh_get_error(session));
93
printf("Trying to download 3 files (a,b,d) and 1 directory (c)\n");
96
r=ssh_scp_pull_request(scp);
98
case SSH_SCP_REQUEST_NEWFILE:
99
size=ssh_scp_request_get_size(scp);
100
filename=strdup(ssh_scp_request_get_filename(scp));
101
mode=ssh_scp_request_get_permissions(scp);
102
printf("downloading file %s, size %d, perms 0%o\n",filename,size,mode);
104
ssh_scp_accept_request(scp);
105
r=ssh_scp_read(scp,buffer,sizeof(buffer));
107
fprintf(stderr,"Error reading scp: %s\n",ssh_get_error(session));
113
fprintf(stderr,"Error: %s\n",ssh_get_error(session));
115
case SSH_SCP_REQUEST_WARNING:
116
fprintf(stderr,"Warning: %s\n",ssh_scp_request_get_warning(scp));
118
case SSH_SCP_REQUEST_NEWDIR:
119
filename=strdup(ssh_scp_request_get_filename(scp));
120
mode=ssh_scp_request_get_permissions(scp);
121
printf("downloading directory %s, perms 0%o\n",filename,mode);
123
ssh_scp_accept_request(scp);
125
case SSH_SCP_REQUEST_ENDDIR:
126
printf("End of directory\n");
128
case SSH_SCP_REQUEST_EOF:
129
printf("End of requests\n");
137
int main(int argc, char **argv){
139
if(opts(argc,argv)<0)
141
session=connect_ssh(host,NULL,verbosity);
144
create_files(session);
145
fetch_files(session);
146
ssh_disconnect(session);