2017-09-29 21 views
0

그래서 IP/Hostname 스캐너를 만들었고 핑을 실행하거나 핑을 실행하지 못하면 출력을 파일로 보내려고합니다.Popen이 파일에 쓰게하는 방법

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' 

이는 str을 출력과는 popen 모두 파일로 작성하거나 할 얻을 수있는 방법이 있나요 실제 코드가

#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() 

모습입니다 : 내 문제는 내가 이러한 오류를 얻을 수있다 코드를 완전히 변경해야합니까? 인수 1이어야 문자열 또는 읽기 :

오류를

형식 오류를 throw는 popen을 인쇄되어 지금은 작동하지 않습니다이 대신

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() 

있는 유일한 방법을 수행하여 내 문제의 일부를 해결 나는 당신의 문제가 제대로 f.write(output)의 라인이 형식 오류를 throw 이해한다면 - 단지 문자 버퍼,

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() 

답변

1

을는 popen 없습니다.

출력이 귀하의 경우에 Popen 개체이기 때문에 이런 경우입니다. 이들과

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

:

# ... 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 

편집 : 이 라인을 교체 또한

를 계속하기 전에 errorsOfSubProcess에 빈 resultOfSubProcess 변수 또는 오류에 대해 확인 할 수 있습니다