-1

残念ながら、この質問をする前に多くの投稿を読みましたが、私の問題に必要な解決策を提供してくれるものはありませんでした。gnu c で mpi プログラムを作成しており、前の子プロセス内で hping3 を実行しようとしています。実際のステータスは、子を生成でき、おそらく hping3 コマンドを実行できるということですが、子のパイプ出力により、hping3 からのものである可能性がある次のエラー メッセージが表示されます。

"you must specify only one target host at a time"

私は次のことを行いました:最初に、パラメータを埋めるために大量の文字配列を作成しました。なぜ配列?実行時にポートと IP を変更したい (反復ループ)

//Parameter list for hping3 fork, maxlength precalculated (tcp restrictions and given params for hping)
char sudo[] = "/usr/bin/sudo";
char path[] = "/usr/sbin/hping3";
char scan[] = "--scan";
char port[] = "65535";
char ip[] = "192.168.111.111";
char verbose[] = "-V";
char *params[7];
params[0]=sudo;
params[1]=path;
params[2]=scan;
params[3]=port;
params[4]=ip;
params[5]=verbose;
params[6]=NULL;

パラメータの変更は次のようになります。

sprintf(*(params + 3), "%u", *(portarray+i));
sprintf(*(params + 4), "%u.%u.%u.%u", (iterator & 0xFF000000)>>24, (iterator & 0x00FF0000)>>16, (iterator & 0x0000FF00)>>8, (iterator & 0x000000FF));

そして最後にフォーク。

if(pid == -1){
               perror("Error forking");
            } else if(pid > 0){
               //Parent does not write
               close(pipes[1]);
               //Status Message
               printf("Pipe Output ...");
                fflush(stdout);
               //Parent, wait for child
               //waitpid(pid, &status, 0);
               //Save stdout from pipe
               while((nbytes = read(pipes[0], buffer+count, buffsize-count)) != -1) count+=nbytes;
               //Print out pipe
               printf("hping3: (%.*s)\n", nbytes, buffer);
               fflush(stdout);
               wait(NULL);
               close(pipes[0]);
            } else {
               //Child does not read
               close(pipes[0]);
               //Map stdout and stderr to write pipe-end
               dup2(pipes[1], STDOUT_FILENO);
               //dup2(pipes[1], STDERR_FILENO);

               //Status Message
               printf("Try to execute hping3 ...");
               fflush(stdout);

               //Child, exec hping with params
               execv("/usr/sbin/hping3",params);
               puts("never happens");
               close(pipes[1]);
               exit(1);
            }

私はこの問題で立ち往生しており、現在12時間解決に取り組んでいます。

4

1 に答える 1

0

この問題の有効な解決策は、execve を使用することでした。execv でも動作します。完全なパスで sudo を呼び出し、hping3 とそのパラメーターを sudo コマンドに渡す必要がありました。

//Child, exec hping with params
execve("/usr/bin/sudo",params, NULL);
于 2015-01-09T14:11:36.807 に答える