2011-03-25 37 views
1

我想要一些脚本来将类似Junos的配置数据导出为类似“CSV”的数据由制表符分隔)并返回。将样品(和AFAIK足够)输入可以是如下:将类似Junos的(树)配置数据“导出”为类似CSV(表格)的数据并返回到脚本

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)(可能会非常快),在awk(1)(不如前一个如果不使用额外的库,只需要基础(我不希望有人愿意为我在这里编写解析器),请参阅perl(1)(为什么不使用纯Perl,仅使用strictwarnings模块)或ANSI C所有,但从未知道)。

回答

0

这是一个更容易从XML,这JUNOS有内置的支持转换:

show configuration | display xml 

或者更好的是,尽量

show configuration | display set