perl解析配置括号样式

时间:2013-12-12 08:16:46

标签: perl csv

我想知道perl是否可以解析junos的大括号配置并转换为csv。

interfaces {
  apply-groups policer-arp;
    ge-1/0/0 {
        description "*** Description ***";
        unit 0 {
            family inet {
                address 10.100.200.1/24 {
                    arp 10.100.200.2 mac 00:16:d4:e7:9b:de;
                }
            }
        }
    }
    ge-1/0/2 {
        description " Description ";
        vlan-tagging;
        unit 101 {
            description "NODEB_CLUSTER#101";
            vlan-id 101;
            family inet {
                address 10.187.132.3/27 {
                    vrrp-group 1 {
                        virtual-address 10.187.132.1;
                        priority 190;
                        accept-data;
                    }
                }
            }
        }
        unit 102 {
            description "Description";
            vlan-id 102;
            family inet {
                address 10.187.132.35/27 {
                    vrrp-group 2 {
                        virtual-address 10.187.132.33;
                        priority 200;
                        accept-data;
                    }
                }
            }
        }
        unit 103 {
            description "NODEB_CLUSTER#103";
            vlan-id 103;
            family inet {
                address 10.187.132.67/27 {
                    vrrp-group 3 {
                        virtual-address 10.187.132.65;
                        priority 190;
                        accept-data;
                    }
                }
            }
        }
        unit 104 {
            description "NODEB_CLUSTER#104";
            vlan-id 104;
            family inet {
                address 10.187.132.99/27 {
                    vrrp-group 4 {
                        virtual-address 10.187.132.97;
                        priority 200;
                        accept-data;
                    }
                }
            }
        }

输出:

Interface, IP Address, VLAN, Virtual-Address, Vrrp-group, Vrrp Priority
ge-1/0/0,10.100.200.1/24, , , , ,               
ge-1/0/2, , , , , ,
ge-1/0/2.101,10.187.132.3/27,101,10.187.132.1,1,190
ge-1/0/2,102,10.187.132.35/27,102,10.187.132.33,2,200
ge-1/0/2,103,10.187.132.67/27,103,10.187.132.65,3,190
ge-1/0/2,104,10.187.132.99/27,104,10.187.132.97,4,200

所以简而言之,我们将junos配置转换为csv,对于接口配置,我们将接口ge-1/0/2计算为ge-1/0 / 2.101,因为它是vlan 101的接口,ge-1/0 / 2.102对于vlan 102,依此类推。 perl可以解析它还是有更快的方法来转换它?

1 个答案:

答案 0 :(得分:2)

如果你知道这种配置的语法,那应该不是很困难。结合使用regexp(使用 g c 修饰符和 \ G 断言)和pos()函数。好的例子是JSON::Tiny

我为此格式添加了解析器示例(但不知道它是否正确,因为主题启动器无法提供任何规范):https://gist.github.com/olegwtf/7941326