examples: use stderr for messages, stdout for data

Reported by: Karel Srot
Bug: https://bugzilla.redhat.com/867462
This commit is contained in:
Kamil Dudka 2012-10-22 13:29:27 +02:00
parent e2bb780d77
commit b31e35aba6
20 changed files with 148 additions and 141 deletions

View File

@ -135,7 +135,7 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */ /* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username)); userauthlist = libssh2_userauth_list(session, username, strlen(username));
printf("Authentication methods: %s\n", userauthlist); fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password")) if (strstr(userauthlist, "password"))
auth |= AUTH_PASSWORD; auth |= AUTH_PASSWORD;
if (strstr(userauthlist, "publickey")) if (strstr(userauthlist, "publickey"))
@ -157,12 +157,12 @@ int main(int argc, char *argv[])
} else if (auth & AUTH_PUBLICKEY) { } else if (auth & AUTH_PUBLICKEY) {
if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, if (libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) { keyfile2, password)) {
printf("\tAuthentication by public key failed!\n"); fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} }
printf("\tAuthentication by public key succeeded.\n"); fprintf(stderr, "\tAuthentication by public key succeeded.\n");
} else { } else {
printf("No supported authentication methods found!\n"); fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown; goto shutdown;
} }
@ -185,7 +185,7 @@ int main(int argc, char *argv[])
goto shutdown; goto shutdown;
} }
printf("Waiting for TCP connection on %s:%d...\n", fprintf(stderr, "Waiting for TCP connection on %s:%d...\n",
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
forwardsock = accept(listensock, (struct sockaddr *)&sin, &sinlen); forwardsock = accept(listensock, (struct sockaddr *)&sin, &sinlen);
@ -197,8 +197,8 @@ int main(int argc, char *argv[])
shost = inet_ntoa(sin.sin_addr); shost = inet_ntoa(sin.sin_addr);
sport = ntohs(sin.sin_port); sport = ntohs(sin.sin_port);
printf("Forwarding connection from %s:%d here to remote %s:%d\n", shost, fprintf(stderr, "Forwarding connection from %s:%d here to remote %s:%d\n",
sport, remote_desthost, remote_destport); shost, sport, remote_desthost, remote_destport);
channel = libssh2_channel_direct_tcpip_ex(session, remote_desthost, channel = libssh2_channel_direct_tcpip_ex(session, remote_desthost,
remote_destport, shost, sport); remote_destport, shost, sport);
@ -228,7 +228,8 @@ int main(int argc, char *argv[])
perror("read"); perror("read");
goto shutdown; goto shutdown;
} else if (0 == len) { } else if (0 == len) {
printf("The client at %s:%d disconnected!\n", shost, sport); fprintf(stderr, "The client at %s:%d disconnected!\n", shost,
sport);
goto shutdown; goto shutdown;
} }
wr = 0; wr = 0;
@ -259,7 +260,7 @@ int main(int argc, char *argv[])
wr += i; wr += i;
} }
if (libssh2_channel_eof(channel)) { if (libssh2_channel_eof(channel)) {
printf("The server at %s:%d disconnected!\n", fprintf(stderr, "The server at %s:%d disconnected!\n",
remote_desthost, remote_destport); remote_desthost, remote_destport);
goto shutdown; goto shutdown;
} }

View File

@ -250,7 +250,7 @@ int main(int argc, char *argv[])
gettimeofday(&end, NULL); gettimeofday(&end, NULL);
time_ms = tvdiff(end, start); time_ms = tvdiff(end, start);
printf("Got %d bytes in %ld ms = %.1f bytes/sec spin: %d\n", total, fprintf(stderr, "Got %d bytes in %ld ms = %.1f bytes/sec spin: %d\n", total,
time_ms, total/(time_ms/1000.0), spin ); time_ms, total/(time_ms/1000.0), spin );
libssh2_channel_free(channel); libssh2_channel_free(channel);

View File

@ -243,7 +243,7 @@ int main(int argc, char *argv[])
duration = (int)(time(NULL)-start); duration = (int)(time(NULL)-start);
printf("%ld bytes in %d seconds makes %.1f bytes/sec\n", fprintf(stderr, "%ld bytes in %d seconds makes %.1f bytes/sec\n",
total, duration, total/(double)duration); total, duration, total/(double)duration);
fprintf(stderr, "Sending EOF\n"); fprintf(stderr, "Sending EOF\n");

View File

@ -55,24 +55,24 @@ static void kbd_callback(const char *name, int name_len,
char buf[1024]; char buf[1024];
(void)abstract; (void)abstract;
printf("Performing keyboard-interactive authentication.\n"); fprintf(stderr, "Performing keyboard-interactive authentication.\n");
printf("Authentication name: '"); fprintf(stderr, "Authentication name: '");
fwrite(name, 1, name_len, stdout); fwrite(name, 1, name_len, stderr);
printf("'\n"); fprintf(stderr, "'\n");
printf("Authentication instruction: '"); fprintf(stderr, "Authentication instruction: '");
fwrite(instruction, 1, instruction_len, stdout); fwrite(instruction, 1, instruction_len, stderr);
printf("'\n"); fprintf(stderr, "'\n");
printf("Number of prompts: %d\n\n", num_prompts); fprintf(stderr, "Number of prompts: %d\n\n", num_prompts);
for (i = 0; i < num_prompts; i++) { for (i = 0; i < num_prompts; i++) {
printf("Prompt %d from server: '", i); fprintf(stderr, "Prompt %d from server: '", i);
fwrite(prompts[i].text, 1, prompts[i].length, stdout); fwrite(prompts[i].text, 1, prompts[i].length, stderr);
printf("'\n"); fprintf(stderr, "'\n");
printf("Please type response: "); fprintf(stderr, "Please type response: ");
fgets(buf, sizeof(buf), stdin); fgets(buf, sizeof(buf), stdin);
n = strlen(buf); n = strlen(buf);
while (n > 0 && strchr("\r\n", buf[n - 1])) while (n > 0 && strchr("\r\n", buf[n - 1]))
@ -82,12 +82,13 @@ static void kbd_callback(const char *name, int name_len,
responses[i].text = strdup(buf); responses[i].text = strdup(buf);
responses[i].length = n; responses[i].length = n;
printf("Response %d from user is '", i); fprintf(stderr, "Response %d from user is '", i);
fwrite(responses[i].text, 1, responses[i].length, stdout); fwrite(responses[i].text, 1, responses[i].length, stderr);
printf("'\n\n"); fprintf(stderr, "'\n\n");
} }
printf("Done. Sending keyboard-interactive responses to server now.\n"); fprintf(stderr,
"Done. Sending keyboard-interactive responses to server now.\n");
} }
@ -127,7 +128,7 @@ int main(int argc, char *argv[])
rc = libssh2_init (0); rc = libssh2_init (0);
if (rc != 0) { if (rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
@ -178,7 +179,7 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */ /* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username)); userauthlist = libssh2_userauth_list(session, username, strlen(username));
printf("Authentication methods: %s\n", userauthlist); fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password") != NULL) { if (strstr(userauthlist, "password") != NULL) {
auth_pw |= 1; auth_pw |= 1;
} }
@ -211,21 +212,23 @@ int main(int argc, char *argv[])
} else if (auth_pw & 2) { } else if (auth_pw & 2) {
/* Or via keyboard-interactive */ /* Or via keyboard-interactive */
if (libssh2_userauth_keyboard_interactive(session, username, &kbd_callback) ) { if (libssh2_userauth_keyboard_interactive(session, username, &kbd_callback) ) {
printf("\tAuthentication by keyboard-interactive failed!\n"); fprintf(stderr,
"\tAuthentication by keyboard-interactive failed!\n");
goto shutdown; goto shutdown;
} else { } else {
printf("\tAuthentication by keyboard-interactive succeeded.\n"); fprintf(stderr,
"\tAuthentication by keyboard-interactive succeeded.\n");
} }
} else if (auth_pw & 4) { } else if (auth_pw & 4) {
/* Or by public key */ /* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, keyfile2, password)) { if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, keyfile2, password)) {
printf("\tAuthentication by public key failed!\n"); fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} else { } else {
printf("\tAuthentication by public key succeeded.\n"); fprintf(stderr, "\tAuthentication by public key succeeded.\n");
} }
} else { } else {
printf("No supported authentication methods found!\n"); fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown; goto shutdown;
} }

View File

@ -141,11 +141,11 @@ int main(int argc, char *argv[])
* user, that's your call * user, that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
printf("Fingerprint: "); fprintf(stderr, "Fingerprint: ");
for(i = 0; i < 20; i++) { for(i = 0; i < 20; i++) {
printf("%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); fprintf(stderr, "\n");
if(argc > 1) { if(argc > 1) {
username = argv[1]; username = argv[1];
@ -162,7 +162,7 @@ int main(int argc, char *argv[])
tempstorage = fopen(STORAGE, "wb"); tempstorage = fopen(STORAGE, "wb");
if(!tempstorage) { if(!tempstorage) {
printf("Can't open temp storage file %s\n", STORAGE); fprintf(stderr, "Can't open temp storage file %s\n", STORAGE);
goto shutdown; goto shutdown;
} }
@ -171,7 +171,7 @@ int main(int argc, char *argv[])
while ((rc = libssh2_userauth_password(session, username, password)) while ((rc = libssh2_userauth_password(session, username, password))
== LIBSSH2_ERROR_EAGAIN); == LIBSSH2_ERROR_EAGAIN);
if (rc) { if (rc) {
printf("Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { } else {
@ -185,7 +185,7 @@ int main(int argc, char *argv[])
password)) == password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if (rc) {
printf("\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
@ -341,7 +341,7 @@ int main(int argc, char *argv[])
#endif #endif
if (tempstorage) if (tempstorage)
fclose(tempstorage); fclose(tempstorage);
printf("all done\n"); fprintf(stderr, "all done\n");
libssh2_exit(); libssh2_exit();

View File

@ -86,7 +86,7 @@ int main(int argc, char *argv[])
local = fopen(loclfile, "rb"); local = fopen(loclfile, "rb");
if (!local) { if (!local) {
printf("Can't local file %s\n", loclfile); fprintf(stderr, "Can't open local file %s\n", loclfile);
return -1; return -1;
} }
@ -129,16 +129,16 @@ int main(int argc, char *argv[])
* user, that's your call * user, that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
printf("Fingerprint: "); fprintf(stderr, "Fingerprint: ");
for(i = 0; i < 20; i++) { for(i = 0; i < 20; i++) {
printf("%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); fprintf(stderr, "\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if (libssh2_userauth_password(session, username, password)) {
printf("Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { } else {
@ -147,7 +147,7 @@ int main(int argc, char *argv[])
"/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa", "/home/username/.ssh/id_rsa",
password)) { password)) {
printf("\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
@ -174,12 +174,12 @@ int main(int argc, char *argv[])
} }
if(libssh2_sftp_fstat_ex(sftp_handle, &attrs, 0) < 0) { if(libssh2_sftp_fstat_ex(sftp_handle, &attrs, 0) < 0) {
printf("libssh2_sftp_fstat_ex failed\n"); fprintf(stderr, "libssh2_sftp_fstat_ex failed\n");
goto shutdown; goto shutdown;
} }
else else
libssh2_sftp_seek64(sftp_handle, attrs.filesize); libssh2_sftp_seek64(sftp_handle, attrs.filesize);
printf("Did a seek to position %ld\n", (long) attrs.filesize); fprintf(stderr, "Did a seek to position %ld\n", (long) attrs.filesize);
fprintf(stderr, "libssh2_sftp_open() a handle for APPEND\n"); fprintf(stderr, "libssh2_sftp_open() a handle for APPEND\n");
@ -222,7 +222,7 @@ shutdown:
#endif #endif
if (local) if (local)
fclose(local); fclose(local);
printf("all done\n"); fprintf(stderr, "all done\n");
libssh2_exit(); libssh2_exit();

View File

@ -110,16 +110,16 @@ int main(int argc, char *argv[])
* user, that's your call * user, that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
printf("Fingerprint: "); fprintf(stderr, "Fingerprint: ");
for(i = 0; i < 20; i++) { for(i = 0; i < 20; i++) {
printf("%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); fprintf(stderr, "\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if (libssh2_userauth_password(session, username, password)) {
printf("Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { } else {
@ -128,7 +128,7 @@ int main(int argc, char *argv[])
"/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa", "/home/username/.ssh/id_rsa",
password)) { password)) {
printf("\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
@ -164,7 +164,7 @@ int main(int argc, char *argv[])
#else #else
close(sock); close(sock);
#endif #endif
printf("all done\n"); fprintf(stderr, "all done\n");
libssh2_exit(); libssh2_exit();

View File

@ -110,16 +110,16 @@ int main(int argc, char *argv[])
* user, that's your call * user, that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
printf("Fingerprint: "); fprintf(stderr, "Fingerprint: ");
for(i = 0; i < 20; i++) { for(i = 0; i < 20; i++) {
printf("%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); fprintf(stderr, "\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if (libssh2_userauth_password(session, username, password)) {
printf("Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { } else {
@ -128,7 +128,7 @@ int main(int argc, char *argv[])
"/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa", "/home/username/.ssh/id_rsa",
password)) { password)) {
printf("\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
@ -164,7 +164,7 @@ int main(int argc, char *argv[])
#else #else
close(sock); close(sock);
#endif #endif
printf("all done\n"); fprintf(stderr, "all done\n");
libssh2_exit(); libssh2_exit();

View File

@ -251,7 +251,7 @@ int main(int argc, char *argv[])
gettimeofday(&end, NULL); gettimeofday(&end, NULL);
time_ms = tvdiff(end, start); time_ms = tvdiff(end, start);
printf("Got %d bytes in %ld ms = %.1f bytes/sec spin: %d\n", total, fprintf(stderr, "Got %d bytes in %ld ms = %.1f bytes/sec spin: %d\n", total,
time_ms, total/(time_ms/1000.0), spin ); time_ms, total/(time_ms/1000.0), spin );
libssh2_sftp_close(sftp_handle); libssh2_sftp_close(sftp_handle);
@ -259,7 +259,7 @@ int main(int argc, char *argv[])
shutdown: shutdown:
printf("libssh2_session_disconnect\n"); fprintf(stderr, "libssh2_session_disconnect\n");
while (libssh2_session_disconnect(session, while (libssh2_session_disconnect(session,
"Normal Shutdown, Thank you") == "Normal Shutdown, Thank you") ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);

View File

@ -85,7 +85,7 @@ int main(int argc, char *argv[])
local = fopen(loclfile, "rb"); local = fopen(loclfile, "rb");
if (!local) { if (!local) {
printf("Can't local file %s\n", loclfile); fprintf(stderr, "Can't open local file %s\n", loclfile);
return -1; return -1;
} }
@ -128,16 +128,16 @@ int main(int argc, char *argv[])
* user, that's your call * user, that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
printf("Fingerprint: "); fprintf(stderr, "Fingerprint: ");
for(i = 0; i < 20; i++) { for(i = 0; i < 20; i++) {
printf("%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); fprintf(stderr, "\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if (libssh2_userauth_password(session, username, password)) {
printf("Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { } else {
@ -146,7 +146,7 @@ int main(int argc, char *argv[])
"/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa", "/home/username/.ssh/id_rsa",
password)) { password)) {
printf("\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
@ -206,7 +206,7 @@ shutdown:
#endif #endif
if (local) if (local)
fclose(local); fclose(local);
printf("all done\n"); fprintf(stderr, "all done\n");
libssh2_exit(); libssh2_exit();

View File

@ -125,7 +125,7 @@ int main(int argc, char *argv[])
local = fopen(loclfile, "rb"); local = fopen(loclfile, "rb");
if (!local) { if (!local) {
printf("Can't local file %s\n", loclfile); fprintf(stderr, "Can't open local file %s\n", loclfile);
return -1; return -1;
} }
@ -169,18 +169,18 @@ int main(int argc, char *argv[])
* that's your call * that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
printf("Fingerprint: "); fprintf(stderr, "Fingerprint: ");
for(i = 0; i < 20; i++) { for(i = 0; i < 20; i++) {
printf("%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); fprintf(stderr, "\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) == while ((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if (rc) {
printf("Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { } else {
@ -191,7 +191,7 @@ int main(int argc, char *argv[])
password)) == password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if (rc) {
printf("\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
@ -253,7 +253,7 @@ int main(int argc, char *argv[])
duration = (int)(time(NULL)-start); duration = (int)(time(NULL)-start);
printf("%ld bytes in %d seconds makes %.1f bytes/sec\n", fprintf(stderr, "%ld bytes in %d seconds makes %.1f bytes/sec\n",
total, duration, total/(double)duration); total, duration, total/(double)duration);
@ -272,7 +272,7 @@ shutdown:
#else #else
close(sock); close(sock);
#endif #endif
printf("all done\n"); fprintf(stderr, "all done\n");
libssh2_exit(); libssh2_exit();

View File

@ -125,7 +125,7 @@ int main(int argc, char *argv[])
local = fopen(loclfile, "rb"); local = fopen(loclfile, "rb");
if (!local) { if (!local) {
printf("Can't local file %s\n", loclfile); fprintf(stderr, "Can't open local file %s\n", loclfile);
return -1; return -1;
} }
@ -169,18 +169,18 @@ int main(int argc, char *argv[])
* that's your call * that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
printf("Fingerprint: "); fprintf(stderr, "Fingerprint: ");
for(i = 0; i < 20; i++) { for(i = 0; i < 20; i++) {
printf("%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); fprintf(stderr, "\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) == while ((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if (rc) {
printf("Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { } else {
@ -191,7 +191,7 @@ int main(int argc, char *argv[])
password)) == password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if (rc) {
printf("\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
@ -262,7 +262,7 @@ int main(int argc, char *argv[])
duration = (int)(time(NULL)-start); duration = (int)(time(NULL)-start);
printf("%ld bytes in %d seconds makes %.1f bytes/sec\n", fprintf(stderr, "%ld bytes in %d seconds makes %.1f bytes/sec\n",
total, duration, total/(double)duration); total, duration, total/(double)duration);
@ -281,7 +281,7 @@ shutdown:
#else #else
close(sock); close(sock);
#endif #endif
printf("all done\n"); fprintf(stderr, "all done\n");
libssh2_exit(); libssh2_exit();

View File

@ -130,16 +130,16 @@ int main(int argc, char *argv[])
* user, that's your call * user, that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
printf("Fingerprint: "); fprintf(stderr, "Fingerprint: ");
for(i = 0; i < 20; i++) { for(i = 0; i < 20; i++) {
printf("%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); fprintf(stderr, "\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if (libssh2_userauth_password(session, username, password)) {
printf("Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { } else {
@ -148,7 +148,7 @@ int main(int argc, char *argv[])
"/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa", "/home/username/.ssh/id_rsa",
password)) { password)) {
printf("\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
@ -229,7 +229,7 @@ int main(int argc, char *argv[])
#else #else
close(sock); close(sock);
#endif #endif
printf("all done\n"); fprintf(stderr, "all done\n");
libssh2_exit(); libssh2_exit();

View File

@ -134,11 +134,11 @@ int main(int argc, char *argv[])
* user, that's your call * user, that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
printf("Fingerprint: "); fprintf(stderr, "Fingerprint: ");
for(i = 0; i < 20; i++) { for(i = 0; i < 20; i++) {
printf("%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); fprintf(stderr, "\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
@ -239,7 +239,7 @@ int main(int argc, char *argv[])
#else #else
close(sock); close(sock);
#endif #endif
printf("all done\n"); fprintf(stderr, "all done\n");
libssh2_exit(); libssh2_exit();

View File

@ -126,15 +126,15 @@ int main(int argc, char *argv[])
* call * call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
printf("Fingerprint: "); fprintf(stderr, "Fingerprint: ");
for(i = 0; i < 20; i++) { for(i = 0; i < 20; i++) {
printf("%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); fprintf(stderr, "\n");
/* check what authentication methods are available */ /* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username)); userauthlist = libssh2_userauth_list(session, username, strlen(username));
printf("Authentication methods: %s\n", userauthlist); fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password") != NULL) { if (strstr(userauthlist, "password") != NULL) {
auth_pw |= 1; auth_pw |= 1;
} }
@ -161,31 +161,33 @@ int main(int argc, char *argv[])
if (auth_pw & 1) { if (auth_pw & 1) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if (libssh2_userauth_password(session, username, password)) {
printf("\tAuthentication by password failed!\n"); fprintf(stderr, "\tAuthentication by password failed!\n");
goto shutdown; goto shutdown;
} else { } else {
printf("\tAuthentication by password succeeded.\n"); fprintf(stderr, "\tAuthentication by password succeeded.\n");
} }
} else if (auth_pw & 2) { } else if (auth_pw & 2) {
/* Or via keyboard-interactive */ /* Or via keyboard-interactive */
if (libssh2_userauth_keyboard_interactive(session, username, if (libssh2_userauth_keyboard_interactive(session, username,
&kbd_callback) ) { &kbd_callback) ) {
printf("\tAuthentication by keyboard-interactive failed!\n"); fprintf(stderr,
"\tAuthentication by keyboard-interactive failed!\n");
goto shutdown; goto shutdown;
} else { } else {
printf("\tAuthentication by keyboard-interactive succeeded.\n"); fprintf(stderr,
"\tAuthentication by keyboard-interactive succeeded.\n");
} }
} else if (auth_pw & 4) { } else if (auth_pw & 4) {
/* Or by public key */ /* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, if (libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) { keyfile2, password)) {
printf("\tAuthentication by public key failed!\n"); fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} else { } else {
printf("\tAuthentication by public key succeeded.\n"); fprintf(stderr, "\tAuthentication by public key succeeded.\n");
} }
} else { } else {
printf("No supported authentication methods found!\n"); fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown; goto shutdown;
} }
@ -250,7 +252,7 @@ int main(int argc, char *argv[])
#else #else
close(sock); close(sock);
#endif #endif
printf("all done!\n"); fprintf(stderr, "all done!\n");
libssh2_exit(); libssh2_exit();

View File

@ -105,15 +105,15 @@ int main(int argc, char *argv[])
* call * call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
printf("Fingerprint: "); fprintf(stderr, "Fingerprint: ");
for(i = 0; i < 20; i++) { for(i = 0; i < 20; i++) {
printf("%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); fprintf(stderr, "\n");
/* check what authentication methods are available */ /* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username)); userauthlist = libssh2_userauth_list(session, username, strlen(username));
printf("Authentication methods: %s\n", userauthlist); fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "publickey") == NULL) { if (strstr(userauthlist, "publickey") == NULL) {
fprintf(stderr, "\"publickey\" authentication is not supported\n"); fprintf(stderr, "\"publickey\" authentication is not supported\n");
goto shutdown; goto shutdown;
@ -147,11 +147,11 @@ int main(int argc, char *argv[])
goto shutdown; goto shutdown;
} }
if (libssh2_agent_userauth(agent, username, identity)) { if (libssh2_agent_userauth(agent, username, identity)) {
printf("\tAuthentication with username %s and " fprintf(stderr, "\tAuthentication with username %s and "
"public key %s failed!\n", "public key %s failed!\n",
username, identity->comment); username, identity->comment);
} else { } else {
printf("\tAuthentication with username %s and " fprintf(stderr, "\tAuthentication with username %s and "
"public key %s succeeded!\n", "public key %s succeeded!\n",
username, identity->comment); username, identity->comment);
break; break;
@ -234,7 +234,7 @@ int main(int argc, char *argv[])
#endif #endif
} }
printf("all done!\n"); fprintf(stderr, "all done!\n");
libssh2_exit(); libssh2_exit();

View File

@ -327,7 +327,7 @@ int main(int argc, char *argv[])
} }
if (exitsignal) if (exitsignal)
printf("\nGot signal: %s\n", exitsignal); fprintf(stderr, "\nGot signal: %s\n", exitsignal);
libssh2_channel_free(channel); libssh2_channel_free(channel);
channel = NULL; channel = NULL;

View File

@ -292,9 +292,9 @@ int main(int argc, char *argv[])
} }
if (exitsignal) if (exitsignal)
printf("\nGot signal: %s\n", exitsignal); fprintf(stderr, "\nGot signal: %s\n", exitsignal);
else else
printf("\nEXIT: %d bytecount: %d\n", exitcode, bytecount); fprintf(stderr, "\nEXIT: %d bytecount: %d\n", exitcode, bytecount);
libssh2_channel_free(channel); libssh2_channel_free(channel);
channel = NULL; channel = NULL;

View File

@ -173,7 +173,7 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */ /* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username)); userauthlist = libssh2_userauth_list(session, username, strlen(username));
printf("Authentication methods: %s\n", userauthlist); fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password")) if (strstr(userauthlist, "password"))
auth |= AUTH_PASSWORD; auth |= AUTH_PASSWORD;
if (strstr(userauthlist, "publickey")) if (strstr(userauthlist, "publickey"))
@ -195,12 +195,12 @@ int main(int argc, char *argv[])
} else if (auth & AUTH_PUBLICKEY) { } else if (auth & AUTH_PUBLICKEY) {
if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, if (libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) { keyfile2, password)) {
printf("Authentication by public key failed!\n"); fprintf(stderr, "Authentication by public key failed!\n");
goto shutdown; goto shutdown;
} }
printf("Authentication by public key succeeded.\n"); fprintf(stderr, "Authentication by public key succeeded.\n");
} else { } else {
printf("No supported authentication methods found!\n"); fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown; goto shutdown;
} }
@ -223,7 +223,7 @@ int main(int argc, char *argv[])
/* NETCONF: http://tools.ietf.org/html/draft-ietf-netconf-ssh-06 */ /* NETCONF: http://tools.ietf.org/html/draft-ietf-netconf-ssh-06 */
printf("Sending NETCONF client <hello>\n"); fprintf(stderr, "Sending NETCONF client <hello>\n");
snprintf(buf, sizeof(buf), snprintf(buf, sizeof(buf),
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<hello>" "<hello>"
@ -235,14 +235,14 @@ int main(int argc, char *argv[])
if (-1 == netconf_write(channel, buf, len)) if (-1 == netconf_write(channel, buf, len))
goto shutdown; goto shutdown;
printf("Reading NETCONF server <hello>\n"); fprintf(stderr, "Reading NETCONF server <hello>\n");
len = netconf_read_until(channel, "</hello>", buf, sizeof(buf)); len = netconf_read_until(channel, "</hello>", buf, sizeof(buf));
if (-1 == len) if (-1 == len)
goto shutdown; goto shutdown;
printf("Got %d bytes:\n----------------------\n%s", (int)len, buf); fprintf(stderr, "Got %d bytes:\n----------------------\n%s", (int)len, buf);
printf("Sending NETCONF <rpc>\n"); fprintf(stderr, "Sending NETCONF <rpc>\n");
snprintf(buf, sizeof(buf), snprintf(buf, sizeof(buf),
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
@ -252,12 +252,12 @@ int main(int argc, char *argv[])
if (-1 == netconf_write(channel, buf, len)) if (-1 == netconf_write(channel, buf, len))
goto shutdown; goto shutdown;
printf("Reading NETCONF <rpc-reply>\n"); fprintf(stderr, "Reading NETCONF <rpc-reply>\n");
len = netconf_read_until(channel, "</rpc-reply>", buf, sizeof(buf)); len = netconf_read_until(channel, "</rpc-reply>", buf, sizeof(buf));
if (-1 == len) if (-1 == len)
goto shutdown; goto shutdown;
printf("Got %d bytes:\n----------------------\n%s", (int)len, buf); fprintf(stderr, "Got %d bytes:\n----------------------\n%s", (int)len, buf);
shutdown: shutdown:
if (channel) if (channel)

View File

@ -137,7 +137,7 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */ /* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username)); userauthlist = libssh2_userauth_list(session, username, strlen(username));
printf("Authentication methods: %s\n", userauthlist); fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password")) if (strstr(userauthlist, "password"))
auth |= AUTH_PASSWORD; auth |= AUTH_PASSWORD;
if (strstr(userauthlist, "publickey")) if (strstr(userauthlist, "publickey"))
@ -159,17 +159,17 @@ int main(int argc, char *argv[])
} else if (auth & AUTH_PUBLICKEY) { } else if (auth & AUTH_PUBLICKEY) {
if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, if (libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) { keyfile2, password)) {
printf("\tAuthentication by public key failed!\n"); fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} }
printf("\tAuthentication by public key succeeded.\n"); fprintf(stderr, "\tAuthentication by public key succeeded.\n");
} else { } else {
printf("No supported authentication methods found!\n"); fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown; goto shutdown;
} }
printf("Asking server to listen on remote %s:%d\n", remote_listenhost, fprintf(stderr, "Asking server to listen on remote %s:%d\n",
remote_wantport); remote_listenhost, remote_wantport);
listener = libssh2_channel_forward_listen_ex(session, remote_listenhost, listener = libssh2_channel_forward_listen_ex(session, remote_listenhost,
remote_wantport, &remote_listenport, 1); remote_wantport, &remote_listenport, 1);
@ -180,10 +180,10 @@ int main(int argc, char *argv[])
goto shutdown; goto shutdown;
} }
printf("Server is listening on %s:%d\n", remote_listenhost, fprintf(stderr, "Server is listening on %s:%d\n", remote_listenhost,
remote_listenport); remote_listenport);
printf("Waiting for remote connection\n"); fprintf(stderr, "Waiting for remote connection\n");
channel = libssh2_channel_forward_accept(listener); channel = libssh2_channel_forward_accept(listener);
if (!channel) { if (!channel) {
fprintf(stderr, "Could not accept connection!\n" fprintf(stderr, "Could not accept connection!\n"
@ -192,7 +192,8 @@ int main(int argc, char *argv[])
goto shutdown; goto shutdown;
} }
printf("Accepted remote connection. Connecting to local server %s:%d\n", fprintf(stderr,
"Accepted remote connection. Connecting to local server %s:%d\n",
local_destip, local_destport); local_destip, local_destport);
forwardsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); forwardsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
@ -206,7 +207,7 @@ int main(int argc, char *argv[])
goto shutdown; goto shutdown;
} }
printf("Forwarding connection from remote %s:%d to local %s:%d\n", fprintf(stderr, "Forwarding connection from remote %s:%d to local %s:%d\n",
remote_listenhost, remote_listenport, local_destip, local_destport); remote_listenhost, remote_listenport, local_destip, local_destport);
/* Must use non-blocking IO hereafter due to the current libssh2 API */ /* Must use non-blocking IO hereafter due to the current libssh2 API */
@ -228,7 +229,7 @@ int main(int argc, char *argv[])
perror("read"); perror("read");
goto shutdown; goto shutdown;
} else if (0 == len) { } else if (0 == len) {
printf("The local server at %s:%d disconnected!\n", fprintf(stderr, "The local server at %s:%d disconnected!\n",
local_destip, local_destport); local_destip, local_destport);
goto shutdown; goto shutdown;
} }
@ -260,7 +261,7 @@ int main(int argc, char *argv[])
wr += i; wr += i;
} }
if (libssh2_channel_eof(channel)) { if (libssh2_channel_eof(channel)) {
printf("The remote client at %s:%d disconnected!\n", fprintf(stderr, "The remote client at %s:%d disconnected!\n",
remote_listenhost, remote_listenport); remote_listenhost, remote_listenport);
goto shutdown; goto shutdown;
} }