diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2015-09-04 21:23:52 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2015-09-04 21:23:52 +0200 |
commit | ac36359ba77810dbc708b2eed3298bbd1a27e20d (patch) | |
tree | 65e71021c2c7111e179b31020131f2828fd60bf8 | |
parent | 363d80d88e25f6346e85a9e84bbb23da3107a3e7 (diff) | |
download | cssh-ac36359ba77810dbc708b2eed3298bbd1a27e20d.tar.gz cssh-ac36359ba77810dbc708b2eed3298bbd1a27e20d.tar.bz2 |
better way to determine default port for SSH
-rw-r--r-- | src/cssh.c | 16 |
1 files changed, 14 insertions, 2 deletions
@@ -13,6 +13,7 @@ #include <pwd.h> #include <inttypes.h> #include <unistd.h> +#include <netdb.h> #include "msleep.h" @@ -20,6 +21,7 @@ #include "version.h" #define BUFSIZE 4096 +#define DEFAULT_SSH_PORT 22 typedef enum auth_state_e { CSSH_AUTH_INIT, @@ -161,6 +163,16 @@ ASK_AGAIN: return true; } +static unsigned short get_default_ssh_port( ) +{ + struct servent *ent = getservbyname( "ssh", "tcp" ); + if( ent != NULL ) { + return (unsigned short)ntohs( ent->s_port ); + } else { + return DEFAULT_SSH_PORT; + } +} + static int accept_server( ssh_session session, unsigned char *hash, size_t hlen ) { char *hexa; @@ -482,7 +494,7 @@ int main( int argc, char *argv[] ) case CSSH_EXECUTE_AS_SSH: { // command line arguments are '[user@]host' if( !args_info.hosts_file_given ) { - unsigned short default_port = 22; + unsigned short default_port = get_default_ssh_port( ); host = (char **)malloc( sizeof( char * ) ); port = (unsigned short *)malloc( sizeof( unsigned short ) ); if( args_info.inputs_num >= 1 ) { @@ -541,7 +553,7 @@ int main( int argc, char *argv[] ) // for the hosts in the file containing hosts unsigned nof_hosts; - unsigned short default_port = 22; + unsigned short default_port = get_default_ssh_port( ); if( args_info.port_given ) { default_port = args_info.port_arg; } |