V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Gothack
V2EX  ›  Python

python 中有没有能解析配置文件的库

  •  
  •   Gothack · 2017-01-08 20:12:19 +08:00 · 2690 次点击
    这是一个创建于 2658 天前的主题,其中的信息可能已经有所发展或是发生改变。

    是这样,楼主经常用 jinja2 去根据模板生成网络设备的配置文件,再由其他人员将配置刷到设备上,这样做批量化上线时很方便。 然而对于在线的设备,如何快速校验配置的正确性一直没有好的方法。这过程类似于一个模板的逆向过程,不知道有没有人做过类似的东西?

    14 条回复    2017-01-09 19:31:11 +08:00
    ansheng
        1
    ansheng  
       2017-01-08 20:18:04 +08:00
    yaml 可以吗?
    saber000
        2
    saber000  
       2017-01-08 20:21:01 +08:00
    pyparsing?
    loading
        3
    loading  
       2017-01-08 20:29:26 +08:00 via Android
    正则
    Delbert
        4
    Delbert  
       2017-01-08 20:36:18 +08:00 via Android
    你的配置文件是什么格式的啊, python 一般都有轮子。
    Gothack
        5
    Gothack  
    OP
       2017-01-08 20:41:31 +08:00
    @ansheng @Delbert
    cisco 之类的设备,不是通用的配置文件类型,网上找了段,类似这种
    ```interface Ethernet0
    nameif test
    security-level 10
    ip address 10.10.88.50 255.255.255.254
    !
    interface Ethernet1
    nameif inside
    security-level 100
    ip address 10.86.194.176 255.255.254.0
    !
    interface Ethernet2
    shutdown
    no nameif
    security-level 0
    no ip address
    !
    interface Ethernet3
    shutdown
    no nameif
    security-level 0
    no ip address
    !
    ```
    Gothack
        6
    Gothack  
    OP
       2017-01-08 20:43:38 +08:00
    @loading 正则是个出路,不过似乎造这个轮子成本太高,主要是由于配置文件量大以及不同品牌设备差异大
    Gothack
        7
    Gothack  
    OP
       2017-01-08 20:46:22 +08:00
    @saber000 没用过这个,我看下,谢谢
    cortexm3
        8
    cortexm3  
       2017-01-08 20:47:13 +08:00
    搞个正则表达式就够了吧。
    rogerchen
        9
    rogerchen  
       2017-01-08 21:10:22 +08:00 via iPad
    视配置复杂程度

    拿正则勉强撸一套
    手写 parser
    手写 ebnf ,用 antlr 生成 python 的 parser
    forrestchang
        10
    forrestchang  
       2017-01-08 22:15:49 +08:00
    递归下降解析器
    qdcanyun
        11
    qdcanyun  
       2017-01-08 22:40:15 +08:00
    pc10201
        12
    pc10201  
       2017-01-08 23:55:05 +08:00
    ansible 现在都可以直接管理 cisco 设备~
    xiamx
        13
    xiamx  
       2017-01-09 00:05:42 +08:00
    看一看配置文件的格式是正则语言还是上下文无关语言。如果是正则语言,基本上可以用 regex 匹配。如果是上下文无关语言,就只能自写 parser 了
    Gothack
        14
    Gothack  
    OP
       2017-01-09 19:31:11 +08:00
    @qdcanyun
    多谢推荐。这个用过些,然而设备厂商属性太强,其他设备不方便,所以倾向于找个通用性的
    @pc10201
    同上。。。
    @cortexm3
    @rogerchen
    正则的话复杂度略高...antlr 的话之前没用过,回头研究下,谢谢
    @forrestchang
    解析器的话会不会太高端了些,怕是我这个小运维搞不定啊。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2977 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 03:17 · PVG 11:17 · LAX 20:17 · JFK 23:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.