|
|
@@ -87,7 +87,7 @@ int main(int argc, char *argv[]) |
|
|
LIBSSH2_CHANNEL *channel; |
|
|
int rc; |
|
|
int exitcode = 0; |
|
|
char *exitsignal=(char *)"none"; |
|
|
char *exitsignal = (char *)"none"; |
|
|
size_t len; |
|
|
LIBSSH2_KNOWNHOSTS *nh; |
|
|
int type; |
|
|
@@ -96,27 +96,27 @@ int main(int argc, char *argv[]) |
|
|
WSADATA wsadata; |
|
|
int err; |
|
|
|
|
|
err = WSAStartup(MAKEWORD(2,0), &wsadata); |
|
|
if (err != 0) { |
|
|
err = WSAStartup(MAKEWORD(2, 0), &wsadata); |
|
|
if(err != 0) { |
|
|
fprintf(stderr, "WSAStartup failed with error: %d\n", err); |
|
|
return 1; |
|
|
} |
|
|
#endif |
|
|
|
|
|
if (argc > 1) |
|
|
if(argc > 1) |
|
|
/* must be ip address only */ |
|
|
hostname = argv[1]; |
|
|
|
|
|
if (argc > 2) { |
|
|
if(argc > 2) { |
|
|
username = argv[2]; |
|
|
} |
|
|
if (argc > 3) { |
|
|
if(argc > 3) { |
|
|
password = argv[3]; |
|
|
} |
|
|
|
|
|
rc = libssh2_init (0); |
|
|
if (rc != 0) { |
|
|
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); |
|
|
rc = libssh2_init(0); |
|
|
if(rc != 0) { |
|
|
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); |
|
|
return 1; |
|
|
} |
|
|
|
|
|
@@ -131,15 +131,15 @@ int main(int argc, char *argv[]) |
|
|
sin.sin_family = AF_INET; |
|
|
sin.sin_port = htons(22); |
|
|
sin.sin_addr.s_addr = hostaddr; |
|
|
if (connect(sock, (struct sockaddr*)(&sin), |
|
|
if(connect(sock, (struct sockaddr*)(&sin), |
|
|
sizeof(struct sockaddr_in)) != 0) { |
|
|
fprintf(stderr, "failed to connect!\n"); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
/* Create a session instance */ |
|
|
session = libssh2_session_init(); |
|
|
if (!session) |
|
|
if(!session) |
|
|
return -1; |
|
|
|
|
|
/* tell libssh2 we want it all done non-blocking */ |
|
|
@@ -148,9 +148,9 @@ int main(int argc, char *argv[]) |
|
|
/* ... start it up. This will trade welcome banners, exchange keys, |
|
|
* and setup crypto, compression, and MAC layers |
|
|
*/ |
|
|
while ((rc = libssh2_session_handshake(session, sock)) == |
|
|
while((rc = libssh2_session_handshake(session, sock)) == |
|
|
LIBSSH2_ERROR_EAGAIN); |
|
|
if (rc) { |
|
|
if(rc) { |
|
|
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); |
|
|
return -1; |
|
|
} |
|
|
@@ -193,11 +193,11 @@ int main(int argc, char *argv[]) |
|
|
} |
|
|
libssh2_knownhost_free(nh); |
|
|
|
|
|
if ( strlen(password) != 0 ) { |
|
|
if(strlen(password) != 0) { |
|
|
/* We could authenticate via password */ |
|
|
while ((rc = libssh2_userauth_password(session, username, password)) == |
|
|
LIBSSH2_ERROR_EAGAIN); |
|
|
if (rc) { |
|
|
while((rc = libssh2_userauth_password(session, username, password)) == |
|
|
LIBSSH2_ERROR_EAGAIN); |
|
|
if(rc) { |
|
|
fprintf(stderr, "Authentication by password failed.\n"); |
|
|
exit(1); |
|
|
} |
|
|
@@ -206,22 +206,22 @@ int main(int argc, char *argv[]) |
|
|
libssh2_trace(session, LIBSSH2_TRACE_SOCKET); |
|
|
|
|
|
/* Exec non-blocking on the remove host */ |
|
|
while( (channel = libssh2_channel_open_session(session)) == NULL && |
|
|
libssh2_session_last_error(session,NULL,NULL,0) == |
|
|
LIBSSH2_ERROR_EAGAIN ) { |
|
|
while((channel = libssh2_channel_open_session(session)) == NULL && |
|
|
libssh2_session_last_error(session, NULL, NULL, 0) == |
|
|
LIBSSH2_ERROR_EAGAIN) { |
|
|
waitsocket(sock, session); |
|
|
} |
|
|
if( channel == NULL ) { |
|
|
fprintf(stderr,"Error\n"); |
|
|
exit( 1 ); |
|
|
if(channel == NULL) { |
|
|
fprintf(stderr, "Error\n"); |
|
|
exit(1); |
|
|
} |
|
|
while( (rc = libssh2_channel_exec(channel, commandline)) == |
|
|
LIBSSH2_ERROR_EAGAIN ) |
|
|
while((rc = libssh2_channel_exec(channel, commandline)) == |
|
|
LIBSSH2_ERROR_EAGAIN) |
|
|
waitsocket(sock, session); |
|
|
|
|
|
if( rc != 0 ) { |
|
|
if(rc != 0) { |
|
|
fprintf(stderr, "exec error\n"); |
|
|
exit( 1 ); |
|
|
exit(1); |
|
|
} |
|
|
else { |
|
|
LIBSSH2_POLLFD *fds = NULL; |
|
|
@@ -236,10 +236,11 @@ int main(int argc, char *argv[]) |
|
|
int rewrites = 0; |
|
|
int i; |
|
|
|
|
|
for (i = 0; i < BUFSIZE; i++) |
|
|
for(i = 0; i < BUFSIZE; i++) |
|
|
buffer[i] = 'A'; |
|
|
|
|
|
if ((fds = malloc (sizeof (LIBSSH2_POLLFD))) == NULL) { |
|
|
fds = malloc(sizeof (LIBSSH2_POLLFD)); |
|
|
if(!fds) { |
|
|
fprintf(stderr, "malloc failed\n"); |
|
|
exit(1); |
|
|
} |
|
|
@@ -252,18 +253,18 @@ int main(int argc, char *argv[]) |
|
|
int rc = (libssh2_poll(fds, 1, 10)); |
|
|
int act = 0; |
|
|
|
|
|
if (rc < 1) |
|
|
if(rc < 1) |
|
|
continue; |
|
|
|
|
|
if (fds[0].revents & LIBSSH2_POLLFD_POLLIN) { |
|
|
if(fds[0].revents & LIBSSH2_POLLFD_POLLIN) { |
|
|
int n = libssh2_channel_read(channel, buffer, sizeof(buffer)); |
|
|
act++; |
|
|
|
|
|
if (n == LIBSSH2_ERROR_EAGAIN) { |
|
|
if(n == LIBSSH2_ERROR_EAGAIN) { |
|
|
rereads++; |
|
|
fprintf(stderr, "will read again\n"); |
|
|
} |
|
|
else if (n < 0) { |
|
|
else if(n < 0) { |
|
|
fprintf(stderr, "read failed\n"); |
|
|
exit(1); |
|
|
} |
|
|
@@ -274,41 +275,42 @@ int main(int argc, char *argv[]) |
|
|
} |
|
|
} |
|
|
|
|
|
if (fds[0].revents & LIBSSH2_POLLFD_POLLOUT) { |
|
|
if(fds[0].revents & LIBSSH2_POLLFD_POLLOUT) { |
|
|
act++; |
|
|
|
|
|
if (totwritten < totsize) { |
|
|
if(totwritten < totsize) { |
|
|
/* we have not written all data yet */ |
|
|
int left = totsize - totwritten; |
|
|
int size = (left < bufsize) ? left : bufsize; |
|
|
int n = libssh2_channel_write_ex(channel, 0, buffer, size); |
|
|
|
|
|
if (n == LIBSSH2_ERROR_EAGAIN) { |
|
|
if(n == LIBSSH2_ERROR_EAGAIN) { |
|
|
rewrites++; |
|
|
fprintf(stderr, "will write again\n"); |
|
|
} |
|
|
else if (n < 0) { |
|
|
else if(n < 0) { |
|
|
fprintf(stderr, "write failed\n"); |
|
|
exit(1); |
|
|
} |
|
|
else { |
|
|
totwritten += n; |
|
|
fprintf(stderr, "wrote %d bytes (%d in total)", |
|
|
n, totwritten); |
|
|
if (left >= bufsize && n != bufsize) { |
|
|
if(left >= bufsize && n != bufsize) { |
|
|
partials++; |
|
|
fprintf(stderr, " PARTIAL"); |
|
|
} |
|
|
fprintf(stderr, "\n"); |
|
|
} |
|
|
} else { |
|
|
} |
|
|
else { |
|
|
/* all data written, send EOF */ |
|
|
rc = libssh2_channel_send_eof(channel); |
|
|
|
|
|
if (rc == LIBSSH2_ERROR_EAGAIN) { |
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) { |
|
|
fprintf(stderr, "will send eof again\n"); |
|
|
} |
|
|
else if (rc < 0) { |
|
|
else if(rc < 0) { |
|
|
fprintf(stderr, "send eof failed\n"); |
|
|
exit(1); |
|
|
} |
|
|
@@ -320,23 +322,23 @@ int main(int argc, char *argv[]) |
|
|
} |
|
|
} |
|
|
|
|
|
if (fds[0].revents & LIBSSH2_POLLFD_CHANNEL_CLOSED) { |
|
|
if (!act) /* don't leave loop until we have read all data */ |
|
|
if(fds[0].revents & LIBSSH2_POLLFD_CHANNEL_CLOSED) { |
|
|
if(!act) /* don't leave loop until we have read all data */ |
|
|
running = 0; |
|
|
} |
|
|
} while(running); |
|
|
|
|
|
exitcode = 127; |
|
|
while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN ) |
|
|
while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) |
|
|
waitsocket(sock, session); |
|
|
|
|
|
if( rc == 0 ) { |
|
|
exitcode = libssh2_channel_get_exit_status( channel ); |
|
|
if(rc == 0) { |
|
|
exitcode = libssh2_channel_get_exit_status(channel); |
|
|
libssh2_channel_get_exit_signal(channel, &exitsignal, |
|
|
NULL, NULL, NULL, NULL, NULL); |
|
|
} |
|
|
|
|
|
if (exitsignal) |
|
|
if(exitsignal) |
|
|
fprintf(stderr, "\nGot signal: %s\n", exitsignal); |
|
|
|
|
|
libssh2_channel_free(channel); |
|
|
@@ -345,7 +347,7 @@ int main(int argc, char *argv[]) |
|
|
fprintf(stderr, "\nrereads: %d rewrites: %d totwritten %d\n", |
|
|
rereads, rewrites, totwritten); |
|
|
|
|
|
if (totwritten != totread) { |
|
|
if(totwritten != totread) { |
|
|
fprintf(stderr, "\n*** FAIL bytes written: %d bytes " |
|
|
"read: %d ***\n", totwritten, totread); |
|
|
exit(1); |
|
|
|