如何将ifconfig命令的输出保存到缓冲区中? [英] How to save the output of ifconfig command into a buffer?

查看:244
本文介绍了如何将ifconfig命令的输出保存到缓冲区中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用C ++和VxWorks将命令"ifconfig"的输出保存到char缓冲区中. 我该怎么办?

I have to save the output of the command "ifconfig" into a char buffer using C++ and VxWorks. How can I do it?

推荐答案

下面是一个使用管道将ifconfig输出保存到缓冲区的示例. 在C解释器外壳程序上尝试-> pipe_test-> puts &pipe_buf.祝你好运.

Here is an example using pipe to save the output of ifconfig into a buffer. Try -> pipe_test and -> puts &pipe_buf on the C interpreter shell. Good luck.

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

这篇关于如何将ifconfig命令的输出保存到缓冲区中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆