有没有办法使用C程序获取.txt文件上的Linux命令(如ifconfig)的输出? [英] Is there a way to obtain the output of a linux command(like ifconfig) on a .txt file using a C program?

查看:31
本文介绍了有没有办法使用C程序获取.txt文件上的Linux命令(如ifconfig)的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上希望使用Linux上的C程序获取.txt文件中的所有网络参数,如IP地址、DNS服务器等。

推荐答案

您需要popen()。以下是摘自here的示例:

void get_popen_data() {
    FILE *pf;
    char command[COMMAND_LEN];
    char data[DATA_SIZE];

    // Execute a process listing
    sprintf(command, "ps aux wwwf"); 

    // Setup our pipe for reading and execute our command.
    pf = popen(command,"r"); 

    if(!pf){
      fprintf(stderr, "Could not open pipe for output.
");
      return;
    }

    // Grab data from process execution
    fgets(data, DATA_SIZE , pf);

    // Print grabbed data to the screen.
    fprintf(stdout, "-%s-
",data); 

    if (pclose(pf) != 0)
        fprintf(stderr," Error: Failed to close command stream 
");

    return;
}

这篇关于有没有办法使用C程序获取.txt文件上的Linux命令(如ifconfig)的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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