【问题标题】:How to save the output of ifconfig command into a buffer?如何将 ifconfig 命令的输出保存到缓冲区中?
【发布时间】:2023-04-10 20:34:01
【问题描述】:

我必须使用 C++ 和 VxWorks 将命令“ifconfig”的输出保存到字符缓冲区中。 我该怎么做?

【问题讨论】:

  • VxWorks没有popen吗?
  • 请添加您尝试过的代码。请查看How to Ask 文章,了解如何让您的问题变得更好。

标签: vxworks ifconfig


【解决方案1】:

ifconfig 是一个 shell 命令,因此您应该能够使用 '>' 将其输出重定向到一个文件,然后读取该文件。

您还可以查看手册中的“Redirecting Shell IO”主题。

【讨论】:

  • 我没有要写入的文件系统,这就是我必须将命令结果复制到缓冲区中的原因。
【解决方案2】:

这是一个使用管道将 ifconfig 的输出保存到缓冲区的示例。 在 C 解释器 shell 上尝试 -> pipe_test-> puts &pipe_buf。祝你好运。

char pipe_buf[128*256];

int pipe_test()
{
    char *pipe_name = "pipe01";
    int pipe_fd;
    int out_fd;
    int nlines;
    int nbytes;

    if (pipeDevCreate(pipe_name,128,256) == ERROR) { /* 128 lines of size 256 bytes */
        perror("pipeDevCreate");
        return -1;
    }

    if ((pipe_fd = open(pipe_name,O_RDWR,0666)) < 0) {
        pipeDevDelete(pipe_name,TRUE);
        perror("open");
        return -1;
    }

    out_fd = ioTaskStdGet(0,STD_OUT);
    ioTaskStdSet(0,STD_OUT,pipe_fd);
    ipcom_run_cmd("ifconfig -a");
    ioTaskStdSet(0,STD_OUT,out_fd);

    if (ioctl(pipe_fd, FIONMSGS, &nlines) == OK && nlines > 0) {
        char *pbuf = &pipe_buf[0];
        int ln;
        memset(pipe_buf,0,sizeof(pipe_buf));        
        for (ln=0; ln<nlines && ln<128; ln++) {
            nbytes = read(pipe_fd,pbuf,256);
            pbuf += nbytes;
        }
    }

    close(pipe_fd);
    pipeDevDelete(pipe_name,TRUE);

    return 0;
}

【讨论】:

    猜你喜欢
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    相关资源
    最近更新 更多