How to save the output of ifconfig command into a buffer?

旧巷老猫 提交于 2019-12-02 10:41:42

问题


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


回答1:


ifconfig is a shell command, so you should be able to redirect its output to a file using '>' and then read that file.

You can also have a look at the topic 'Redirecting Shell IO' in the manual.




回答2:


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


来源:https://stackoverflow.com/questions/48729861/how-to-save-the-output-of-ifconfig-command-into-a-buffer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!