mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-12-08 03:42:12 +03:00
examples: Support passing port to libssh_scp to simplify testing
Signed-off-by: Jakub Jelen <jjelen@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
@@ -21,7 +21,7 @@ clients must be made or how a client should react.
|
|||||||
#include "examples_common.h"
|
#include "examples_common.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
ssh_session connect_ssh(const char *host, const char *user, int verbosity)
|
ssh_session connect_ssh(const char *host, const char *port, const char *user, int verbosity)
|
||||||
{
|
{
|
||||||
ssh_session session = NULL;
|
ssh_session session = NULL;
|
||||||
int auth = 0;
|
int auth = 0;
|
||||||
@@ -38,6 +38,13 @@ ssh_session connect_ssh(const char *host, const char *user, int verbosity)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (port != NULL) {
|
||||||
|
if (ssh_options_set(session, SSH_OPTIONS_PORT_STR, port) < 0) {
|
||||||
|
ssh_free(session);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (ssh_options_set(session, SSH_OPTIONS_HOST, host) < 0) {
|
if (ssh_options_set(session, SSH_OPTIONS_HOST, host) < 0) {
|
||||||
ssh_free(session);
|
ssh_free(session);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -21,6 +21,6 @@ clients must be made or how a client should react.
|
|||||||
int authenticate_console(ssh_session session);
|
int authenticate_console(ssh_session session);
|
||||||
int authenticate_kbdint(ssh_session session, const char *password);
|
int authenticate_kbdint(ssh_session session, const char *password);
|
||||||
int verify_knownhost(ssh_session session);
|
int verify_knownhost(ssh_session session);
|
||||||
ssh_session connect_ssh(const char *hostname, const char *user, int verbosity);
|
ssh_session connect_ssh(const char *hostname, const char *port, const char *user, int verbosity);
|
||||||
|
|
||||||
#endif /* EXAMPLES_COMMON_H_ */
|
#endif /* EXAMPLES_COMMON_H_ */
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ int main(void) {
|
|||||||
int rbytes, wbytes, total = 0;
|
int rbytes, wbytes, total = 0;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
session = connect_ssh("localhost", NULL, 0);
|
session = connect_ssh("localhost", NULL, NULL, 0);
|
||||||
if (session == NULL) {
|
if (session == NULL) {
|
||||||
ssh_finalize();
|
ssh_finalize();
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ static char **sources = NULL;
|
|||||||
static int nsources;
|
static int nsources;
|
||||||
static char *destination = NULL;
|
static char *destination = NULL;
|
||||||
static int verbosity = 0;
|
static int verbosity = 0;
|
||||||
|
static char *port = NULL;
|
||||||
|
|
||||||
struct location {
|
struct location {
|
||||||
int is_ssh;
|
int is_ssh;
|
||||||
@@ -49,9 +50,10 @@ enum {
|
|||||||
static void usage(const char *argv0) {
|
static void usage(const char *argv0) {
|
||||||
fprintf(stderr, "Usage : %s [options] [[user@]host1:]file1 ... \n"
|
fprintf(stderr, "Usage : %s [options] [[user@]host1:]file1 ... \n"
|
||||||
" [[user@]host2:]destination\n"
|
" [[user@]host2:]destination\n"
|
||||||
"sample scp client - libssh-%s\n",
|
"sample scp client - libssh-%s\n"
|
||||||
// "Options :\n",
|
"Options :\n"
|
||||||
// " -r : use RSA to verify host public key\n",
|
" -P : use port to connect to remote host\n"
|
||||||
|
" -v : increase verbosity of libssh. Can be used multiple times\n",
|
||||||
argv0,
|
argv0,
|
||||||
ssh_version(0));
|
ssh_version(0));
|
||||||
exit(0);
|
exit(0);
|
||||||
@@ -60,11 +62,14 @@ static void usage(const char *argv0) {
|
|||||||
static int opts(int argc, char **argv) {
|
static int opts(int argc, char **argv) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
while((i = getopt(argc, argv, "v")) != -1) {
|
while((i = getopt(argc, argv, "P:v")) != -1) {
|
||||||
switch(i) {
|
switch(i) {
|
||||||
case 'v':
|
case 'v':
|
||||||
verbosity++;
|
verbosity++;
|
||||||
break;
|
break;
|
||||||
|
case 'P':
|
||||||
|
port = optarg;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "unknown option %c\n", optopt);
|
fprintf(stderr, "unknown option %c\n", optopt);
|
||||||
usage(argv[0]);
|
usage(argv[0]);
|
||||||
@@ -183,7 +188,7 @@ static void close_location(struct location *loc) {
|
|||||||
|
|
||||||
static int open_location(struct location *loc, int flag) {
|
static int open_location(struct location *loc, int flag) {
|
||||||
if (loc->is_ssh && flag == WRITE) {
|
if (loc->is_ssh && flag == WRITE) {
|
||||||
loc->session = connect_ssh(loc->host, loc->user, verbosity);
|
loc->session = connect_ssh(loc->host, port, loc->user, verbosity);
|
||||||
if (!loc->session) {
|
if (!loc->session) {
|
||||||
fprintf(stderr, "Couldn't connect to %s\n", loc->host);
|
fprintf(stderr, "Couldn't connect to %s\n", loc->host);
|
||||||
return -1;
|
return -1;
|
||||||
@@ -209,7 +214,7 @@ static int open_location(struct location *loc, int flag) {
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
} else if (loc->is_ssh && flag == READ) {
|
} else if (loc->is_ssh && flag == READ) {
|
||||||
loc->session = connect_ssh(loc->host, loc->user, verbosity);
|
loc->session = connect_ssh(loc->host, port, loc->user, verbosity);
|
||||||
if (!loc->session) {
|
if (!loc->session) {
|
||||||
fprintf(stderr, "Couldn't connect to %s\n", loc->host);
|
fprintf(stderr, "Couldn't connect to %s\n", loc->host);
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ int main(int argc, char **argv)
|
|||||||
ssh_session session = NULL;
|
ssh_session session = NULL;
|
||||||
if (opts(argc, argv) < 0)
|
if (opts(argc, argv) < 0)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
session = connect_ssh(host, NULL, verbosity);
|
session = connect_ssh(host, NULL, NULL, verbosity);
|
||||||
if (session == NULL)
|
if (session == NULL)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
create_files(session);
|
create_files(session);
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ int main(void)
|
|||||||
int rc;
|
int rc;
|
||||||
uint64_t total = 0;
|
uint64_t total = 0;
|
||||||
uint64_t lastshown = 4096;
|
uint64_t lastshown = 4096;
|
||||||
session = connect_ssh("localhost", NULL, 0);
|
session = connect_ssh("localhost", NULL, NULL, 0);
|
||||||
if (session == NULL) {
|
if (session == NULL) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ int main(void) {
|
|||||||
char buffer[1024*1024];
|
char buffer[1024*1024];
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
session = connect_ssh("localhost", NULL, 0);
|
session = connect_ssh("localhost", NULL, NULL, 0);
|
||||||
if (session == NULL) {
|
if (session == NULL) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user