summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2015-09-05 21:32:16 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2015-09-05 21:32:16 +0200
commit45e154f4e289f898ef4a49212239770c4ec20f04 (patch)
tree11c527e2132bab9e2b5a657ed21bc83b7e177b29
parentac36359ba77810dbc708b2eed3298bbd1a27e20d (diff)
downloadcssh-45e154f4e289f898ef4a49212239770c4ec20f04.tar.gz
cssh-45e154f4e289f898ef4a49212239770c4ec20f04.tar.bz2
parsing some scp arguments, working download
-rw-r--r--src/cssh.c109
1 files changed, 90 insertions, 19 deletions
diff --git a/src/cssh.c b/src/cssh.c
index be138b4..8ad2b09 100644
--- a/src/cssh.c
+++ b/src/cssh.c
@@ -373,6 +373,12 @@ static int read_hosts_file( const char *hosts_file, unsigned short default_port,
if( strlen( buf ) > 0 && buf[strlen( buf )-1] == '\n' ) {
buf[strlen(buf)-1] = '\0';
}
+
+ // ignore comment lines
+ if( buf[0] == '#' ) {
+ continue;
+ }
+
// the user can be part of the hostname, libssh allows this,
// but we must parse the port separately
char *p = strchr( buf, ':' );
@@ -485,16 +491,23 @@ int main( int argc, char *argv[] )
// command line parsing and reading of additional files like the hosts file
unsigned int nof_sessions = 0;
- unsigned int command_pos = 0;
char **host = NULL;
unsigned short *port = NULL;
char cmd[1024];
+ copy_direction_e copy_direction = CSSH_COPY_DIRECTION_DOWNLOAD;
+ char *local_directory = NULL;
+ char *remote_directory = NULL;
switch( execution_mode ) {
case CSSH_EXECUTE_AS_SSH: {
+ unsigned int command_pos = 0;
+
// command line arguments are '[user@]host'
if( !args_info.hosts_file_given ) {
unsigned short default_port = get_default_ssh_port( );
+ if( args_info.port_given ) {
+ default_port = args_info.port_arg;
+ }
host = (char **)malloc( sizeof( char * ) );
port = (unsigned short *)malloc( sizeof( unsigned short ) );
if( args_info.inputs_num >= 1 ) {
@@ -506,9 +519,6 @@ int main( int argc, char *argv[] )
host[0] = strdup( "localhost" );
port[0] = default_port;
}
- if( args_info.port_given ) {
- port[0] = args_info.port_arg;
- }
}
// compose command to be executed in SSH mode
@@ -539,7 +549,81 @@ int main( int argc, char *argv[] )
fprintf( stderr, "ERROR: Expecting a source and a destination when copying, encountered more arguments\n" );
exit( EXIT_FAILURE );
}
+ }
+
+ // parse 2 arguments and see where we have a local
+ // path and where a host destination with path
+ // host destination can be explicit (one without -H hosts
+ // parameter) or it is a placeholder host user@host:/dira/dirb,
+ // so :/adir is a way or user@:/adir or host:/adir or user@host:adir
+ // also host: and user@host: for home dir on other side, this means
+ // we also have user@: for home on all hosts in -H hosts or
+ // just : for the home directory on all hosts in -H hosts.
+ // seems quite logical (at least to me :-))
+
+ unsigned short default_port = get_default_ssh_port( );
+ host = (char **)malloc( sizeof( char * ) );
+ port = (unsigned short *)malloc( sizeof( unsigned short ) );
+ if( args_info.port_given ) {
+ default_port = args_info.port_arg;
+ }
+
+ char *s = strdup( args_info.inputs[0] );
+ char *s2 = NULL;
+ char *p = s;
+ while( *p != '\0' ) {
+ if( *p == ':' ) {
+ *p = '\0';
+ s2 = p+1;
+ host[0] = strdup( s );
+ port[0] = default_port;
+ nof_sessions = 1;
+ copy_direction = CSSH_COPY_DIRECTION_DOWNLOAD;
+ }
+ p++;
+ }
+ if( s2 != NULL ) {
+ if( nof_sessions > 0 ) {
+ remote_directory = strdup( s2 );
+ } else {
+ local_directory = strdup( s2 );
+ }
+ } else {
+ local_directory = strdup( s );
}
+ free( s );
+
+ s = strdup( args_info.inputs[1] );
+ p = s;
+ s2 = NULL;
+ while( *p != '\0' ) {
+ if( *p == ':' ) {
+ if( nof_sessions == 0 ) {
+ *p = '\0';
+ s2 = p+1;
+ host[0] = strdup( s );
+ port[0] = default_port;
+ nof_sessions = 1;
+ copy_direction = CSSH_COPY_DIRECTION_UPLOAD;
+ } else {
+ free( s );
+ fprintf( stderr, "ERROR: specified a host for source and destination, please use a local directory for one of them\n" );
+ exit( EXIT_FAILURE );
+ }
+ }
+ p++;
+ }
+ if( s2 != NULL ) {
+ if( nof_sessions > 0 ) {
+ remote_directory = strdup( s2 );
+ } else {
+ local_directory = strdup( s2 );
+ }
+ } else {
+ local_directory = strdup( s );
+ }
+ free( s );
+
} break;
case CSSH_EXECUTE_UNKNOWN:
@@ -564,7 +648,7 @@ int main( int argc, char *argv[] )
nof_sessions = nof_hosts;
}
- // initialize session with parameters read
+ // initialize session with host, port and user parameters read
session = (ssh_session *)malloc( nof_sessions * sizeof( ssh_session ) );
if( session == NULL ) {
@@ -942,19 +1026,6 @@ int main( int argc, char *argv[] )
case CSSH_EXECUTE_AS_SCP: {
- // TODO: parse 2 arguments and see where we have a local
- // path and where a host destination with path
- // TODO: host destination can be explicit (one without -H hosts
- // parameter) or it is a placeholder host user@host:/dira/dirb,
- // so :/adir is a way or user@:/adir or host:/adir or user@host:adir
- // also host: and user@host: for home dir on other side, this means
- // we also have user@: for home on all hosts in -H hosts or
- // just : for the home directory on all hosts in -H hosts.
- // seems quite logical (at least to me :-))
-
- copy_direction_e copy_direction = CSSH_COPY_DIRECTION_DOWNLOAD;
- const char *local_directory = "adir";
-
ssh_scp *scp = (ssh_scp *)malloc( ( nof_sessions + 1 ) * sizeof( ssh_scp ) );
memset( scp, 0, nof_sessions + 1 );
if( scp == NULL ) {
@@ -966,7 +1037,7 @@ int main( int argc, char *argv[] )
scp[i] = ssh_scp_new( session[i],
( ( copy_direction == CSSH_COPY_DIRECTION_UPLOAD ) ? SSH_SCP_WRITE : SSH_SCP_READ ) |
( ( args_info.recursive_given > 0 ) ? SSH_SCP_RECURSIVE : 0 ),
- local_directory );
+ remote_directory );
if( scp[i] == NULL ) {
fprintf( stderr, "ERROR: Unable to open SCP channel: %s\n",
ssh_get_error( session[i] ) );