Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to save the results in a variable? #59

Closed
sayment opened this issue May 10, 2022 · 2 comments
Closed

Is it possible to save the results in a variable? #59

sayment opened this issue May 10, 2022 · 2 comments

Comments

@sayment
Copy link

sayment commented May 10, 2022

I'm using the ping3 module in separate threads for every interface in the machine by the help of threading module.
Ping3 module gives the system output/prints to the screen. For that reason, I'm trying to get the system output in a variable using io module. In these case, instead of saving the output to the separate files for every thread/IP, all the outputs will be saved in a single file.
Instead of getting the output using alternative methods, is it possible to save the verbose_ping output directly to a variable? When I tried this, it returns NoneType variable.

You can see the snippets below.

threads = []

for i in range(len(IPs)):
    t = Thread(target=_ping, args=(serverIP, IPs[i], duration, resultDirectoryName, ))
    threads.append(t)
    t.start()

for t in threads:
    t.join()

It's the function that I get the results using io module.

def _ping(dest_addr, src_addr, duration, resultDirectoryName):

    output = io.StringIO()
    
    with redirect_stdout(output):
        verbose_ping(dest_addr=dest_addr, count=duration, interval=1, src_addr=src_addr)
    
    pingOutput = output.getvalue().rstrip().split("\n")
      
    fileName = PATH_RESULT   "\\"   resultDirectoryName   "\\"   resultDirectoryName   \
        "_ip"   src_addr.split(".")[-1]   ".txt"
    
    fileName = open(fileName, "w")
    fileName.writelines(output.getvalue())
    fileName.close()
    
    return pingOutput

Output file. The logs should have saved in to different files separated by IP address its send from.

ping 'google.com.tr' from '192.168.0.243' ... 62ms ping 'google.com.tr' from '192.168.0.108' ... 62ms ping 'google.com.tr' from '192.168.0.237' ... 62ms ping 'google.com.tr' from '192.168.0.243' ... 37ms ping 'google.com.tr' from '192.168.0.108' ... 84ms ping 'google.com.tr' from '192.168.0.237' ... 84ms ping 'google.com.tr' from '192.168.0.243' ... 31ms ping 'google.com.tr' from '192.168.0.108' ... 56ms ping 'google.com.tr' from '192.168.0.237' ... 56ms ping 'google.com.tr' from '192.168.0.243' ... 64ms ping 'google.com.tr' from '192.168.0.108' ... 30ms ping 'google.com.tr' from '192.168.0.237' ... 80ms ping 'google.com.tr' from '192.168.0.243' ... 29ms ping 'google.com.tr' from '192.168.0.108' ... 35ms ping 'google.com.tr' from '192.168.0.237' ... 30ms ping 'google.com.tr' from '192.168.0.243' ... 33ms ping 'google.com.tr' from '192.168.0.108' ... 28ms ping 'google.com.tr' from '192.168.0.237' ... 28ms ping 'google.com.tr' from '192.168.0.243' ... 20ms ping 'google.com.tr' from '192.168.0.108' ... 31ms ping 'google.com.tr' from '192.168.0.237' ... 22ms ping 'google.com.tr' from '192.168.0.243' ... 31ms ping 'google.com.tr' from '192.168.0.108' ... 22ms ping 'google.com.tr' from '192.168.0.237' ... 31ms ping 'google.com.tr' from '192.168.0.243' ... 29ms ping 'google.com.tr' from '192.168.0.108' ... 31ms ping 'google.com.tr' from '192.168.0.237' ... 53ms ping 'google.com.tr' from '192.168.0.243' ... 32ms

Thanks for your help.

@kyan001
Copy link
Owner

kyan001 commented May 10, 2022

As you mentioned in your code, io.StringIO is a good way to save the output as a variable.

Beside, you can use the following code to save the ping result.

# verbose_ping
with redirect_stdout(output):
        verbose_ping(dest_addr=dest_addr, count=duration, interval=1, src_addr=src_addr)


# ping
import time

for i in range(duration):  # count=duration
    delay = ping(dest_addr=dest_addr, src_addr=src_addr, unit="ms")
    log_ping_to_file(filename, "ping ... from ... "   str(delay))  # write the delay to your log file
    time.sleep(1)  # interval=1

@sayment
Copy link
Author

sayment commented May 10, 2022

Dear @kyan001 , thank you very much for your quick response. It worked. Thanks.

Btw, I have just checked the closed issues. #23 is doing the same thing. Using multiprocessing module instead of threading solved the conflict as well.
Thanks again.

@sayment sayment closed this as completed May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants