2014-04-29 3 views
1

최근 라우터/스위치 구성의 무리에서 서브넷이 발생하는지 확인하기 위해 인턴십을위한 스크립트를 작성해야했습니다.Python : txt 파일의 첫 번째 부분에 대한 루프 전용 루프

출력을 생성하는 스크립트를 만들었습니다. 이제 서브넷이 발생하면 aanwezig.txt에 쓰기를 수행하고 nietAanwezig.txt에 쓰지 않으면 출력을 읽는 두 번째 스크립트가 필요합니다 (하나는 작동하지 않습니다).

는 다른 답변의 많은이 스크립트를 만들기 위해 저를 도와 그것은 작동하지만 그것은 단지 첫번째 48 개 IP에 대해 실행하고

checkOutput.py의 코드 ... 2000 이상이 있습니다 :

def main(): 

    file = open('../iprangesclean.txt', 'rb') 
    aanwezig = open('../aanwezig.txt', 'w') 
    nietAanwezig = open('../nietAanwezig.txt', 'w') 
    output = open('output.txt', 'rb') 

    for line in file: 
     originalLine = line 
     line.rstrip() 
     line = line.replace(' ', '') 
     line = line.replace('\n', '') 
     line = line.replace('\r', '') 
     one,two,three,four = line.split('.') 

     # 3Byte IP: 
     ipaddr = str(one) + "." + str(two) + "." + str(three) 

     counter = 1 
     found = 0 
     for lijn in output: 

      if re.search("\b{0}\b".format(ipaddr),lijn) and found == 0: 
       found = 1        
      else: 
       found = 2 

      print counter 
      counter= counter + 1 

     if found == 1: 
      aanwezig.write(originalLine) 
      print "Written to aanwezig" 
     elif found == 2: 
      nietAanwezig.write(originalLine) 
      print "Written to nietAanwezig" 
     found = 0 

    file.close() 
    aanwezig.close() 
    nietAanwezig.close() 


main() 
iprangesclean.txt의

형식은 다음과 같다 :

10.35.6.0/24 
10.132.42.0/24 
10.143.26.0/24 
10.143.30.0/24 
10.143.31.0/24 
10.143.32.0/24 
10.35.7.0/24 
10.143.35.0/24 
10.143.44.0/24 
10.143.96.0/24 
10.142.224.0/24 
10.142.185.0/24 
10.142.32.0/24 
10.142.208.0/24 
10.142.70.0/24 
and so on... 

output.txt의 일부 (이것은 사용자가 통보가 있기 때문에 내가 모든 것을 줄 수 없다 ATION) :

*name of device*.txt:logging 10.138.200.100 
*name of device*.txt:access-list 37 permit 10.138.200.96 0.0.0.31 
*name of device*.txt:access-list 38 permit 10.138.200.100 
*name of device*.txt:snmp-server host 10.138.200.100 *someword* 
*name of device*.txt:logging 10.138.200.100 
+0

이것은 파이썬 프로그램이 아닙니다. 질문을 편집하고 들여 쓰기를 수정하십시오. – david

+0

'output.txt' 모르겠다 – emeth

+0

사이드 노트에서 코드를 정리하고 여분의 줄을 먼저 제거하는 것이 좋습니다. https://gist.github.com/anonymous/11394195 – s16h

답변

1

이 변화를 시도 : 나는 문제를 정확하게 이해한다면

for lijn in output: 
    found = 0 # put this here 
    if re.search("\b{0}\b".format(ipaddr),lijn) and found == 0: 
     found = 1 
    else: 
     found = 2 

    print counter 
    counter= counter + 1 

    """Indent one level so it us in the for statement""" 
    if found == 1: 
     aanwezig.write(originalLine) 
     print "Written to aanwezig" 
    elif found == 2: 
     nietAanwezig.write(originalLine) 
     print "Written to nietAanwezig" 

이 올바른 방향으로 당신을 안내합니다. if statement은 현재 for statement에서 실행되지 않습니다. 문제가 해결되면 found 변수가 필요하지 않습니다. 당신은 단지 다음과 같은 것을 가질 수 있습니다 :

for counter, lijn in enumerate(output, 1): 
    if re.search("\b{0}\b".format(ipaddr),lijn): 
     aanwezig.write(originalLine) 
     print "Written to aanwezig" 
    else: 
     nietAanwezig.write(originalLine) 
     print "Written to nietAanwezig" 

    print counter 

제가 질문을 오해 한 경우 알려주십시오.

참고 위의 코드는 테스트하지 않았으므로 시작 지점으로 사용해보십시오.

+0

if == 1 ....을 for 루프에 구현 했으므로 작동해야하는 것처럼 보이지만 여전히 동일한 문제가 있습니다. 2337의 처음 48 줄만 수행합니다 ... 나는 이중 검사를했습니다. , iprangesclean.txt에 48 행 주위에 공백이 없거나 없습니다. – MrC00p3r

관련 문제