From 07d099f652224d66530fc603662a034d026e5d13 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Tue, 16 Sep 2025 16:04:50 +0200 Subject: [PATCH] examples: Support passing port to libssh_scp to simplify testing Signed-off-by: Jakub Jelen Reviewed-by: Andreas Schneider --- examples/connect_ssh.c | 9 ++++++++- examples/examples_common.h | 2 +- examples/exec.c | 2 +- examples/libssh_scp.c | 17 +++++++++++------ examples/scp_download.c | 2 +- examples/senddata.c | 2 +- tests/chmodtest.c | 2 +- 7 files changed, 24 insertions(+), 12 deletions(-) diff --git a/examples/connect_ssh.c b/examples/connect_ssh.c index 1b9ecbda..b07f824d 100644 --- a/examples/connect_ssh.c +++ b/examples/connect_ssh.c @@ -21,7 +21,7 @@ clients must be made or how a client should react. #include "examples_common.h" #include -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; 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) { ssh_free(session); return NULL; diff --git a/examples/examples_common.h b/examples/examples_common.h index 86fffebe..6f5a1b12 100644 --- a/examples/examples_common.h +++ b/examples/examples_common.h @@ -21,6 +21,6 @@ clients must be made or how a client should react. int authenticate_console(ssh_session session); int authenticate_kbdint(ssh_session session, const char *password); 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_ */ diff --git a/examples/exec.c b/examples/exec.c index f90df364..9c1d8dbb 100644 --- a/examples/exec.c +++ b/examples/exec.c @@ -11,7 +11,7 @@ int main(void) { int rbytes, wbytes, total = 0; int rc; - session = connect_ssh("localhost", NULL, 0); + session = connect_ssh("localhost", NULL, NULL, 0); if (session == NULL) { ssh_finalize(); return 1; diff --git a/examples/libssh_scp.c b/examples/libssh_scp.c index 2c46a036..2b1a7627 100644 --- a/examples/libssh_scp.c +++ b/examples/libssh_scp.c @@ -30,6 +30,7 @@ static char **sources = NULL; static int nsources; static char *destination = NULL; static int verbosity = 0; +static char *port = NULL; struct location { int is_ssh; @@ -49,9 +50,10 @@ enum { static void usage(const char *argv0) { fprintf(stderr, "Usage : %s [options] [[user@]host1:]file1 ... \n" " [[user@]host2:]destination\n" - "sample scp client - libssh-%s\n", - // "Options :\n", - // " -r : use RSA to verify host public key\n", + "sample scp client - libssh-%s\n" + "Options :\n" + " -P : use port to connect to remote host\n" + " -v : increase verbosity of libssh. Can be used multiple times\n", argv0, ssh_version(0)); exit(0); @@ -60,11 +62,14 @@ static void usage(const char *argv0) { static int opts(int argc, char **argv) { int i; - while((i = getopt(argc, argv, "v")) != -1) { + while((i = getopt(argc, argv, "P:v")) != -1) { switch(i) { case 'v': verbosity++; break; + case 'P': + port = optarg; + break; default: fprintf(stderr, "unknown option %c\n", optopt); usage(argv[0]); @@ -183,7 +188,7 @@ static void close_location(struct location *loc) { static int open_location(struct location *loc, int flag) { 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) { fprintf(stderr, "Couldn't connect to %s\n", loc->host); return -1; @@ -209,7 +214,7 @@ static int open_location(struct location *loc, int flag) { } return 0; } 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) { fprintf(stderr, "Couldn't connect to %s\n", loc->host); return -1; diff --git a/examples/scp_download.c b/examples/scp_download.c index b9de72fb..24355e29 100644 --- a/examples/scp_download.c +++ b/examples/scp_download.c @@ -182,7 +182,7 @@ int main(int argc, char **argv) ssh_session session = NULL; if (opts(argc, argv) < 0) return EXIT_FAILURE; - session = connect_ssh(host, NULL, verbosity); + session = connect_ssh(host, NULL, NULL, verbosity); if (session == NULL) return EXIT_FAILURE; create_files(session); diff --git a/examples/senddata.c b/examples/senddata.c index 7bbc2ca0..5be65af3 100644 --- a/examples/senddata.c +++ b/examples/senddata.c @@ -13,7 +13,7 @@ int main(void) int rc; uint64_t total = 0; uint64_t lastshown = 4096; - session = connect_ssh("localhost", NULL, 0); + session = connect_ssh("localhost", NULL, NULL, 0); if (session == NULL) { return 1; } diff --git a/tests/chmodtest.c b/tests/chmodtest.c index 1e6f5112..60ac6a5b 100644 --- a/tests/chmodtest.c +++ b/tests/chmodtest.c @@ -10,7 +10,7 @@ int main(void) { char buffer[1024*1024]; int rc; - session = connect_ssh("localhost", NULL, 0); + session = connect_ssh("localhost", NULL, NULL, 0); if (session == NULL) { return 1; }