用于将Junos型树配置数据“导出”到CSV型表数据并返回的脚本

问题描述

| 我想使用一些脚本将类似Junos的配置数据导出到“ \\\\\\\\\\\\\\\\\\\\类似于CSV数据\”数据(由制表符分隔)中并返回。样本(和足够的AFAIK)输入可能如下:
firewall {
    filter protect {
        term \"protocol bgp\" {
            from {
                prefix-list {
                    bgp-peers;
                }
                protocol tcp; ## Any ideas how to preserve comments in output?
                port bgp;
            }
            then accept;
        }
        term protocol_ntp {
            from {
                prefix-list {
                    \"protocol ntp\";
                }
                protocol udp;
                port ntp;
            }
            then accept;
        }
    }
}
预期的输出将如下所示(\“ | \”栏表示期望使用制表符的位置):
firewall|filter protect|term \"protocol bgp\"|from|prefix-list|bgp-peers;
firewall|filter protect|term \"protocol bgp\"|from|protocol tcp;
firewall|filter protect|term \"protocol bgp\"|from|port bgp;
firewall|filter protect|term \"protocol bgp\"|then accept;
firewall|filter protect|term protocol_ntp|from|prefix-list|\"protocol ntp\";
firewall|filter protect|term protocol_ntp|from|protocol udp;
firewall|filter protect|term protocol_ntp|from|port ntp;
firewall|filter protect|term protocol_ntp|then accept;
乍一看,这个问题似乎很容易,但第二个原因并不是因为双引号。 该问题是恕我直言可以通过一些额外的双引号处理简化为“树到表”和“表到树”问题。老实说,我不知道该怎么做... 我更喜欢按受欢迎程度排序的下一个解决方案(对我而言):在
sed(1)
中(可能非常快),在ѭ3not中(对我而言不如前者好),在
perl(1)
中(为什么不使用仅使用纯Perl和
strict
warnings
模块来源)或在ANSI C中没有额外的库,只是基本库(我不希望有人在这里为我编写解析器,但从来没有人知道)。     

解决方法

从XML转换起来很容易,JunOS内置了对XML的支持:
show configuration | display xml
还是更好,尝试
show configuration | display set
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...