简体   繁体   中英

read line from match string in file .. python

I have this script that prints pool names from a F5 config file.. I want to run this on multiple F5 config files.. I want to be able to read from ^pool|^} (pool OR } bring start of line)... At the moment i am getting my desired output as i am telling the script to read from line 2348..

I want to eliminate the use of i in xrange(2348) as other files F5 config files are smaller..

The way it will work is if i get the script to start reading line by line of the file from ^pool OR ^}
Current script:

import re
seenrule = 'false'

File = open("/home/t816874/F5conf/unzip/config/bigip.conf", "r")
for i in xrange(2348):
        File.next()

for line in File:
        if re.match("(^pool p)", line, re.IGNORECASE):
                a = line.lstrip("pool p")
                ONE = a[:-2]
                print "Pool Name:", ONE

        if re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d] {1,3})', line):
               if seenrule == 'false':
                       x = line.lstrip("members ")
                       print x

if re.match("(^rule )", line):
        seenrule = 'true'

---------------------------------------------------------------

My Attempt:

import re
seenrule = 'false'
startline = 'false'

File = open("/home/t816874/F5conf/unzip/config/bigip.conf", "r")
if re.match("(^pool|^})", line):
      startline = 'true'

for line in File:
        if re.match("(^pool p)", line, re.IGNORECASE):
                a = line.lstrip("pool p")
                ONE = a[:-2]
                print "Pool Name:", ONE

        if re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d] {1,3})', line):
               if seenrule == 'false':
                       x = line.lstrip("members ")
                       print x

if re.match("(^rule )", line):
        seenrule = 'true'

Thanks for your help guys !

-----------

conf file is similar to:

node 172.0.0.0 {
   screen ab2583-1.2
}
node 172.0.0.1 {
   screen ab2583-1.3
}
node 172.0.0.3 {
   screen ab2584-1.2
}
pool pWWW_abcd_HTTPS {
   lb method member predictive
   monitor all mWWW_staging.telecom_TCP
   members {
      0.0.0.0:8421 {}
      0.0.0.1:18431 {
         session user disabled
      }
      0.0.0.2:8421 {}
      0.0.0.3:18431 {
         session user disabled
      }
   }
}
pool pWWW_vcm2APP.defg_HTTP {
   lb method member predictive
   monitor all mWWW_vcm2APP.defg_TCP
   members 0.0.0.5:27110 {}
}
node..
....
.

It is not completely clear what you want, but I'll try to guess. You are trying to get members for each pool. You didn't mention anything about seenrule - so I dropped it.

If you don't want to parse this data structure, and if top-level block always and exlusively ends with line '}\\n' , you can do something like this:

import re
f = open('/home/t816874/F5conf/unzip/config/bigip.conf', 'r')

# Iterate over lines.
for line in f:
    # If pool block is met.
    if line.startswith('pool p'):
        pname = line.split()[1]
        pmembers = []
        # Iterate over block.
        # Block is expected to be terminated by line '}\n'.
        for pline in iter(lambda: next(f), '}\n'):
            members = re.findall(r'\d{1,3}(?:\.\d{1,3}){3}:\d{1,5}', pline)
            pmembers.extend(members)

        print(pname, pmembers)

f.close()

output:

pWWW_abcd_HTTPS ['0.0.0.0:8421', '0.0.0.1:18431', '0.0.0.2:8421', '0.0.0.3:18431']
pWWW_vcm2APP.defg_HTTP ['0.0.0.5:27110']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM