0

I am using fping program to get network latencies of a list of hosts. Its a shell program but I want to use it in my python script and save the output in some database.

I am using subprocess.call() like this:

import subprocess
subprocess.call(["fping","-l","google.com"])

The problem with this is its an infinite loop given indicated by -l flag so it will go on printing the input to the console. But after every output, I need some sort of callback so that I can save it in db. How can I do that?

I looked for subprocess.check_output() but its not working.

1
  • use this link https://stackoverflow.com/a/75175057/12780274 is very simple Commented Jan 19, 2023 at 16:05

1 Answer 1

1

This may help you:

def execute(cmd):
    popen = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE,
                             universal_newlines=True)
    for stdout_line in iter(popen.stdout.readline, ""):
         yield stdout_line
    popen.stdout.close()
    return_code = popen.wait()
    if return_code:
        raise subprocess.CalledProcessError(return_code, cmd)

So you can basically execute:

for line in execute("fping -l google.com"):
    print(line)

For example.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.