与popen()连接的telnet连接的错误密码在c++

发布于 2025-01-22 20:38:24 字数 2653 浏览 0 评论 0原文

对于一个项目,我需要使用Telnet协议控制一个远程电源套接字(来自APC构造函数的AP7920B)。此连接在C ++程序中使用。

为了启动连接,使用popen()函数。

#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;
}

登录名和密码在标题中声明为字符串:

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

我的代码使用popen()打开腻子终端并发送telnet命令以在远程插座上连接。连接后,Telnet请求登录。 fputs()键入登录。 telnet请求密码。 fputs()键入密码。 fputs仅采用一个CSTRING(解释了.c_str())。

我的问题是密码有9个字符(代替8个字符)用于Telnet(我认为Telnet以Cstring为单词),我无法登录。我们有一个日志文本可以注意。

但是,登录是正确的,被Telnet(没有其他字符)接受。

您应该知道我的代码适用于另一个远程套接字(其他构造函数)。

您对这个问题有想法吗?如何解决?

提前致谢 !

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情泪▽动烟 2025-01-29 20:38:24

如下所示, fputs()该函数开始从地址指定的地址指定(str)开始复制复制直到达到终止的空字符('\ 0')。此终止零字符未复制到流中。

也许问题是您只能发送8个字样,因此也许您必须将密码分为2个部分,并发送另一个字符:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文