convert_bird_to_yaml.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. require 'yaml'
  2. # 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]}
  3. class ParseError < StandardError; end
  4. class NoProtocolConfigurationFound < ParseError; end
  5. class NoProtocolsConfigured < ParseError; end
  6. def parse cnt
  7. 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]}/)
  8. peers = []
  9. raise NoProtocolConfigurationFound if proto_conf.length == 0
  10. proto_conf.each do |peer_old|
  11. peer = {}
  12. peer["name"] = peer_old[0]
  13. peer["template"] = peer_old[1]
  14. peer["neighbor"] = peer_old[2]
  15. peer["neighbor_asn"] = peer_old[3].to_i
  16. 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]+};/)
  17. raise NoProtocolsConfigured if ip_conf.length == 0
  18. raise ParseError, "Too much protocols configured" if ip_conf.length > 2
  19. peer["protocols"] = []
  20. ip_conf.each do |conf|
  21. peer["protocols"] << conf[0]
  22. end
  23. peer["bgp_communities"] = {}
  24. peer["bgp_communities"]["latency"] = ip_conf[0][1].to_i
  25. peer["bgp_communities"]["bandwidth"] = ip_conf[0][2].to_i
  26. peer["bgp_communities"]["encryption"] = ip_conf[0][3].to_i
  27. peer["enabled"] = true
  28. peers << peer
  29. end
  30. return peers
  31. end
  32. peers = []
  33. Dir["peers/*.conf"].each do |conf_file|
  34. begin
  35. parsed_peers = parse(File.read(conf_file))
  36. parsed_peers.each do |peer|
  37. peers << peer
  38. end
  39. rescue ParseError => e
  40. puts "Parse error at file #{conf_file}: #{e.class}!"
  41. end
  42. end
  43. puts peers.to_yaml