1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-07-28 01:41:48 +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
#endif
static int verbosity;
static char *destination;
static void do_sftp(ssh_session session) {
sftp_session sftp = sftp_new(session);
sftp_dir dir;
@ -244,50 +241,63 @@ static void usage(const char *argv0) {
fprintf(stderr, "Usage : %s [-v] remotehost\n"
"sample sftp test client - libssh-%s\n"
"Options :\n"
" -l user : log in as user\n"
" -p port : connect to port\n"
" -v : increase log verbosity\n",
argv0,
ssh_version(0));
exit(0);
}
static int opts(int argc, char **argv) {
int i;
int main(int argc, char **argv)
{
ssh_session session = NULL;
char *destination = NULL;
int auth = 0;
int state;
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;
}
}
ssh_init();
session = ssh_new();
destination = argv[optind];
if (destination == NULL) {
if (ssh_options_getopt(session, &argc, argv)) {
fprintf(stderr,
"Error parsing command line: %s\n",
ssh_get_error(session));
ssh_free(session);
ssh_finalize();
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 0;
}
int main(int argc, char **argv) {
ssh_session session;
if (opts(argc, argv) < 0) {
return EXIT_FAILURE;
if (ssh_connect(session)) {
fprintf(stderr, "Connection failed : %s\n", ssh_get_error(session));
return -1;
}
session = connect_ssh(destination, NULL, verbosity);
if (session == NULL) {
return EXIT_FAILURE;
state = verify_knownhost(session);
if (state != 0) {
return -1;
}
auth = authenticate_console(session);
if (auth != SSH_AUTH_SUCCESS) {
return -1;
}
do_sftp(session);
ssh_disconnect(session);
ssh_free(session);
ssh_finalize();
return 0;
}