rrcheck.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env perl
  2. ##
  3. ## Read BGPd logfile and lookup RR's whois database.
  4. ##
  5. ## Copyright (c) 1997 Kunihiro Ishiguro
  6. ##
  7. use Socket;
  8. ## Configuration variables
  9. $whois_host = "whois.jpix.ad.jp";
  10. #$logfile = "/usr/local/sbin/logfile"
  11. $logfile = shift || die "Please specify filename";
  12. ## mail routine
  13. {
  14. local ($prefix, $origin);
  15. open (LOG, $logfile) || die "can't open $logfile";
  16. $index = '';
  17. while ($index) {
  18. $index = <LOG>;
  19. if ($index =~ /[bgpd]/) {
  20. break;
  21. }
  22. }
  23. while (<LOG>) {
  24. if (/([\d\.\/]+)\s+([\d\.]+)\s+(\d+)\s+(\d+)\s+([\d ]+)\s+[ie\?]/) {
  25. $prefix = $1;
  26. $nexthop = $2;
  27. $med = $3;
  28. $dummy = $4;
  29. $aspath = $5;
  30. ($origin) = ($aspath =~ /([\d]+)$/);
  31. print "$nexthop [$origin] $prefix $aspath ";
  32. $ret = &whois_check ($prefix, $origin);
  33. if ($ret == 0) {
  34. print "Check OK\n";
  35. } elsif ($ret == 1){
  36. print "AS orgin mismatch\n";
  37. } else {
  38. print "prefix doesn't exist \n";
  39. }
  40. }
  41. }
  42. }
  43. sub whois_check
  44. {
  45. local ($prefix, $origin) = @_;
  46. local ($rr_prefix, $rr_origin) = ();
  47. local (@result);
  48. $origin = "AS" . $origin;
  49. @result = &whois ($prefix);
  50. $prefix_match = 0;
  51. foreach (@result) {
  52. if (/^route:.*\s([\d\.\/]+)$/) {
  53. $rr_prefix = $1;
  54. }
  55. if (/^origin:.*\s(AS[\d]+)$/) {
  56. $rr_origin = $1;
  57. if ($prefix eq $rr_prefix and $origin eq $rr_origin) {
  58. return 0;
  59. } elsif ($prefix eq $rr_prefix) {
  60. $prefix_match = 1;
  61. }
  62. }
  63. }
  64. # alarm_mail ($prefix, $origin, @result);
  65. if ($prefix_match) {
  66. return 1;
  67. } else {
  68. return 2;
  69. }
  70. }
  71. ## get port of whois
  72. sub get_whois_port
  73. {
  74. local ($name, $aliases, $port, $proto) = getservbyname ("whois", "tcp");
  75. return ($port, $proto);
  76. }
  77. ## whois lookup
  78. sub whois
  79. {
  80. local ($query) = @_;
  81. local ($port, $proto) = &get_whois_port;
  82. local (@result);
  83. if ($whois_host=~ /^\s*\d+\.\d+\.\d+\.\d+\s*$/) {
  84. $address = pack ("C4",split(/\./,$host));
  85. } else {
  86. $address = (gethostbyname ($whois_host))[4];
  87. }
  88. socket (SOCKET, PF_INET, SOCK_STREAM, $proto);
  89. if (connect (SOCKET, sockaddr_in ($port, $address))) {
  90. local ($oldhandle) = select (SOCKET);
  91. $| = 1;
  92. select($oldhandle);
  93. print SOCKET "$query\r\n";
  94. @result = <SOCKET>;
  95. return @result;
  96. }
  97. }
  98. ##
  99. sub alarm_mail
  100. {
  101. local ($prefix, $origin, @result) = @_;
  102. open (MAIL, "|$mailer -t $mail_address") || die "can't open $mailer";
  103. print MAIL "From: root\@rr1.jpix.ad.jp\n";
  104. print MAIL "Subject: RR $origin $prefix\n";
  105. print MAIL "MIME-Version: 1.0\n";
  106. print MAIL "Content-Type: text/plain; charset=us-ascii \n\n";
  107. print MAIL "RR Lookup Error Report\n";
  108. print MAIL "======================\n";
  109. print MAIL "Announced route : $prefix from $origin\n\n";
  110. print MAIL "@result";
  111. close MAIL;
  112. }