1

I am using NMAP for port scanning on a host. When I run the following command on terminal, it gives the results within a minute most of the times.

$ nmap -p 1-65535 10.180.5.237 | awk --posix '{ if ($1 ~ /^[0-9]/) print $0+0}'
22
80
2404

But if I run the same command using system() call in a C program it takes indefinite time, I am saying it indefinite because I gave more than 7 minutes but no output so stopped the process.

if ((system("nmap -p 1-65535 10.180.5.237 | awk --posix '{ if ($1 ~ /^[0-9]/) print $0+0}' > file_openPorts")) == -1) {
      fprintf(stderr, "ERROR : NMAP port scan error...\n");
      return 1;
 }

why is it like that and is there any solution?.

  • 2
    Do you mean on terminal it starts giving the results within a minute most of the times, or it completes the whole execution within a minute? If what happens is that it starts giving the results, you might be seeing the effects of C stdio buffering, where when connected to a terminal it will flush every line, but when redirected to a file it will only flush when it has a lot more data to write... – filbranden Mar 27 '18 at 5:09
  • Could not reproduce... – sneep Mar 27 '18 at 6:03
  • It took 31 sec to completes whole execution without redirecting the output to a file. and I tried redirecting the output to a file, then it took an almost same amount of time(33 sec), and the process completes there. – reddi hari Mar 27 '18 at 6:26
  • 2
    Just a note: The awk command can be written as awk '$1 ~ /^[0-9]/ { print $0+0 }'. The --posix bit is not needed and it's more common to use "pattern { action }" rather than "{ if (condition) action }". – Kusalananda Mar 27 '18 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.