问题描述
|
我想使用一些脚本将类似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