如何解决用于将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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。