script.rb 958 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. require 'psych'
  2. require 'ipaddr'
  3. prefixes = []
  4. exl = ["schema.json"]
  5. Dir['/var/lib/bird/crxn/entitydb/*.json'].each do |entity_file|
  6. if ! exl.include? File.basename(entity_file)
  7. entity_json = File.read entity_file
  8. entity = Psych.safe_load entity_json
  9. entity_prefixes = entity['route']
  10. final_prefixes = []
  11. # avoid injection of code
  12. entity_prefixes.each_pair do |prefix, value|
  13. ip = IPAddr.new prefix
  14. maxlen = "64";
  15. if value['max-len']
  16. maxlen = value['max-len'].to_i.to_s
  17. end
  18. if ip.ipv6? && ip.private?
  19. final_prefixes << "#{ip}/#{ip.prefix}{#{ip.prefix},#{maxlen}}"
  20. else
  21. puts "Found illeagl ip #{ip.inspect}"
  22. end
  23. end
  24. prefixes.push(*final_prefixes)
  25. end
  26. rescue
  27. puts "Generic error: #{$!}"
  28. end
  29. conf_file = File.new '/var/lib/bird/crxn/crxn_ips.conf', 'w'
  30. conf_file.puts 'define CRXN_IPs = ['
  31. conf_file.puts "\t#{prefixes.join(",\n\t")}"
  32. conf_file.puts '];'
  33. conf_file.close