peers2bgpConfig.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use XML::Twig;
  4. use POSIX qw(strftime);
  5. if (scalar(@ARGV) lt 2) {
  6. print STDERR "usage: peers2bgpConfig.pl <xml-source> <ix> <as-number>\n";
  7. exit -1;
  8. }
  9. my ($xml_source, $target_ix, $target_asn) = @ARGV;
  10. my $sessions = {};
  11. sub regSession {
  12. my ($t, $peer) = @_;
  13. my $ix = $peer->{'att'}->{'ix'};
  14. return unless ($ix eq $target_ix);
  15. my ($name, $ip, $as, $contact, $max_prefixes, $view,
  16. $as_set, $pfx_in, $pfx_out, $map_in, $map_out) = (
  17. $peer->first_child('name')->text,
  18. $peer->first_child('ip')->text,
  19. $peer->first_child('as')->text,
  20. $peer->first_child('contact'),
  21. $peer->first_child('max-prefixes'),
  22. $peer->first_child('view'),
  23. $peer->first_child('as-set'),
  24. $peer->first_child('prefix-in'),
  25. $peer->first_child('prefix-out'),
  26. $peer->first_child('map-in'),
  27. $peer->first_child('map-out')
  28. );
  29. if (defined $contact) {
  30. $contact = $contact->text;
  31. } else {
  32. $contact = "nomail";
  33. }
  34. if (defined $target_asn) {
  35. if ($target_asn != /^$/) {
  36. return unless ($target_asn eq $as);
  37. }
  38. }
  39. if ($target_ix =~ /(panap|sfinx)/i) {
  40. my $pp = $target_ix;
  41. $pp =~ tr/a-z/A-Z/;
  42. printf STDOUT "neighbor %s remote-as %s\n", $ip, $as;
  43. printf STDOUT "neighbor %s description %s %s\n", $ip, $name, $contact;
  44. printf STDOUT "neighbor %s shutdown\n", $ip;
  45. if ($max_prefixes) {
  46. printf STDOUT "neighbor %s maximum-prefix %s\n",
  47. $ip, $max_prefixes->text;
  48. }
  49. printf STDOUT "neighbor %s peer-group %s\n", $ip, $pp;
  50. if ($pfx_in) {
  51. printf STDOUT "neighbor %s prefix-list %s in\n", $ip, $pfx_in->text;
  52. }
  53. if ($pfx_out) {
  54. printf STDOUT "neighbor %s prefix-list %s out\n", $ip, $pfx_out->text;
  55. }
  56. if ($map_in) {
  57. printf STDOUT "neighbor %s route-map %s in\n", $ip, $map_in->text;
  58. }
  59. if ($map_out) {
  60. printf STDOUT "neighbor %s route-map %s out\n", $ip, $map_out->text;
  61. }
  62. printf STDOUT "no neighbor %s shutdown\n", $ip;
  63. } else {
  64. printf STDOUT "neighbor %s remote-as %s\n", $ip, $as;
  65. printf STDOUT "neighbor %s description %s %s\n", $ip, $name, $contact;
  66. printf STDOUT "neighbor %s shutdown\n", $ip;
  67. if ($ip =~ /:/o) {
  68. printf STDOUT "no neighbor %s activate\n", $ip;
  69. printf STDOUT "address-family ipv6\n";
  70. printf STDOUT "neighbor %s maximum-prefix %s\n",
  71. $ip, $max_prefixes ? $max_prefixes->text : 10;
  72. printf STDOUT "neighbor %s prefix-list %s in\n",
  73. $ip, $pfx_in ? $pfx_in->text : "peer-ip6-in";
  74. if (defined($pfx_out)) {
  75. printf STDOUT "neighbor %s prefix-list %s out\n", $ip, $pfx_out->text;
  76. }
  77. } else {
  78. printf STDOUT "neighbor %s maximum-prefix %s\n",
  79. $ip, $max_prefixes ? $max_prefixes->text : 100;
  80. printf STDOUT "neighbor %s prefix-list %s in\n",
  81. $ip, $pfx_in ? $pfx_in->text : "pfx-all-but-gitoyen";
  82. printf STDOUT "neighbor %s prefix-list %s out\n",
  83. $ip, $pfx_out ? $pfx_out->text : "pfx-all";
  84. }
  85. printf STDOUT "neighbor %s route-map %s in\n",
  86. $ip, $map_in ? $map_in->text : "$ix-in";
  87. printf STDOUT "neighbor %s route-map %s out\n",
  88. $ip, $map_out ? $map_out->text : "$ix-out";
  89. printf STDOUT "neighbor %s soft-reconfiguration inbound\n", $ip;
  90. if ($ip =~ /:/o) {
  91. printf STDOUT "exit-address-family\n";
  92. }
  93. printf STDOUT "no neighbor %s shutdown\n", $ip;
  94. }
  95. }
  96. my $t = XML::Twig->new( twig_handlers => { 'peer' => \&regSession } );
  97. $t->parsefile($xml_source);
  98. exit 0;