'Wrong password for telnet connection with popen() in C++

For a project, I need to control a remote power socket (AP7920b from APC constructor) with Telnet protocol. This connection is used in a C++ program.

To initiate the connection, popen() function is used.

#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <cstdlib>
#include <cstring>

bool telnet_fixture::send_telnet_command(std::string &command, std::string &socket)
{
    std::cout << socket << std::endl;
    bool result = false;
    // replace socket number in command template
    //command.replace(4, 6, socket);

    // launch command
    std::string command_line_with_parameters = m_plink_path + " " + m_ip + m_plink_command_options + ">status_connection_telnet.txt";
    std::cout << "Opening telnet connection with " << command_line_with_parameters << " to launch command '" << command << "'" << std::endl;
    FILE *command_pipe = popen(command_line_with_parameters.c_str(), "w");
    if (command_pipe == nullptr)
    {
        //throw process_open_exception{command_line_with_parameters};
        std::cout << "Unable to launch command '" << command_line_with_parameters << "' !" << std::endl;
    }
    else
    {
    std::cout << "Command '" << command_line_with_parameters << "' launched correctly" << std::endl;
    }
    //Delay
    usleep(1000000);

    // Type in login
    std::cout << "Typing in login : " << m_login << std::endl;
    m_login += "\n";
    fputs(m_login.c_str(), command_pipe);

    // Type in password
    std::cout << "Typing in password : " << m_password.c_str() << std::endl;
    fputs(m_password.c_str(), command_pipe);
    fputs("\n", command_pipe);

    // Type in command
    std::cout << "Typing in command : " << command << std::endl;
    command += "\n";
    fputs(command.c_str(), command_pipe);

    // Type in exit command
    fputs(m_commands[m_type]["EXIT"].c_str(), command_pipe);
    fputs("\n", command_pipe);

    auto return_code = pclose(command_pipe);
    std::cout << "Command finished and returned " << return_code << std::endl;

    return result;
}

The login and the password is declared as a string in the header:

std::string m_login = "login";
std::string m_password = "password";

My code use popen() to open a putty terminal and send Telnet command to connect on the remote socket. After connection, Telnet requests a login. fputs() type the login. And Telnet requests a password. fputs() type the password. fputs take only a cstring (explained the .c_str()).

My issue is the password have 9 characters (instead 8 characters) for telnet (I think telnet take the null term of cstring) and I can't log in. We have a log text to note this.

However, the login is correct and accepted by Telnet (without another character).

You should know that my code works for another remote socket (other constructor).

Do you have an idea of ​​the problem ? How to solve it ?

Thanks in advance !



Solution 1:[1]

As defined here fputs() the function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This terminating null-character is not copied to the stream.

Maybe the issue is that you can only send 8-bytes so maybe you have to break your password into 2 parts and send one more character for example :

fputs(m_password_first_8_chars.c_str(), command_pipe);
fputs(m_password_last_char.c_str_(), command_pipe);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gigis95