Python: For loop only loops over the first part of a txt file -
Recently I had to create a script to check my internship whether a subnet router / switch would be in the bunch of config is. / P>
I created a script that produces the output Now I need a second script (I could not work it in one), which reads the output, if subnet then aanwezig Txt
, if nietAanwezig .txt
.
Many other answers helped me to create this script and it works, but it only executes for the first 48 IPs and is over 2000 ...
checkOutput.py
code:
def main (): file = open ('../ iprangesclean.txt', 'rb') aanwezig = line in file Open ('../Aanwezig.txt', 'w') nietAanwezig = open ('../ nietAanwezig.txt', 'w') output = open ('output.txt', 'rb'): Original Line = line line.rstrip () line = row.Rayle ('', '') line = row.Relay ('\ n', '') line = row.rele ('\ r', '') one, two, Green, four = line. Split ( '.') # 3 by IP: ipaddr = str (a) + "." + Str (two) + "." + Str (three) counter = 1 found = for lin in output: if re.search ("\ b {0} \ b" .format (ipaddr), leision) and found == 0: found = 1 else: found = 2 print counter counter = counter + 1 found == 1: aanwezig.write (originalLine) print elif "written to aanwezig" found == 2: nietAanwezig.write (originalLine) print "written to nietAanwezig" found = 0 file. The format of iprangesclean.txt is similar to the following: 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 ...
Part of output.txt
(I can not give you everything because it has user information):
* device name * .txt: logging 10.138.200.100 * device name * .txt: access-list 37 Permit 10.138.200.96 0.0.0.31 * Device Name * .txt: Access Ch-List 38 Permit 10.138.200.100 * Device Name * .txt: SNMP-Server Host 10.138.200.100 * someword * * Device Name * .txt: Login 10.138 200.100
Try this change:
lijn Found in the output for: = 0 # put it here by re.search ("\ b {0} \ b" .format (ipaddr), leision) and found == 0: found = 1 else: found = 2 print counter counter = Counter + 1 "" Indent is a level, then found it in us for the statement "" "if found == 1: nietAanwezig.write (originalLine) print: aanwezig.write (originalLine) print elif found == 2" aanwezig Written "Written Decision" written to "
If I understand the problem correctly, then it will guide you. A will guide that the right direction if statement
is not currently executed for the statement
. If this fixes your problem, you do not need to found
variable or you may have something similar: For the counter
, lin n These enumerates (output, 1): If re-search ("\ b {0} \ b" .format (ipaddr), lien): aanwezig.write (originalLine) print something "written to aanwezig": nietAanwezig.write (originalLine) Print print counter "written to nietAanwezig"
Please let me know if I have misunderstood the question.
Note I have not tested the code above, see them as the starting point.
Comments
Post a Comment