【问题标题】:Is there a way to obtain the output of a linux command(like ifconfig) on a .txt file using a C program? [duplicate]有没有办法使用 C 程序在 .txt 文件上获取 linux 命令(如 ifconfig)的输出? [复制]
【发布时间】:2013-08-20 06:15:53
【问题描述】:

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

【问题讨论】:

    标签: c networking network-programming


    【解决方案1】:

    你想要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.\n");
          return;
        }
    
        // Grab data from process execution
        fgets(data, DATA_SIZE , pf);
    
        // Print grabbed data to the screen.
        fprintf(stdout, "-%s-\n",data); 
    
        if (pclose(pf) != 0)
            fprintf(stderr," Error: Failed to close command stream \n");
    
        return;
    }
    

    【讨论】:

    【解决方案2】:

    是的,popen 很好,而我还有其他方法。您只需打开文件并使用 dup2 将文件转换为 STDOUT_FINO ,然后调用 system ,做任何你想做的事情。输出只是打印到文件a.txt

    代码:

    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    int main(int argc,char *argv[])
    {
        int fd;
        if(argc != 2)
        {printf("./a.out <cmdString>\n"); return -1;}
    
        if((fd = open("a.txt",O_RDWR|O_CREAT|O_TRUNC)) < 0)
        {printf("open a.txt error\n");return -1;}
    
        if(dup2(fd,STDOUT_FILENO) != STDOUT_FILENO)
        {printf("dup2 error\n");return -1;}
    
        system(argv[1]);
    
    
        close(fd);  
        return 0;
    }
    

    编译并调用 ./a.out ifconfig 。

    文件 a.txt:

    eth0      Link encap:Ethernet  HWaddr 00:26:22:0A:CD:51  
              inet addr:192.168.1.4  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::226:22ff:fe0a:cd51/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:1655 errors:0 dropped:0 overruns:0 frame:2
              TX packets:1344 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:829059 (809.6 Kb)  TX bytes:180519 (176.2 Kb)
              Interrupt:17 
    
    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:65536  Metric:1
              RX packets:617 errors:0 dropped:0 overruns:0 frame:0
              TX packets:617 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:49256 (48.1 Kb)  TX bytes:49256 (48.1 Kb)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      相关资源
      最近更新 更多