【问题标题】:How to pipe through the output of hping3 from child to parent?如何将 hping3 的输出从子级传输到父级?
【发布时间】:2014-11-28 14:03:03
【问题描述】:

我最近问了一个关于如何在 c 程序中调用 hping3 的问题。 提供的解决方案有效,但我现在有另一个问题。 hping3 的输出必须传递给父进程,所以我做了以下操作。 请注意,这只是我认为错误所在的代码n-p。 下面的 sn-ps 在一个循环中运行,我在其中迭代一系列 ip 地址,这是在我的 mpi 程序中提供两种不同的分发策略所必需的。

//Stick together the params
sprintf(params, "--scan %u %u.%u.%u.%u -V", *(portarray + i), (iterator & 0xFF000000)>>24, (iterator & 0x00FF0000)>>16, (iterator & 0x0000FF00)>>8, (iterator & 0x000000FF));
            //Pipe and check status
            if(pipe(pipes)==-1){
               perror("Error piping");
            }
            //Fork and check status
            pid=fork();
            if(pid == -1){
               perror("Error forking");
            } else if(pid > 0){
               //Parent does not write
               close(pipes[1]);
               //Save stdout from pipe
               nbytes = read(pipes[0], buffer, sizeof(buffer));
               //Parent, wait for child
               waitpid(pid, &status, 0);
               //Print out pipe
               printf("hping3: (%.*s)\n", nbytes, buffer);
               wait(NULL);
               close(pipes[0]);
            } else {
               //Child does not read
               close(pipes[0]);
               //Map stdout and stderr to write pipe-end
               dup2(pipes[1], 1);
               dup2(pipes[1], 2);
               //Child, exec hping with params
               execl("sudo /usr/sbin/hping3","sudo /usr/sbin/hping3",params,NULL);
               close(pipes[1]);
               //Exit child to prevent fork-bomb
               return 0;
            }
            //Sleep for specified delaytime
            sleep((unsigned int)delay);

我不知道问题出在哪里。 输出如下(当然是在循环中):

hping3()

hping3 程序打印到 stdout 和 stderr,我通过输出重定向到文件在 shell 上对其进行了测试。

【问题讨论】:

    标签: c pipe exec fork output-redirect


    【解决方案1】:

    缓冲区是如何定义的?你有没有检查过 nbytes 的值?您确定正在执行 hping 吗?我会尝试以下方法:

    int buffsize  = 100;
    char * buffer = malloc(buffsize);
    
    if (pid == -1) {
       perror("Error forking");
    } else if (pid > 0) {
       //Parent does not write
       close(pipes[1]);
       // I'd first wait for the child to finish.
       waitpid(pid, &status, 0);
       // Save stdout from pipe 
       // recall that sizeof(buffer) != buffsize when malloc'ed.
       int count = 0;
       while((nbytes = read(pipes[0], buffer+count, buffsize-count) != -1) count += nbytes;
       //Print out pipe
       printf("hping3: (%.*s)\n", nbytes, buffer);
       wait(NULL);
       close(pipes[0]);
    } else {
       //Child does not read
       close(pipes[0]);
       //Map stdout and stderr to write pipe-end
       dup2(pipes[1],1);
       dup2(pipes[1],2);
       // Close pipes[1]
       close(pipes[1]);
       //Child, exec hping with params
       if (execl("sudo /usr/sbin/hping3","sudo /usr/sbin/hping3",params,NULL) == -1)
          perror("execl error");
       //Exit child to prevent fork-bomb
       return 0;
    }
    

    【讨论】:

    • 缓冲区被定义为字符缓冲区[4096]。 nbytes 定义为 int nbytes 并在主循环之前用 0 初始化。由于我在该程序的早期版本中忘记了一个参数,因此正在执行 hping3 知道这一点
    • 您是否尝试过代码(先等待pid,然后读取)?有用吗?
    • Sry 无法做到这一点,最后几天真的很紧张。
    • 终于有时间测试它,不幸的是它不适合我。执行卡在 execl 语句中。 hping3 实际上没有被执行。
    猜你喜欢
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    相关资源
    最近更新 更多