我有代码可以在'line vty'上找到'access-class'的名称
之后,我能够找到 ACL,但随后我想检查 ACL 的每一行以验证“拒绝”和“允许”语句是否具有“日志”关键字。如果没有“log”语句,则打印 OPEN 报告,如果条目上有“log”语句,则打印 NOT A FINDING 报告。
这就是我不知道如何解析 ACL 语句的地方,我是否可以使用 CiscoConfParse 或更标准的 python 来完成这项工作?
#Importing the necessary modules.
import sys
from sys import argv
#Importing the necessary modules.
from ciscoconfparse import CiscoConfParse
import sys
import argparse
def check_VTY_ACL_NET1637():
## Search for LINE VTY access-list name then check if that ACL has log keyword
#
for VTY_ACL in parse.find_children_w_parents('line vty', 'access-class'):
#print(VTY_ACL[14])
VTY_ACL = VTY_ACL.lstrip('access-class ')
#print (VTY_ACL)
VTY_ACL_2 = VTY_ACL.rstrip(' in')
#print(VTY_ACL_R)
#has_ACL_in = VTY_ACL.find_lines(r'access-class')
#print(has_ACL_in)
#for IP_ACL_LIST in parse.find_objects_w_child(VTY_ACL_R, 'log'):
#for IP_ACL_LIST in parse.find_lines(VTY_ACL_R):
for IP_ACL_LIST in parse.find_parents_w_child(VTY_ACL_2, ''):
#print(IP_ACL_LIST)
#IP_ACL_ACE = parse.has_line_with(' log')
IP_ACL_ACE = parse.find_children_w_parents(IP_ACL_LIST, '')
#print(IP_ACL_ACE)
has_log_keyword = parse.has_line_with(r' log')
#print(has_log_keyword)
#
#has_log_keyword = has_log_keyword.split()
for log in IP_ACL_ACE:
#print (log)
#has_not_log_keyword = parse.has_line_with(r'. log')
#print(has_not_log_keyword)
keyword_log = 'log'
keyword_permit = 'permit'
keyword_deny = 'deny'
log = log.split()
print (log)
if (not keyword_log):
print('OPEN LINE VTY')
else:
print("Not a Finding: 'NET-VLAN-023'" )
# Main starting of script
def start():
script, input_file = argv
global parse
parse = CiscoConfParse (input_file)
print("Opening config file: %r\n" % input_file)
check_VTY_ACL_NET1637()
def main():
args = sys.argv[1:]
if len(args) == 1:
start()
#else:
#usage()
if __name__ == "__main__":
main()
这是我在 VTY 上与 ACL 一起使用的示例配置文件
Current configuration : 25432 bytes
!
ip access-list extended SSH2-IN
remark ///\\\///\\\///\\\///\\\///\\\///\\\///
remark ///\\\***DEC 8 2015***///\\\
remark SomeSite //VoSIP //
remark ******************************************
permit ip 10.227.2.128 0.0.0.63 any
permit tcp 43.81.133.0 0.0.0.255 any eq 22 log
deny ip any any
!
line vty 0 4
access-class SSH2-IN in
line vty 5 15
access-class SSH2-IN in
!
end