2017-09-29 2 views
0

So habe ich gemacht und IP/Hostname Scanner und ich versuche, die Ausgabe in eine Datei zu drucken, nachdem es den Ping ausgeführt oder schlägt fehl, den Ping auszuführen. Mein Problem ist, ich diese Fehler erhalten:Wie Popen in eine Datei schreiben

Traceback (most recent call last): 
    File ".\IPlookup.py", line 59, in <module> 
    print >> IPtext," is down!", 'Filename:', filename 
AttributeError: 'str' object has no attribute 'write' 

Traceback (most recent call last): 
File ".\IPlookup.py", line 55, in <module> 
    print >> output, 'Filename:', filename 
AttributeError: 'Popen' object has no attribute 'write' 

Dies ist, was der eigentliche Code wie

#This here code is used to scan IP's hostnames or files and ping them, then if there is 0% packet loss it does an nslookup... 

import sys 
import os 
import subprocess 

#This is supposed to print the output to a txt file but boy howdy does it not work 

elif userType == '-l': 
    IPtext = raw_input("Please enter IP or URL: ") 
    response = os.system("ping -c 1 " + IPtext) 
    output = subprocess.Popen(['nslookup',IPtext]) 
    if response == 0: 
     f = open('out.txt','w') 
     print >> output, 'Filename:', filename 
     f.close() 
    else: 
     f = open('out.txt','w') 
     print >> IPtext," is down!", 'Filename:', filename 
     f.close() 

sieht Gibt es einen Weg, um sowohl die str Ausgang und die popen zu bekommen, um eine Datei zu schreiben oder tun Ich muss meinen Code komplett ändern?

einen Teil meines Problems gelöst diese stattdessen

elif userType == '-l': 
    with open('out.txt','a') as f: 
     IPtext = raw_input("Please enter IP or URL: ") 
     response = os.system("ping -c 1 " + IPtext) 
     output = subprocess.Popen(['nslookup',IPtext]) 
     if response == 0: 
      f.write(output) 
      f.close() 
     else: 
      f.write(IPtext) 
      f.close() 

Das einzige, was jetzt nicht mehr funktioniert ist Ausdruck von popen, indem Sie die den Fehler

Typeerror wirft: Argument 1 muss Zeichenfolge oder lesen -nur Zeichenpuffer, popen nicht

elif userType == 't -l' or userType == 't --logfile': 
     with open('Pass.txt','a') as f: 
     IPtext = raw_input("Please enter IP or URL: ") 
     response = os.system("ping -c 1 " + IPtext) 
     merp = subprocess.Popen(['nslookup',IPtext], stdout=subprocess.PIPE) 
     out, err = merp.communicate() 
     if response == 0: 
      f.write('\n') 
      f.write(out) 
      f.close() 
     else: 
      with open('Fail.txt','a') as f: 
       f.write('\n') 
       f.write(IPtext) 
       f.close() 

Antwort

1

Wenn ich Ihr Problem richtig verstehe, die Linie der f.write(output) die Typeerror wirft.

Dies ist der Fall, weil Ausgabe in Ihrem Fall ein Popen-Objekt ist. Ersetzen Sie diese Zeilen:

output = subprocess.Popen(['nslookup',IPtext]) 
    if response == 0: 
     f.write(output) 

mit diesen:

# ... everything before your .subprocess.Popen(...) line 
popen = subprocess.Popen(['nslookup',IPtext], stdout=subprocess.PIPE)# this is executed asynchronus 
popen.wait() # this is to wait for the asynchron execution 
resultOfSubProcess, errorsOfSubProcess = popen.communicate() 
# resultOfSubProcess should contain the results of Popen if no errors occured 
# errorsOfSubProcess should contain errors, if some occured 
    if response == 0: 
     f.write(resultOfSubProcess) 
     # ... the rest of your code 

Edit: Sie können auch gegen einen leeren resultOfSubProcess Variable oder Fehler in errorsOfSubProcess überprüfen möchten, bevor Sie fortfahren