[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++ Wrapper, getting started


This worked.  Thanks!

#include <iostream>
using namespace std;

#include <libssh/libsshpp.hpp>

int main()
{
    int port = 22;

    //Can use SSH_LOG_PROTOCOL here for verbose output
    int verbosity = SSH_LOG_NOLOG;

    ssh::Session session;

    try
    {
        session.setOption( SSH_OPTIONS_LOG_VERBOSITY, &verbosity );
        session.setOption( SSH_OPTIONS_PORT, &port );
        session.setOption( SSH_OPTIONS_USER, "user" );
        session.setOption( SSH_OPTIONS_HOST, "192.168.52.101" );

        session.connect();

        if( session.isServerKnown() != SSH_SERVER_KNOWN_OK )
        {
            if( session.writeKnownhost() != SSH_OK )
            {
                cout << "writeKnownHost failed" << endl;
            }
            else
            {
                session.connect();
            }
        }

        if( session.userauthPassword( "password" ) != SSH_AUTH_SUCCESS )
        {
            cout << "failed auth" << endl;
        }

        ssh::Channel channel( session );
        channel.openSession();
        //Source environment if necessary, run executable
channel.requestExec( "source /path/to/set_env.sh; /path/to/executable/..." );
        channel.close();
        channel.sendEof();

        //Unfortunate brute force step, the exec call needed some time
        sleep( 1 );

    }
    catch( ssh::SshException e )
    {
        std::cout << "Error during connection : ";
        std::cout << e.getError() << std::endl;
    }

    return 0;
}

On 05/15/2015 09:29 AM, Dave Miller @ Harbrick wrote:
Thanks Emre,

I've added verbosity and everything is returning successfully. The problem is that requestExec() is not actually running the command that I ask it to. It returns successfully, but does nothing.

On 05/14/2015 11:24 PM, Emre Soranlar wrote:

Hi Dave,

You should set port number and verbosity for the connection. And make sure the ssh is active on the remote computer. See the my connection, ( this is C code but there is no difference in c++ )

  int verbosity = SSH_LOG_PROTOCOL;

  int port = 22;

  char *password = "test";

  int rc;

  ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "192.168.56.102");

  ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);

  ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);

  ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "user");

  rc = ssh_connect(my_ssh_session);

  if ( rc == SSH_OK )

  {

      EventLogger::LogMessage(true, "Connection succeded %d", rc);

  }

  else

  {

      EventLogger::LogMessage(true, "ssh_connect failed %d", rc);

      return false;

  }

  rc = ssh_userauth_password(my_ssh_session, NULL, password);

  if ( rc == SSH_AUTH_SUCCESS )

  {

      EventLogger::LogMessage(true, "Authenticate succeded %d", rc);

  }

Try that way, this is working on my machine properly


On 15 May 2015 at 09:11, Emre Soranlar <emresoranlar@xxxxxxxxx <mailto:emresoranlar@xxxxxxxxx>> wrote:

    Hi Dave,

    You should set port number and verbosity for the connection. And
    make sure the ssh is active on the remote computer. See the my
    connection, ( this is C code but there is no difference in c++ )

            int verbosity = SSH_LOG_PROTOCOL;
            int port = 22;
            char *password = "test";
            int rc;

            ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST,
    "192.168.56.102");
            ssh_options_set(my_ssh_session,
    SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
            ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
            ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "user");

            rc = ssh_connect(my_ssh_session);
            if ( rc == SSH_OK )
            {
                EventLogger::LogMessage(true, "Connection succeded
    %d", rc);
            }
            else
            {
                EventLogger::LogMessage(true, "ssh_connect failed
    %d", rc);
                return false;
            }

            rc = ssh_userauth_password(my_ssh_session, NULL, password);
            if ( rc == SSH_AUTH_SUCCESS )
            {
                EventLogger::LogMessage(true, "Authenticate succeded
    %d", rc);
            }



    Try that way, this is working on my machine properly

    On 15 May 2015 at 02:49, Dave Miller @ Harbrick
    <dmiller@xxxxxxxxxxxx <mailto:dmiller@xxxxxxxxxxxx>> wrote:

        Greetings libssh,

        I'm having trouble getting started using the C++ wrapper. I'd
        like to start a session+channel to do commands on a remote
        machine.
        Can't get it to work.  What am I missing? Also, I've shown in
        the comments where a disconnect call causes a seg fault.

        Thank you!

        Code:

        #include <iostream>
        using namespace std;

        #include <libssh/libsshpp.hpp>

        int main()
        {
            ssh::Session session;
            try
            {
                session.setOption( SSH_OPTIONS_USER, "user" );
                session.setOption( SSH_OPTIONS_HOST, "192.168.200.101" );
                session.connect();
                session.userauthPassword( "password" );

                ssh::Channel channel( session );
                channel.openSession();

                channel.requestExec( "psync-manager stop" );
                channel.close();
                channel.sendEof();

        // This causes a seg fault in ssh_channel_free() ???
        //              session.disconnect();
            }
            catch (ssh::SshException e)
            {
                std::cout << "Error during connection : ";
                std::cout << e.getError() << std::endl;
            }

            return 0;
        }
--
        Dave Miller





-- EMRE SORANLAR
    Ankara University
    Software Engineer
    Environmental Tectonics Corp/ Turkey / ODTU




--
EMRE SORANLAR
Ankara University
Software Engineer
Environmental Tectonics Corp/ Turkey / ODTU

--

Dave Miller





References:
C++ Wrapper, getting started"Dave Miller @ Harbrick" <dmiller@xxxxxxxxxxxx>
Re: C++ Wrapper, getting startedEmre Soranlar <emresoranlar@xxxxxxxxx>
Re: C++ Wrapper, getting started"Dave Miller @ Harbrick" <dmiller@xxxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org