想用ciscoconfparse2检查Cisco IOS配置中VTYs是否仅支持SSH
这是我在这里的第一篇帖子!!
我正在尝试验证一个IOS配置,看看它的所有VTY(虚拟终端)是否只使用SSH。
我使用Python和ciscoconfparse2库,并创建了这个函数(目前我还没有使用所有的参数):
为了让大家更明白,参数包含以下信息:
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的最佳方法,感谢你的帮助!
提前谢谢!
1 个回答
0
我想我找到了问题所在。
运行配置的子行开头有一个空格,所以我需要使用的正则表达式(regexp)得考虑到这一点。
因为我在参考ciscoparseconf2文档中的例子,它们在调用find_parent_objects和find_child_objects这两个函数时,并没有把开头的空格算进去。
一旦我把正则表达式改成这样:^\s+transport\s+input\s+ssh$,它就开始返回我期待的结果了。