From 2ec633f33db5060ec087ef6a923dc91795503faa Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 7 Nov 2017 15:28:52 +0100 Subject: examples: Rename samplessh to ssh-client Signed-off-by: Andreas Schneider --- examples/CMakeLists.txt | 4 +- examples/sample.c | 356 ------------------------------------------------ examples/ssh_client.c | 356 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 358 insertions(+), 358 deletions(-) delete mode 100644 examples/sample.c create mode 100644 examples/ssh_client.c (limited to 'examples') diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index e0f4878d..0b2bcffb 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -30,8 +30,8 @@ if (UNIX AND NOT WIN32) target_link_libraries(samplesftp ${LIBSSH_SHARED_LIBRARY}) endif (WITH_SFTP) - add_executable(samplessh sample.c ${examples_SRCS}) - target_link_libraries(samplessh ${LIBSSH_SHARED_LIBRARY}) + add_executable(ssh-client ssh_client.c ${examples_SRCS}) + target_link_libraries(ssh-client ${LIBSSH_SHARED_LIBRARY}) if (WITH_SERVER AND ARGP_LIBRARY) if (HAVE_LIBUTIL) diff --git a/examples/sample.c b/examples/sample.c deleted file mode 100644 index 53f7f6c4..00000000 --- a/examples/sample.c +++ /dev/null @@ -1,356 +0,0 @@ -/* client.c */ -/* -Copyright 2003-2009 Aris Adamantiadis - -This file is part of the SSH Library - -You are free to copy this file, modify it in any way, consider it being public -domain. This does not apply to the rest of the library though, but it is -allowed to cut-and-paste working code from this file to any license of -program. -The goal is to show the API in action. It's not a reference on how terminal -clients must be made or how a client should react. -*/ - -#include "config.h" -#include -#include -#include - -#include -#include - -#ifdef HAVE_TERMIOS_H -#include -#endif -#ifdef HAVE_UNISTD_H -#include -#endif -#ifdef HAVE_PTY_H -#include -#endif - -#include -#include -#include -#include - -#include -#include -#include - - -#include "examples_common.h" -#define MAXCMD 10 - -static char *host; -static char *user; -static char *cmds[MAXCMD]; -static struct termios terminal; - -static char *pcap_file=NULL; - -static char *proxycommand; - -static int auth_callback(const char *prompt, char *buf, size_t len, - int echo, int verify, void *userdata) { - (void) verify; - (void) userdata; - - return ssh_getpass(prompt, buf, len, echo, verify); -} - -struct ssh_callbacks_struct cb = { - .auth_function=auth_callback, - .userdata=NULL -}; - -static void add_cmd(char *cmd){ - int n; - - for (n = 0; (n < MAXCMD) && cmds[n] != NULL; n++); - - if (n == MAXCMD) { - return; - } - cmds[n]=strdup(cmd); -} - -static void usage(void) -{ - fprintf(stderr, - "Usage : ssh [options] [login@]hostname\n" - "sample client - libssh-%s\n" - "Options :\n" - " -l user : log in as user\n" - " -p port : connect to port\n" - " -d : use DSS to verify host public key\n" - " -r : use RSA to verify host public key\n" -#ifdef WITH_PCAP - " -P file : create a pcap debugging file\n" -#endif -#ifndef _WIN32 - " -T proxycommand : command to execute as a socket proxy\n" -#endif - "\n", - ssh_version(0)); - - exit(0); -} - -static int opts(int argc, char **argv){ - int i; -// for(i=0;ic_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); - termios_p->c_oflag &= ~OPOST; - termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); - termios_p->c_cflag &= ~(CSIZE|PARENB); - termios_p->c_cflag |= CS8; -} -#endif - - -static void do_cleanup(int i) { - /* unused variable */ - (void) i; - - tcsetattr(0,TCSANOW,&terminal); -} - -static void do_exit(int i) { - /* unused variable */ - (void) i; - - do_cleanup(0); - exit(0); -} - -ssh_channel chan; -int signal_delayed=0; - -static void sigwindowchanged(int i){ - (void) i; - signal_delayed=1; -} - -static void setsignal(void){ - signal(SIGWINCH, sigwindowchanged); - signal_delayed=0; -} - -static void sizechanged(void){ - struct winsize win = { 0, 0, 0, 0 }; - ioctl(1, TIOCGWINSZ, &win); - ssh_channel_change_pty_size(chan,win.ws_col, win.ws_row); -// printf("Changed pty size\n"); - setsignal(); -} - -static void select_loop(ssh_session session,ssh_channel channel){ - ssh_connector connector_in, connector_out, connector_err; - ssh_event event = ssh_event_new(); - - /* stdin */ - connector_in = ssh_connector_new(session); - ssh_connector_set_out_channel(connector_in, channel, SSH_CONNECTOR_STDOUT); - ssh_connector_set_in_fd(connector_in, 0); - ssh_event_add_connector(event, connector_in); - - /* stdout */ - connector_out = ssh_connector_new(session); - ssh_connector_set_out_fd(connector_out, 1); - ssh_connector_set_in_channel(connector_out, channel, SSH_CONNECTOR_STDOUT); - ssh_event_add_connector(event, connector_out); - - /* stderr */ - connector_err = ssh_connector_new(session); - ssh_connector_set_out_fd(connector_err, 2); - ssh_connector_set_in_channel(connector_err, channel, SSH_CONNECTOR_STDERR); - ssh_event_add_connector(event, connector_err); - - while(ssh_channel_is_open(channel)){ - if(signal_delayed) - sizechanged(); - ssh_event_dopoll(event, 60000); - } - ssh_event_remove_connector(event, connector_in); - ssh_event_remove_connector(event, connector_out); - ssh_event_remove_connector(event, connector_err); - - ssh_connector_free(connector_in); - ssh_connector_free(connector_out); - ssh_connector_free(connector_err); - - ssh_event_free(event); - ssh_channel_free(channel); -} - -static void shell(ssh_session session){ - ssh_channel channel; - struct termios terminal_local; - int interactive=isatty(0); - channel = ssh_channel_new(session); - if(interactive){ - tcgetattr(0,&terminal_local); - memcpy(&terminal,&terminal_local,sizeof(struct termios)); - } - if(ssh_channel_open_session(channel)){ - printf("error opening channel : %s\n",ssh_get_error(session)); - return; - } - chan=channel; - if(interactive){ - ssh_channel_request_pty(channel); - sizechanged(); - } - if(ssh_channel_request_shell(channel)){ - printf("Requesting shell : %s\n",ssh_get_error(session)); - return; - } - if(interactive){ - cfmakeraw(&terminal_local); - tcsetattr(0,TCSANOW,&terminal_local); - setsignal(); - } - signal(SIGTERM,do_cleanup); - select_loop(session,channel); - if(interactive) - do_cleanup(0); -} - -static void batch_shell(ssh_session session){ - ssh_channel channel; - char buffer[1024]; - int i,s=0; - for(i=0;i +#include +#include + +#include +#include + +#ifdef HAVE_TERMIOS_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_PTY_H +#include +#endif + +#include +#include +#include +#include + +#include +#include +#include + + +#include "examples_common.h" +#define MAXCMD 10 + +static char *host; +static char *user; +static char *cmds[MAXCMD]; +static struct termios terminal; + +static char *pcap_file=NULL; + +static char *proxycommand; + +static int auth_callback(const char *prompt, char *buf, size_t len, + int echo, int verify, void *userdata) { + (void) verify; + (void) userdata; + + return ssh_getpass(prompt, buf, len, echo, verify); +} + +struct ssh_callbacks_struct cb = { + .auth_function=auth_callback, + .userdata=NULL +}; + +static void add_cmd(char *cmd){ + int n; + + for (n = 0; (n < MAXCMD) && cmds[n] != NULL; n++); + + if (n == MAXCMD) { + return; + } + cmds[n]=strdup(cmd); +} + +static void usage(void) +{ + fprintf(stderr, + "Usage : ssh [options] [login@]hostname\n" + "sample client - libssh-%s\n" + "Options :\n" + " -l user : log in as user\n" + " -p port : connect to port\n" + " -d : use DSS to verify host public key\n" + " -r : use RSA to verify host public key\n" +#ifdef WITH_PCAP + " -P file : create a pcap debugging file\n" +#endif +#ifndef _WIN32 + " -T proxycommand : command to execute as a socket proxy\n" +#endif + "\n", + ssh_version(0)); + + exit(0); +} + +static int opts(int argc, char **argv){ + int i; +// for(i=0;ic_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); + termios_p->c_oflag &= ~OPOST; + termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); + termios_p->c_cflag &= ~(CSIZE|PARENB); + termios_p->c_cflag |= CS8; +} +#endif + + +static void do_cleanup(int i) { + /* unused variable */ + (void) i; + + tcsetattr(0,TCSANOW,&terminal); +} + +static void do_exit(int i) { + /* unused variable */ + (void) i; + + do_cleanup(0); + exit(0); +} + +ssh_channel chan; +int signal_delayed=0; + +static void sigwindowchanged(int i){ + (void) i; + signal_delayed=1; +} + +static void setsignal(void){ + signal(SIGWINCH, sigwindowchanged); + signal_delayed=0; +} + +static void sizechanged(void){ + struct winsize win = { 0, 0, 0, 0 }; + ioctl(1, TIOCGWINSZ, &win); + ssh_channel_change_pty_size(chan,win.ws_col, win.ws_row); +// printf("Changed pty size\n"); + setsignal(); +} + +static void select_loop(ssh_session session,ssh_channel channel){ + ssh_connector connector_in, connector_out, connector_err; + ssh_event event = ssh_event_new(); + + /* stdin */ + connector_in = ssh_connector_new(session); + ssh_connector_set_out_channel(connector_in, channel, SSH_CONNECTOR_STDOUT); + ssh_connector_set_in_fd(connector_in, 0); + ssh_event_add_connector(event, connector_in); + + /* stdout */ + connector_out = ssh_connector_new(session); + ssh_connector_set_out_fd(connector_out, 1); + ssh_connector_set_in_channel(connector_out, channel, SSH_CONNECTOR_STDOUT); + ssh_event_add_connector(event, connector_out); + + /* stderr */ + connector_err = ssh_connector_new(session); + ssh_connector_set_out_fd(connector_err, 2); + ssh_connector_set_in_channel(connector_err, channel, SSH_CONNECTOR_STDERR); + ssh_event_add_connector(event, connector_err); + + while(ssh_channel_is_open(channel)){ + if(signal_delayed) + sizechanged(); + ssh_event_dopoll(event, 60000); + } + ssh_event_remove_connector(event, connector_in); + ssh_event_remove_connector(event, connector_out); + ssh_event_remove_connector(event, connector_err); + + ssh_connector_free(connector_in); + ssh_connector_free(connector_out); + ssh_connector_free(connector_err); + + ssh_event_free(event); + ssh_channel_free(channel); +} + +static void shell(ssh_session session){ + ssh_channel channel; + struct termios terminal_local; + int interactive=isatty(0); + channel = ssh_channel_new(session); + if(interactive){ + tcgetattr(0,&terminal_local); + memcpy(&terminal,&terminal_local,sizeof(struct termios)); + } + if(ssh_channel_open_session(channel)){ + printf("error opening channel : %s\n",ssh_get_error(session)); + return; + } + chan=channel; + if(interactive){ + ssh_channel_request_pty(channel); + sizechanged(); + } + if(ssh_channel_request_shell(channel)){ + printf("Requesting shell : %s\n",ssh_get_error(session)); + return; + } + if(interactive){ + cfmakeraw(&terminal_local); + tcsetattr(0,TCSANOW,&terminal_local); + setsignal(); + } + signal(SIGTERM,do_cleanup); + select_loop(session,channel); + if(interactive) + do_cleanup(0); +} + +static void batch_shell(ssh_session session){ + ssh_channel channel; + char buffer[1024]; + int i,s=0; + for(i=0;i