python - 使用 CiscoConfParse 查找应用于 VTY 的 ACL,然后检查 ACL 的日志语句

标签 python ciscoconfparse

我有代码可以在“line vty”上查找“access-class”的名称

之后我能够找到 ACL,但随后我想检查 ACL 的每一行以验证“拒绝”和“允许”语句是否具有“log”关键字。如果条目上没有“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

最佳答案

from ciscoconfparse import CiscoConfParse

input_file = 'some_site.conf'
parse = CiscoConfParse(input_file)

## Build a list of vty ACLs here (and flag if a vty doesn't have one)
vty_acl_names = set([])
for vtyobj in parse.find_objects(r'^line\svty'):

    vty_acl_name = vtyobj.re_match_iter_typed('access-class\s+(\S+)\s+in', 
        result_type=str, default="")

    if not vty_acl_name:
        print "FAIL: '{0}' doesn't have an ACL".format(vtyobj.text)
    else:
        vty_acl_names.add(vty_acl_name)

## Check ACL log compliance here (and ensure the ACL is even in the config)
for vty_acl_name in vty_acl_names:
    try:
        aclobj = parse.find_objects(r'ip\s+access-list\s+extended\s+{0}'.format(vty_acl_name))[0]
    except IndexError:
        print "FAIL: ACL {0} is applied to a vty, but it's missing from the config!".format(vty_acl_name)
    ## NOTE: this only works if you are using extended ACLs on the VTY
    for ace in aclobj.children:

        if 'remark' in ace.text:
            continue
        if 'log' in ace.text:
            print "NOT A FINDING - ACL {0}, ACE: {1}".format(vty_acl_name, ace.text)
        else:
            print "OPEN (no log) - ACL {0}, ACE: {1}".format(vty_acl_name, ace.text)

当我在您的配置上运行它时,我得到以下输出:

(py27_default) mpenning@MPENNING-BRIX C:\Users\mpenning
> python garrybaker.py
FAIL: 'line vty 5 15' doesn't have an ACL
OPEN (no log) - ACL SSH2-IN, ACE:  permit ip 10.227.2.128 0.0.0.63 any
NOT A FINDING - ACL SSH2-IN, ACE:  permit tcp 43.81.133.0 0.0.0.255 any eq 22 log
OPEN (no log) - ACL SSH2-IN, ACE:  deny   ip any any

(py27_default) mpenning@MPENNING-BRIX C:\Users\mpenning
>

我冒昧地添加了您的要求...我标记了没有 ACL 的 vty 线路。

现在,您可能会问“为什么它说‘line vty 5 15’没有 ACL,因为我把它放在配置中?这是因为我使用了 re_match_iter_typed() ,它只搜索父级的子级行...当您输入这样的配置时,CiscoConfParse 不会将访问类与 5 15 上的父 vty 行关联起来,因为访问类行缩进不超过 vty 5 15 行:

line vty 0 4
 access-class SSH2-IN in
line vty 5 15
access-class SSH2-IN in
!

缩进对 CiscoConfParse 很重要......你必须知道你的输入......如果你不能依赖人们缩进他们的配置,那么使用 Kirk 的答案中的方法:-)

关于python - 使用 CiscoConfParse 查找应用于 VTY 的 ACL,然后检查 ACL 的日志语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37565117/

相关文章:

python - scipy 的 stats.rv_discrete 模块中 'sum pk not equal to 1' 错误的解决方法

python - ttk进度条卡住

python - 在 GAE 沙箱中运行 Python 的 nose 单元测试

python - 多重处理 python 方法

python - 如何以内存有效的方式在Python中将矩阵一分为二?

python - 需要帮助解析 Cisco 输出

python - 运行 python 脚本解析 IOS 配置文件时出现错误消息

python - Python 3.4 模块中的 ciscoconfparse 未正确导入