Home > Blockchain >  Can you omit initial message on netstat call in program
Can you omit initial message on netstat call in program

Time:05-03

Is there anyway to omit the initial 2 lines when calling system("netstat -ltnp -a | grep ./server");?

I dont want my program to read "(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)"

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:8091            0.0.0.0:*               LISTEN      109392/./server     
tcp        0      0 127.0.0.1:8101          127.0.0.1:57114         ESTABLISHED 108863/./server     
tcp        0      0 127.0.0.1:41238         127.0.0.1:8091          ESTABLISHED 109397/./client     
tcp        0      0 127.0.0.1:8091          127.0.0.1:41238         ESTABLISHED 109392/./server     
tcp        0      0 127.0.0.1:57114         127.0.0.1:8101          ESTABLISHED 108865/./client

CodePudding user response:

The trivial solution would be to replace grep with e.g. sed or Awk, which easily let you add such constraints.

system("netstat -ltnp -a | sed -e '1,2d' -e '\\%./server%'");

(Properly speaking, the dot should be backslashed, too.)

For anything more involved, perhaps create a parser for the netstat output (and/or see if you can get it to emit results in a well-defined machine-readable format).

Are you sure this warning message is printed on standard output, though? Any well-behaved Unix utility should be emitting diagnostic messages to standard error, not standard output. The simple solution then is to redirect standard error to /dev/null.

  • Related