CiscoConfigParser & Functions

104 views Asked by At

I am trying to make the code cleaner and better manageable and i wanted to start with reading a cisco file. However when i try to put it in a function, it is not able to give me the outputs. The same works perfectly out of a function

Working model

parse = CiscoConfParse("C:\\python\\mydata\\TestConfigFile.txt")    
TCPSrv = parse.find_objects("service\stcp\sdestination\seq")
UDPSrv = parse.find_objects("service\sudp\sdestination\seq")
ObjectNetwork = parse.find_objects("^object\snetwork\s")
ObjectGroupSrv = parse.find_objects("^object-group\sservice")
ObjectGroupNetwork = parse.find_objects("^object-group\snetwork\s")

This creates a list for all the above like the one below

TCPSrv = [<IOSCfgLine # 83 ' service tcp destination eq https' (parent is # 82)>,<IOSCfgLine # 97 ' service tcp destination eq www '(parent is # 102)>]

But when i put this into a function, it does not work. This is the first time i am trying out to use functions, and I know that i am doing something wrong.

This is my code for Functions

def cisco(filename):
    parse = CiscoConfParse(filename)
    TCPSrv = parse.find_objects("service\stcp\sdestination\seq")
    UDPSrv = parse.find_objects("service\sudp\sdestination\seq")
    ObjectNetwork = parse.find_objects("^object\snetwork\s")
    ObjectGroupSrv = parse.find_objects("^object-group\sservice")
    ObjectGroupNetwork = parse.find_objects("^object-group\snetwork\s")
    return TCPSrv, UDPSrv, ObjectNetwork, ObjectGroupSrv, ObjectGroupNetwork


file = C:\\python\\mydata\\TestConfigFile.txt


cisco(file)

This does not give any output.

>>> TCPSrc
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'TCPSrc' is not defined

I have tried putting it like this below also

cisco("C:\\python\\mydata\\TestConfigFile.txt")

Can someone kindly assist what I am doing wrong.

1

There are 1 answers

0
Mike Pennington On BEST ANSWER

This does not give any output

>>> TCPSrc
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'TCPSrc' is not defined

You have not assigned the return values to anything. When you call cisco(), you need to assign the return values to something... please use:

from ciscoconfparse import CiscoConfParse

def cisco(filename):
    parse = CiscoConfParse(filename)
    TCPSrv = parse.find_objects("service\stcp\sdestination\seq")
    UDPSrv = parse.find_objects("service\sudp\sdestination\seq")
    ObjectNetwork = parse.find_objects("^object\snetwork\s")
    ObjectGroupSrv = parse.find_objects("^object-group\sservice")
    ObjectGroupNetwork = parse.find_objects("^object-group\snetwork\s")
    return TCPSrv, UDPSrv, ObjectNetwork, ObjectGroupSrv, ObjectGroupNetwork

values = cisco("C:\\python\\mydata\\TestConfigFile.txt")
TCPsrv = values[0]
UDPsrv = values[1]
# ... etc unpack the remaining values as illustrated above