1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- require 'yaml'
- # protocol bgp ([\d\w]+) from ((?:neo|dn)peers) {[\s\n ]* neighbor (['"]?[a-f0-9:.]+(?:%[\w\d]+)?['"]?) as (\w{3,10});([a-z0-9\n\s{}_,();]+)[\n\s]}
- class ParseError < StandardError; end
- class NoProtocolConfigurationFound < ParseError; end
- class NoProtocolsConfigured < ParseError; end
- def parse cnt
- proto_conf = cnt.scan(/protocol bgp ([\d\w_]+) from ((?:neo|dn)peers) {[\s\n ]* neighbor ([a-f0-9:.]+(?: ?% ?['"]?[\w\d._-]+)?['"]?) as (\w{3,10});([a-z0-9\n\s{}_,();]+)[\n\s]}/)
- peers = []
- raise NoProtocolConfigurationFound if proto_conf.length == 0
- proto_conf.each do |peer_old|
- peer = {}
- peer["name"] = peer_old[0]
- peer["template"] = peer_old[1]
- peer["neighbor"] = peer_old[2]
- peer["neighbor_asn"] = peer_old[3].to_i
- ip_conf = peer_old[4].scan(/(ipv[46]) {[\n\s]+import where dn42_import_filter\((\d),(\d{2}),(\d{2})\);[\n\s]+export where dn42_export_filter\((\d),(\d{2}),(\d{2})\);[\n\s]+};/)
- raise NoProtocolsConfigured if ip_conf.length == 0
- raise ParseError, "Too much protocols configured" if ip_conf.length > 2
- peer["protocols"] = []
- ip_conf.each do |conf|
- peer["protocols"] << conf[0]
- end
- peer["bgp_communities"] = {}
- peer["bgp_communities"]["latency"] = ip_conf[0][1].to_i
- peer["bgp_communities"]["bandwidth"] = ip_conf[0][2].to_i
- peer["bgp_communities"]["encryption"] = ip_conf[0][3].to_i
-
- peer["enabled"] = true
- peers << peer
- end
- return peers
- end
- peers = []
- Dir["peers/*.conf"].each do |conf_file|
- begin
- parsed_peers = parse(File.read(conf_file))
- parsed_peers.each do |peer|
- peers << peer
- end
- rescue ParseError => e
- puts "Parse error at file #{conf_file}: #{e.class}!"
- end
- end
- puts peers.to_yaml
|