2

I'm building a script to parse out data from switch configuration files and have come to a bit of hiccup (using Python 3.5)

Here's the pseudo code I'm trying to work through:

if re.match(patter0):
    continue to match pattern1
    if re.match(pattern1):
        test if < 10 minutes
            return pass or fail

Here's the code that I have so far, I've tried many variations including for-in loops, while loops, and nested if statements, but I'm getting no where.

def net1639andnet1624(input_file):
    maxTimeOut = 10
    pattern0 = re.compile("^line vty (([0-9] [0-9])|([0-9] [0-9][0-9]))$")
    pattern1 = re.compile(
    "^ exec-timeout\s([0-9]|[0-9][0-9]|[[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9])\s([0-9]|[0-9][0-9]|[[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9])$")

    for i, line in enumerate(open(input_file)):
        for match in re.finditer(pattern0, line):
            print(line)
            for match in re.finditer(pattern1, line):
                minutes = match.group(1)
                seconds = match.group(2)
                print(line)
                if int (minutes) > maxTimeOut:
                    print('fail')
                else:
                    print('pass')

Last run, this return None when it should be returning 1 for each line vty shown below:

!
line con 0
 exec-timeout 0 30
 stopbits 1
line vty 0 3
 access-class 182 in
 exec-timeout 1440 30
 transport input telnet
line vty 4 15
 access-class 182 in
 no exec
 exec-timeout 0 1
 transport input none
!
end

So basically, I'm trying to get to the point where it tells me

line vty 0 3
exe-timeout 1440 30
fail
line vty 4 15
exec-timeout 0 1
pass
Chris
  • 934
  • 1
  • 17
  • 38
  • 2
    Would it be possible to get the input file so that we can test our suggested fixes? Testing is always good. – Alexander Craggs May 30 '16 at 00:00
  • You can have a look at [this question and anwsers](http://stackoverflow.com/questions/529830/do-python-regexes-support-something-like-perls-g) – le_top May 30 '16 at 01:49
  • @Popey Gilbert. The input file is listed in the original post...it's just a .txt file; copy/paste...it's the 15 lines starting with a `!` and ending with the word `end` – Chris May 30 '16 at 02:00

1 Answers1

0

I may have found a solution though it's not very dynamic which seems like it could present problems in the future. Like if I came across a switch with more than three vty's the code would need adjusted. Here's what I was able to come up with tonight, thoughts or other answers will be considered too:

def net1639(input_file):
    maxTimeOut = 10
    p = 'pass'
    f = 'fail'
    pattern0 = re.compile("^line vty (([0-9] [0-9])|([0-9] [0-9][0-9]))$")
    pattern1 = re.compile(
        "^ exec-timeout\s([0-9]|[0-9][0-9]|[[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9])\s([0-9]|[0-9][0-9]|[[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9])$")

    elements = []
    elements.append([])
    elements.append([])
    elements.append([])

    for i, line in enumerate(open(input_file)):
        for match in re.finditer(pattern0, line):
            if elements[0] is not None:
                elements[0].append(line.strip())
            elif elements[1] is not None:
                elements[1].append(line.strip())
            elif elements[2] is not None:
                elements[2].append(line.strip())
        for match in re.finditer(pattern1, line):
            minutes = match.group(1)
            seconds = match.group(2)
            if elements[0]:
                elements[0].append("Mintues: %s" % minutes)
                if int(minutes) <= maxTimeOut:
                    if int(minutes) == 0:
                        elements[0].append("Seconds: %s" % seconds)
                    elements[0].append(p)
                else:
                    elements[0].append(f)
            elif elements[1]:
                elements[1].append(minutes)
                if int(minutes) > maxTimeOut:
                    if int(minutes) == 0:
                        elements[1].append("Seconds: %s" % seconds)
                   elements[1].append(p)
                else:
                    elements[1].append(f)
            elif elements[2]:
                elements[2].append(minutes)
                if int(minutes) > maxTimeOut:
                    if int(minutes) == 0:
                        elements[1].append("Seconds: %s" % seconds)
                        elements[2].append(p)
                else:
                    elements[2].append(f)


    for row in elements:
        for column in row:
            print(column)

Here is a picture of the output:

enter image description here

Chris
  • 934
  • 1
  • 17
  • 38