Subject: [libssh2] Shell initiation process?

[libssh2] Shell initiation process?

From: Mark Erikson <mark_at_isquaredsoftware.com>
Date: Thu, 18 Jan 2007 05:47:51 +0800

Hi again, folks. I've spent some time playing with libssh2, and while
I've made progress, I'm stuck at the moment. As I mentioned previously,
I'm going for a terminal emulation connection. I've finally figured out
how to get what appears to be a successful to the server (a Linux VM
running on my Windows XP box), but I can't get anything to happen after
that. I've tried writing commands, reading, polling, and nothing seems
to be going on. I'm attaching my current code, and would appreciate any
further assistance anyone can give.

My current sample program runs something like this:

// initialize Winsock
// create socket
// initialize session
// create channel
// request PTY
// request shell

// try reading and writing stuff, and nothing happens

// clean up and shut down

I get the feeling that I'm close, but that there's something obvious I'm
missing.

For the record, I'm using modified versions of Chris Nystrom's socklib
and ssh2lib to abstract out the socket handling and a couple of the
libssh2 steps. Also, apologies if this code is a bit messy - I've been
focused on just trying to make something cooperate.

Again, thanks for any help you can offer.

Mark Erikson
http://www.isquaredsoftware.com

#include "ssh2_lib.h"
#include <stdio.h>
#include "sock_lib.h"
#include <iostream>

#define SSH_PORT (22)

using namespace std;

int main(int argc, char *argv[])
{
        //int sock;
        char buf[256] = "";
        char version[] = "0.09_bin_050";
        int gPort = 22;
        SOCKET lhSocket;

        const char* server = "172.17.207.32";
        const char* username = "****";
        const char* passwd = "****";

        printf("--\n");

        WSADATA wsaData;
        if(WSAStartup(MAKEWORD(2,0),&wsaData) != 0)
        {
                cout<<"Socket Initialization Error. Program aborted\n";
                return -1;
        }

        lhSocket = init_tcp_client(server, SSH_PORT);

        if (init_session(lhSocket, username, passwd) != 0) {
                printf("Session init failed\r\n");
                //close_socket(sock);
                return (-1);
        }

        LIBSSH2_CHANNEL* localChannel;

        if (open_channel() != 0) {
                fprintf(stderr, "error: open_channel() failed.\n");
                return (-1);
        }

        localChannel = get_channel();

        if (libssh2_channel_request_pty(localChannel, "vanilla")) {
                fprintf(stderr, "Failed requesting pty\n");
                return (-1);
        }

        /* Open a SHELL on that pty */
        if (libssh2_channel_shell(localChannel)) {
                fprintf(stderr,
                        "Unable to request shell on allocated pty\n");
                return (-1);
        }

        puts_channel("testing blah blah\r\n");

        char buff[1024];
        gets_channel(buf, 1020);

        buff[1023] = '\0';

        printf("Buffer: %s\r\n", buff);

        bool dataWaiting = false;
        for(int i = 0; i < 5; i++)
        {
                dataWaiting = (libssh2_poll_channel_read(localChannel, 1) == 1);

                if(dataWaiting)
                {
                        break;
                }
                else
                {
                        Sleep(1000);
                        cout << i + 1 << " ";
                }
        }

        if(dataWaiting)
        {
                char buffer[1024];

                int bytesRead = libssh2_channel_read(localChannel, buffer, 1024);
                printf("Buffer: %d, %s", bytesRead, buffer);
        }
        else
        {
                printf("No data found!");
        }

        free_channel();
        free_session();

        close_socket(lhSocket);

        printf("\r\nall done\n--\n");
        return 0;
}

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

_______________________________________________
libssh2-devel mailing list
libssh2-devel_at_lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libssh2-devel
Received on 2007-01-17