1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-07-29 13:01:13 +03:00

examples: Support more options in the sftp client

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Norbert Pocs <npocs@redhat.com>
This commit is contained in:
Jakub Jelen
2022-12-16 16:08:09 +01:00
parent 834603c96b
commit 6b4c2a21bc

View File

@ -33,9 +33,6 @@ clients must be made or how a client should react.
#define BUF_SIZE 65536 #define BUF_SIZE 65536
#endif #endif
static int verbosity;
static char *destination;
static void do_sftp(ssh_session session) { static void do_sftp(ssh_session session) {
sftp_session sftp = sftp_new(session); sftp_session sftp = sftp_new(session);
sftp_dir dir; sftp_dir dir;
@ -244,50 +241,63 @@ static void usage(const char *argv0) {
fprintf(stderr, "Usage : %s [-v] remotehost\n" fprintf(stderr, "Usage : %s [-v] remotehost\n"
"sample sftp test client - libssh-%s\n" "sample sftp test client - libssh-%s\n"
"Options :\n" "Options :\n"
" -l user : log in as user\n"
" -p port : connect to port\n"
" -v : increase log verbosity\n", " -v : increase log verbosity\n",
argv0, argv0,
ssh_version(0)); ssh_version(0));
exit(0); exit(0);
} }
static int opts(int argc, char **argv) { int main(int argc, char **argv)
int i; {
ssh_session session = NULL;
char *destination = NULL;
int auth = 0;
int state;
while ((i = getopt(argc, argv, "v")) != -1) { ssh_init();
switch(i) { session = ssh_new();
case 'v':
verbosity++;
break;
default:
fprintf(stderr, "unknown option %c\n", optopt);
usage(argv[0]);
return -1;
}
}
destination = argv[optind]; if (ssh_options_getopt(session, &argc, argv)) {
if (destination == NULL) { fprintf(stderr,
"Error parsing command line: %s\n",
ssh_get_error(session));
ssh_free(session);
ssh_finalize();
usage(argv[0]); usage(argv[0]);
return EXIT_FAILURE;
}
if (argc < 1) {
usage(argv[0]);
return EXIT_FAILURE;
}
destination = argv[1];
if (ssh_options_set(session, SSH_OPTIONS_HOST, destination) < 0) {
return -1; return -1;
} }
return 0; if (ssh_connect(session)) {
} fprintf(stderr, "Connection failed : %s\n", ssh_get_error(session));
return -1;
int main(int argc, char **argv) {
ssh_session session;
if (opts(argc, argv) < 0) {
return EXIT_FAILURE;
} }
session = connect_ssh(destination, NULL, verbosity); state = verify_knownhost(session);
if (session == NULL) { if (state != 0) {
return EXIT_FAILURE; return -1;
}
auth = authenticate_console(session);
if (auth != SSH_AUTH_SUCCESS) {
return -1;
} }
do_sftp(session); do_sftp(session);
ssh_disconnect(session); ssh_disconnect(session);
ssh_free(session); ssh_free(session);
ssh_finalize();
return 0; return 0;
} }