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

Libssh (C++ wrapper): connecting to rbash


Hello,
I'm trying to use libssh 5.0 to connect to an account on a machine.

When running "ps -ef  | grep ps " I get:
rbash -c ps -ef | grep ps
which corresponds to the command I'm running.

So I'm on a rbash shell, with no other option given to rbash (especially,
no --noprofile).

When checking the ~/.profile file (which should be loaded - per man rbash),
it contains
PATH=/home/my_usr/bin

However, when executing "echo $PATH" I get this:
PATH=/usr/bin:/usr/local/bin:[etc]

Does anyone know why the ~/.profile file wasn't loaded?

Attached file: the wrapper around the C++ libssh.hpp file that I'm using.
The command to execute the remote command is line 58.


Thanks,
Pascal
#include "ssh_command.h"
#include "traces.h"

#define DISPLAY_DEBUG_CLASS "SshCommand"

SshCommand::SshCommand(Conf* aConf)
{
    m_conf = aConf;
    mySession = NULL;
    myChannel = NULL;
}

SshCommand::~SshCommand()
{
    //If we have ssh remains, delete them.
    local_clean();
}

/*
 * Performs the following operations:
 * 1 - open a new ssh session
 * 2 - set options for the session
 * 3 - check that we know the server
 * 4 - check auth of user
 * 5 - connect as user to the remote machine
 * 6 - create channel in that session
 * 7 - open session in the channel
 * 8 - send the command
 * 9 - read the response
 * 10 - close the channel
 * 11 - disconnect the session
 */
 /* Parameters:
  *   iCommand: char* containing the command to run on the other machine.
  *   oBuffer: buffer (non-null pointer) which will be filled with the response.
  *   bufferSize: size of the buffer.
  * Output:
  *   number of bytes read (that were copied into the buffer). -1 means an error occurred.
  */
int SshCommand::runCommand(const char* iCommand, char* oBuffer, int bufferSize)
{
    // buffer in which we'll store the output.
    int bytesRead = 0;

    DISPLAY("Running... " << iCommand);
    try
    {
        initSession();

        initChannel();
        
        for(int i = 0; i<bufferSize; i++)
        {
          oBuffer[i] = '\0';
        }
            
        // send the request over the channel
        myChannel->requestExec(iCommand);

        // read the output
        bytesRead = myChannel->read((void*)oBuffer, bufferSize);
        DISPLAY("Response: " << bytesRead << " " << oBuffer );
        
  /*      myChannel->sendEof();
        DISPLAY("EoF" );

        //check result
        myChannel->requestExec("echo $?");
        
        DISPLAY("Sending echo $?");
        execRead = myChannel->read((void*) execResult, bufferSize);
        DISPLAY("execRead: " << execRead << " " << bufferSize );
               
        if (atoi(execResult) != 0)
        {
          bytesRead = -1;
        }
  */
        
    }
    catch (ssh::SshException e){
     DISPLAY("Ssh Excpetion!");
    }
    catch (...){
     DISPLAY("Exception!");
    }
    
    local_clean();
    
    return bytesRead;
}

void SshCommand::initSession()
{
    int res = 0;
    mySession = new ssh::Session();
    int timeout = 3600;

    if (mySession == NULL)
    {
        //throw();
    }

//    mySession->optionsParseConfig(SSH_CONFIG_FILE);
    mySession->setOption(SSH_OPTIONS_HOST, m_conf->m_sshData.host.c_str());
    mySession->setOption(SSH_OPTIONS_USER, m_conf->m_sshData.username.c_str());
    mySession->setOption(SSH_OPTIONS_TIMEOUT, &timeout);
    
    mySession->connect();
        
    res = mySession->isServerKnown();
    if (res != SSH_SERVER_KNOWN_OK )
    {
        //throw();
    }
    res = mySession->userauthPassword(m_conf->m_sshData.password.c_str());
    if (res != SSH_AUTH_SUCCESS)
    {
        //throw();
    }
    
}

void SshCommand::initChannel()
{
    myChannel = new ssh::Channel(*mySession);

    if (myChannel == NULL)
    {
        // throw();
    }

    myChannel->openSession();
}

void SshCommand::local_clean()
{
    if(myChannel!=NULL)
    {
        if (myChannel->isOpen())
        {
            myChannel->sendEof();
            myChannel->close();
        }
        free(myChannel);
        myChannel = NULL;
    }


    if (mySession != NULL)
    {
        mySession->disconnect();
        free(mySession);
        mySession = NULL;
    }
}

Follow-Ups:
Re: Libssh (C++ wrapper): connecting to rbashPascal Bertrand <floppyzedolfin@xxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org