Linux channel: capture ping real-time output via popen

Linux / C / pipes:

How can I capture ping command output using popen (or similar system calls). Currently, popen will wait for ping to complete. Then the output will be reset together.

Pseudocode:

fp= popen("ping xxxx", "r"); while(!feof(pFp)) { if(fgets(fp ...) // <==currently the code blocks here until ping finishes in popen { printf(...real time ping output here); } } 
+6
source share
1 answer

This is not waiting for ping to complete. Rather, ping waits until the stdout buffer is full before writing anything. The only ways to avoid this are pseudo-ttys. Either you must abandon popen and write code to start the ping child process yourself, and use pseudo-tty to communicate (this is easy with the non-standard, but widely available forkpty function), or you can write a wrapper program that starts ping through pseudo- pty and captures the output signal and writes it without buffering to stdout .

+7
source

Source: https://habr.com/ru/post/891721/


All Articles