想要检查 Cisco IOS 配置,如果 VTY 仅具有 ciscoconfparse2 的 SSH

问题描述 投票:0回答:1

这是我在这里发表的第一篇文章!
我正在尝试验证 IOS 配置是否仅具有 SSH 的所有 VTY。
我使用带有 ciscoconfparse2 库的 Python,并且创建了这个函数(在这个阶段我没有使用所有参数):

仅供参考,参数具有以下信息:

in_test = (a list with several  lines: line vty 0 4, line vty 5 15 and line vty 16 31 
in_rexp = line vty 0 4 or line vty 5 15 or line vty 16 31
in_defa = transport input ssh
def func_parent(in_parse, in_test, in_rexp, in_defa, in_neg) -\> bool:
    '''
    Checks if the VTYs have SSH only.
    '''

    object_name = [
        obj for obj in in_parse.find_parent_objects(in_rexp, in_defa)]
    print(object_name)
    if not object_name:
        print(
            Fore.RED + f'{in_test} has not SSH only ----------> 9')
        return False
    else:
        print(
            Fore.GREEN + f'{in_test} has SSH only ----------> 10')
        return True

所以,如果列表为空,则意味着没有 ssh...
现在,对于以下配置...

line vty 0 4
 exec-timeout 0 0
 logging synchronous
 length 0
 transport input ssh
line vty 5 15
 exec-timeout 0 0
 logging synchronous
 length 0
transport input ssh
 line vty 16 31
 length 0
 transport input ssh
!

我收到了这个...

\[\<IOSCfgLine # 1972 'line vty 0 4'\>\]
line vty 0 4 has SSH only ----------\> 10      \>\>\> OK

\[\<IOSCfgLine # 1977 'line vty 5 15'\>\]
line vty 5 15 has SSH only ----------\> 10     \>\>\> OK

\[\]
line vty 16 31 has not SSH only ----------\> 9  \>\>\> WRONG

VTY 16 31 是错误的.....

对于这个配置..

line vty 0 4
 exec-timeout 0 0
 logging synchronous
 transport input ssh telnet
 length 0
line vty 5 15
 exec-timeout 0 0
 transport input telnet
 length 0
line vty 16 31
 exec-timeout 10 0
 transport input ssh telnet
 length 0
!

...这个结果...

\[\<IOSCfgLine # 14395 'line vty 0 4'\>\]
line vty 0 4 has SSH only ----------\> 10         \>\>\> WRONG

\[\]
line vty 5 15 has not SSH only ----------\> 9     \>\>\> OK

\[\]
line vty 16 31 has not SSH only ----------\> 9     \>\>\> OK

我猜测匹配条件不适用于 ssh 和 telnet 组合。

我尝试使用 find_parent_objects 和 find_child_objects 得到相同的结果。
也许这不是仅在配置中检查 SSH 的最佳方法,感谢您的帮助!

蒂亚

python cisco
1个回答
0
投票

我想我发现了问题。 正在运行的配置在子行的开头有一个空格,因此我必须使用的正则表达式应该考虑到这一点。 由于我遵循 ciscoparseconf2 文档中的示例,因此它们在函数 find_parent_objects 和 find_child_objectts 的查询中不包含开头的空格。当我像这样重新编写正则表达式 ^\s+transport\s+input\s+ssh$ 时,它表示返回我所期望的内容。

© www.soinside.com 2019 - 2024. All rights reserved.