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:
@ -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++;
|
if (ssh_options_getopt(session, &argc, argv)) {
|
||||||
break;
|
fprintf(stderr,
|
||||||
default:
|
"Error parsing command line: %s\n",
|
||||||
fprintf(stderr, "unknown option %c\n", optopt);
|
ssh_get_error(session));
|
||||||
|
ssh_free(session);
|
||||||
|
ssh_finalize();
|
||||||
usage(argv[0]);
|
usage(argv[0]);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
destination = argv[optind];
|
|
||||||
if (destination == NULL) {
|
|
||||||
usage(argv[0]);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
|
||||||
ssh_session session;
|
|
||||||
|
|
||||||
if (opts(argc, argv) < 0) {
|
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
if (argc < 1) {
|
||||||
session = connect_ssh(destination, NULL, verbosity);
|
usage(argv[0]);
|
||||||
if (session == NULL) {
|
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
destination = argv[1];
|
||||||
|
|
||||||
|
if (ssh_options_set(session, SSH_OPTIONS_HOST, destination) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ssh_connect(session)) {
|
||||||
|
fprintf(stderr, "Connection failed : %s\n", ssh_get_error(session));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
state = verify_knownhost(session);
|
||||||
|
if (state != 0) {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user