0

我有这个功能,如果你用 ssh 连接到一个系统,你可以调用它来在那个系统上执行你给定的命令。

std::string sshconnection::exec_ssh_command(ssh_session session, char *command) {
    string receive = "";
    int rc, nbytes;
    char buffer[256];
    ssh_channel channel = ssh_channel_new(session);
    if( channel == NULL )
            return NULL;

    rc = ssh_channel_open_session(channel);
    if( rc != SSH_OK ) {
            ssh_channel_free(channel);
            return NULL;
    }

    rc = ssh_channel_request_exec(channel, command);
    if( rc != SSH_OK ) {
            ssh_channel_close(channel);
            ssh_channel_free(channel);
            cout << "Error";
            return NULL;
    }
    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    while (nbytes > 0)
    {
            if (write(1, buffer, nbytes) != (unsigned int) nbytes)
            {
                    ssh_channel_close(channel);
                    ssh_channel_free(channel);
                    return NULL;
            }
            nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    }

    if( nbytes < 0 )
            return NULL;

    ssh_channel_send_eof(channel);
    ssh_channel_close(channel);
    ssh_channel_free(channel);

    return receive;
}

这个功能很好用。我只是不明白即将进入文件描述符= 1 的那write部分buffer。我们没有填写receive任何地方,但它是返回值。如果我们像下面这样调用这个函数:

s = exec_ssh_command(my_ssh_session, "cat /proc/stat" );

s不会有任何价值,但如果我们这样做:

std::cout<<s;

这将打印s价值。当然我们不能保存s在文件中。
有人可以向我解释这是怎么回事吗?

编辑:
连接到 ssh 的功能:

int sshconnection::sshConnection()
{
    if( my_ssh_session == NULL ) {
            cout << "Error creating ssh session" << endl;
            return 1;
    }
    ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "yourip");
    ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "username");
    int rc = ssh_connect(my_ssh_session);
    if( rc != SSH_OK ) {
            cout << "Error with connecting" << endl;
            ssh_free(my_ssh_session);
            return -1;
    }

    rc = ssh_userauth_password(my_ssh_session, NULL, "yourpassword");
    if( rc != SSH_AUTH_SUCCESS) {
            cout << "Error with authorization  " << ssh_get_error(my_ssh_session) << endl;
            ssh_disconnect(my_ssh_session);
            ssh_free(my_ssh_session);
            return -1;
    }
//    ssh_disconnect(my_ssh_session);
    //    ssh_free(my_ssh_session);
}
4

1 回答 1

0

我知道这是旧的,但我有同样的问题。我想出了以下解决方案。

像这样使用std::string::appendreceive.append(buffer, nbytes)

std::string sshconnection::exec_ssh_command(ssh_session session, char *command) {
    string receive = "";
    int rc, nbytes;
    char buffer[256];
    ssh_channel channel = ssh_channel_new(session);
    if( channel == NULL )
            return NULL;

    rc = ssh_channel_open_session(channel);
    if( rc != SSH_OK ) {
            ssh_channel_free(channel);
            return NULL;
    }

    rc = ssh_channel_request_exec(channel, command);
    if( rc != SSH_OK ) {
            ssh_channel_close(channel);
            ssh_channel_free(channel);
            cout << "Error";
            return NULL;
    }
    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    while (nbytes > 0)
    {
            receive.append(buffer, nbytes);
            nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    }

    if( nbytes < 0 )
            return NULL;

    ssh_channel_send_eof(channel);
    ssh_channel_close(channel);
    ssh_channel_free(channel);

    return receive;
}
于 2021-01-26T17:33:13.543 回答