Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * $ gcc -g -O0 -D HAVE_NETINET_IN_H -D HAVE_SYS_SOCKET_H -D HAVE_UNISTD_H -D HAVE_ARPA_INET_H -lssh2 -o ssh2_exec ssh2_exec.c
- * $ ./ssh2_exec 127.0.0.1 user password command keyfile.pub keyfile
- *
- */
- #include <libssh2.h>
- #ifdef HAVE_WINSOCK2_H
- #include <winsock2.h>
- #endif
- #ifdef HAVE_SYS_SOCKET_H
- #include <sys/socket.h>
- #endif
- #ifdef HAVE_NETINET_IN_H
- #include <netinet/in.h>
- #endif
- #ifdef HAVE_SYS_SELECT_H
- #include <sys/select.h>
- #endif
- #ifdef HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- #ifdef HAVE_ARPA_INET_H
- #include <arpa/inet.h>
- #endif
- #include <sys/time.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdio.h>
- #include <ctype.h>
- static int
- waitsocket (
- int socket_fd,
- LIBSSH2_SESSION * session)
- {
- struct timeval timeout;
- int rc;
- fd_set fd;
- fd_set *writefd = NULL;
- fd_set *readfd = NULL;
- int dir;
- timeout.tv_sec = 3;
- timeout.tv_usec = 0;
- FD_ZERO (&fd);
- FD_SET (socket_fd, &fd);
- /* now make sure we wait in the correct direction */
- dir = libssh2_session_block_directions (session);
- if (dir & LIBSSH2_SESSION_BLOCK_INBOUND)
- readfd = &fd;
- if (dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
- writefd = &fd;
- rc = select (socket_fd + 1, readfd, writefd, NULL, &timeout);
- return rc;
- }
- int
- main (
- int argc,
- char *argv[])
- {
- const char *hostaddr_str = "127.0.0.1";
- const char *commandline = "uptime";
- const char *username = "user";
- const char *password = "password";
- const char *keyfile1 = "";
- const char *keyfile2 = "";
- unsigned long hostaddr;
- int sock;
- struct sockaddr_in sin;
- const char *fingerprint;
- LIBSSH2_SESSION *session;
- LIBSSH2_CHANNEL *channel;
- int rc;
- int exitcode;
- int bytecount = 0;
- size_t len;
- int type;
- int i;
- char *userauthlist;
- char auth_pw = 0;
- #ifdef WIN32
- WSADATA wsadata;
- WSAStartup (MAKEWORD (2, 0), &wsadata);
- #endif
- if (argc > 1) {
- hostaddr_str = argv[1];
- }
- if (argc > 2) {
- username = argv[2];
- }
- if (argc > 3) {
- password = argv[3];
- }
- if (argc > 4) {
- commandline = argv[4];
- }
- if (argc > 5) {
- keyfile1 = argv[5];
- }
- if (argc > 6) {
- keyfile2 = argv[6];
- }
- hostaddr = inet_addr (hostaddr_str);
- /* establish socket connection */
- sock = socket (AF_INET, SOCK_STREAM, 0);
- sin.sin_family = AF_INET;
- sin.sin_port = htons (22);
- sin.sin_addr.s_addr = hostaddr;
- if (connect (sock, (struct sockaddr *)(&sin),
- sizeof (struct sockaddr_in)) != 0) {
- fprintf (stderr, "failed to connect!\n");
- return -1;
- }
- /* create session handle */
- session = libssh2_session_init ();
- if (!session)
- return -1;
- /* do everything non-blocking */
- libssh2_session_set_blocking (session, 0);
- /* statup session */
- while ((rc = libssh2_session_startup (session, sock)) ==
- LIBSSH2_ERROR_EAGAIN);
- if (rc) {
- fprintf (stderr, "Failure establishing SSH session: %d\n", rc);
- return -1;
- }
- if ((strlen (keyfile1) != 0) && (strlen (keyfile2) != 0)) {
- while ((rc = libssh2_userauth_publickey_fromfile (session, username,
- keyfile1,
- keyfile2,
- password)) ==
- LIBSSH2_ERROR_EAGAIN);
- if (rc) {
- fprintf (stderr, "Authentication by public key (%s with %s and %s) failed\n", username, keyfile1, keyfile2);
- goto shutdown;
- } else {
- fprintf (stderr, "Authentication by public key succeeded\n");
- }
- } else if (strlen (password) != 0) {
- /* authenticate by password */
- while ((rc = libssh2_userauth_password (session, username, password)) ==
- LIBSSH2_ERROR_EAGAIN) ;
- if (rc) {
- fprintf (stderr, "Authentication by password failed.\n");
- goto shutdown;
- } else {
- fprintf (stderr, "Authentication by password succeeded\n");
- }
- }
- /* create channel and execute command */
- 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 in libssh2_channel_open_session\n");
- exit (1);
- }
- while ((rc = libssh2_channel_exec (channel, commandline)) ==
- LIBSSH2_ERROR_EAGAIN) {
- waitsocket (sock, session);
- }
- if (rc != 0) {
- fprintf (stderr, "Error in libssh2_channel_exec\n");
- exit (1);
- }
- for (;;) {
- /* loop until we block */
- int rc;
- do {
- char buffer[0x4000];
- rc = libssh2_channel_read (channel, buffer, sizeof (buffer));
- if (rc > 0) {
- int i;
- bytecount += rc;
- fprintf (stderr, "We read:\n");
- for (i = 0; i < rc; ++i)
- fputc (buffer[i], stderr);
- fprintf (stderr, "\n");
- } else {
- fprintf (stderr, "libssh2_channel_read returned %d\n", rc);
- }
- }
- while (rc > 0);
- /* wait on LIBSSH2_ERROR_EAGAIN, else we got 0 bytes or an error so break */
- if (rc == LIBSSH2_ERROR_EAGAIN) {
- waitsocket (sock, session);
- } else
- break;
- }
- exitcode = 127;
- while ((rc = libssh2_channel_close (channel)) == LIBSSH2_ERROR_EAGAIN) {
- waitsocket (sock, session);
- }
- if (rc == 0) {
- exitcode = libssh2_channel_get_exit_status (channel);
- }
- printf ("\nEXIT: %d bytecount: %d\n", exitcode, bytecount);
- libssh2_channel_free (channel);
- channel = NULL;
- shutdown:
- libssh2_session_disconnect (session,
- "Normal Shutdown, Thank you for playing");
- libssh2_session_free (session);
- #ifdef WIN32
- closesocket (sock);
- #else
- close (sock);
- #endif
- fprintf (stderr, "all done\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement