regex - python: Searching thru a router configuration for certain lines that are present -
i have router configuration file. router has thousands of "interfaces", , i'm trying make sure have two lines in each interface configuration section. typical router configuration file this:
<whitespaces> interface_1 <whitespaces> description interface_1 <whitespaces> etc etc' <whitespaces> etc etc etc <whitespaces> <config line searching confirm present> <whitespaces> etc etc etc etc ! ! ! random number of router lines <whitespaces> <second config line searching confirm present> ! <whitespaces> interface_2 <whitespaces> description interface_2 <whitespaces> etc etc <whitespaces> etc etc etc <whitespaces> <config line searching confirm present> <whitespaces> etc etc etc etc ! random number of router lines <whitespaces> <second config line searching confirm present> etc
so want logic: - go thru router config. when see interface_x, on lookout 2 lines after that, , make sure present in config. , move on next interface, , same thing, on , on again.
here tricky part: - want 2 lines in interface config, , python needs know search 'area' line after interface_x , before next interface_y config. - 2 lines i'm searching randomly spaced in config, aren't 10th line , 12th line after interface_x. can present anywhere between interface_x , interface_y (the next interface definition).
dying. been on 4 days , can't seem match correctly.
want python script spit out output says example: "interface_22 , interface_89 missing 2 lines looking tom". don't care if interface right honestly, care when interface configuration wrong.
file_name = 'c:\\python\\router.txt' f = open(file_name, 'r') output_string_obj = f.read() = output_string_obj.splitlines() line in a: if re.match("(.*)(interface)(.*)", line): # logic search presence of 2 lines after matching # interface, prior next instance of interface # definition if re.match("(.*)(check-1-line-present)(.*)", line + ???): if re.match("(.*)(check-2-line-present)(.*)", line ?): print ("this interface has appropriate config") else: print ("this interface missing key 2 lines")
dying guys. first question i've ever posted board. i'll take thoughts/logic flow / statements/ ideas has. in type of search situation alot, don't know in router config missing, know has somewhere between point , point b, have nothing else 'grip' onto.... ''' '
configuration file wouldn't big. concept search in loop thru configuration files, search logic, each individual file isn't big. assume question small config file, see point.
example config similar this: (ill post example tonight)
interface_1 line line line ip-address x.x.x.x line line dhcp y.y.y.y line ! ! interface_2 line line line ip-address z.z.z.z line line dhcp y.y.y.y line ! ! static config, have "line" 30 lines or 10, or whatever. 2 words i'm looking : "ip-address" , "dhcp" after, unique.
basically i'm checking make sure config has both ip address , dhcp configured !
Comments
Post a Comment