최근 라우터/스위치 구성의 무리에서 서브넷이 발생하는지 확인하기 위해 인턴십을위한 스크립트를 작성해야했습니다.Python : txt 파일의 첫 번째 부분에 대한 루프 전용 루프
출력을 생성하는 스크립트를 만들었습니다. 이제 서브넷이 발생하면 aanwezig.txt
에 쓰기를 수행하고 nietAanwezig.txt
에 쓰지 않으면 출력을 읽는 두 번째 스크립트가 필요합니다 (하나는 작동하지 않습니다).
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
이것은 파이썬 프로그램이 아닙니다. 질문을 편집하고 들여 쓰기를 수정하십시오. – david
'output.txt' 모르겠다 – emeth
사이드 노트에서 코드를 정리하고 여분의 줄을 먼저 제거하는 것이 좋습니다. https://gist.github.com/anonymous/11394195 – s16h