0

I am trying to write my below pinging script results into a Text file, but i got an error message.

Below is my code:

import os
import time
import subprocess


# Open file for saving ping results
results_file = open("results.txt", "w")

with open('ip-source.txt') as IPs:
    hosts = IPs.read()
    hosts = hosts.splitlines()
    for ip in hosts:
        os.system('cls')
        print('Printing now:', ip)
        print('-'*60)
        result = os.system('ping -n 4 {}'.format(ip))
        print('-'*60)
        time.sleep(1)

with open("output.txt", "w", encoding="utf8") as output:
    output.write("\n".join([post.text for post in result]))

and when i ran it i got below error message:

Traceback (most recent call last):
File "script.py", line 21, in <module>
output.write("\n".join([post.text for post in result]))
TypeError: 'int' object is not iterable
5
  • what is the type of result? Commented Aug 25, 2020 at 5:16
  • take a look at what os.system returns, it is not the console output, but rather the exit signal (0,1) Commented Aug 25, 2020 at 5:16
  • result containing int data. can you please print result Commented Aug 25, 2020 at 5:17
  • 1
    Does this answer your question? Running shell command and capturing the output Commented Aug 25, 2020 at 5:18
  • i printed the output of 'result' it is 0-1 Commented Aug 25, 2020 at 5:32

2 Answers 2

1

Probably better to open the output file once, then have each of the subprocesses append to it as you go.

with open('ip-source.txt') as IPs, open("output.txt", "w") as output:
    for ip in IPs:
        subprocess.run(
            ['ping', '-n', '4', ip.rstrip('\n')],
            stdout=output, text=True, check=True)

Notice also how this avoids invoking a shell at all.

You could streamline this slightly by opening the output file as binary; then you can take out the text=True and let the file accept raw bytes (even though they are eventually going to be parsed as Unicode strings by whatever reads the text file subsequently).

In Python 3, a subprocess returns bytes by default; you then have to decode those bytes into a str (once you know their encoding); or you can specify text=True to have Python transparently encode and decode all these bytes as strings, using the default encoding (UTF-8 on any adult system; YMMV on Windows).

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

2 Comments

How can exclude those ping results with Request timed out. or destination unreachable because it causes a problem later in analysis
Then you can't write directly to the file, or maybe you'll want to grep -v -e 'Request timed out' -e 'destination unreachable' file and take it from there. To filter them out while Python is reading, use result = subprocess.run(..., capture=True) and loop over result.stdout.splitlines() to filter out the ones you don't want. This isn't hard to find from the subprocess documentation; ask a new question if you can't figure it out.
1

os.system returns the exit signal (typically 0 or 1) from the process called. If you look, os.system is actually just a wrapper function for subprocess.Popen.

You'll want to a) use something like subprocess.Popen or subprocess.check_output and then b) add results to a list:

import subprocess
...
results = []
for ip in hosts:
    ...
    results.append(str(subprocess.check_output(["ping", "-n", "4", str(ip)])))

with open("output.txt", "w", encoding="utf8") as output:
    output.writelines(results)

4 Comments

Unfortunatuly i got below error: Traceback (most recent call last): File "script.py", line 23, in <module> output.writelines(results) TypeError: write() argument must be str, not bytes
You can fix that with text=True but I'll post a separate answer with more details.
@AhmedM. Just convert the output to a str object then
Lovely bro, it worked after converted the output to str! Thanks!

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.