rrlookup.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #$mail_address = "toshio\@iri.co.jp";
  11. $mail_address = "kunihiro\@zebra.org";
  12. $mailer = "/usr/sbin/sendmail -oi";
  13. #$logfile = "/usr/local/sbin/logfile"
  14. $logfile = "logfile";
  15. $lookuplog = "lookuplog";
  16. ## mail routine
  17. {
  18. local ($prefix, $origin);
  19. open (LOG, $logfile) || die "can't open $logfile";
  20. open (LOOKUP, ">$lookuplog") || die "can't open $lookuplog";
  21. for (;;) {
  22. while (<LOG>) {
  23. if (/Update\S+ ([\d\.\/]+) .* (\d+) [ie\?]/) {
  24. $prefix = $1;
  25. $origin = $2;
  26. $ret = &whois_check ($prefix, $origin);
  27. if ($ret) {
  28. print LOOKUP "$prefix AS$origin : Check OK\n";
  29. } else {
  30. print LOOKUP "$prefix AS$origin : Error\n";
  31. }
  32. # fflush (LOOKUP);
  33. }
  34. }
  35. sleep (3);
  36. }
  37. }
  38. sub whois_check
  39. {
  40. local ($prefix, $origin) = @_;
  41. local ($rr_prefix, $rr_origin) = ();
  42. local (@result);
  43. $origin = "AS" . $origin;
  44. # print "$prefix $origin\n";
  45. @result = &whois ($prefix);
  46. foreach (@result) {
  47. if (/^route:.*\s([\d\.\/]+)$/) {
  48. $rr_prefix = $1;
  49. }
  50. if (/^origin:.*\s(AS[\d]+)$/) {
  51. $rr_origin = $1;
  52. if ($prefix eq $rr_prefix and $origin eq $rr_origin) {
  53. return 1;
  54. }
  55. }
  56. }
  57. alarm_mail ($prefix, $origin, @result);
  58. return 0;
  59. }
  60. ## get port of whois
  61. sub get_whois_port
  62. {
  63. local ($name, $aliases, $port, $proto) = getservbyname ("whois", "tcp");
  64. return ($port, $proto);
  65. }
  66. ## whois lookup
  67. sub whois
  68. {
  69. local ($query) = @_;
  70. local ($port, $proto) = &get_whois_port;
  71. local (@result);
  72. if ($whois_host=~ /^\s*\d+\.\d+\.\d+\.\d+\s*$/) {
  73. $address = pack ("C4",split(/\./,$host));
  74. } else {
  75. $address = (gethostbyname ($whois_host))[4];
  76. }
  77. socket (SOCKET, PF_INET, SOCK_STREAM, $proto);
  78. if (connect (SOCKET, sockaddr_in ($port, $address))) {
  79. local ($oldhandle) = select (SOCKET);
  80. $| = 1;
  81. select($oldhandle);
  82. print SOCKET "$query\r\n";
  83. @result = <SOCKET>;
  84. return @result;
  85. }
  86. }
  87. ##
  88. sub alarm_mail
  89. {
  90. local ($prefix, $origin, @result) = @_;
  91. open (MAIL, "|$mailer -t $mail_address") || die "can't open $mailer";
  92. print MAIL "From: root\@rr1.jpix.ad.jp\n";
  93. print MAIL "Subject: RR $origin $prefix\n";
  94. print MAIL "MIME-Version: 1.0\n";
  95. print MAIL "Content-Type: text/plain; charset=us-ascii \n\n";
  96. print MAIL "RR Lookup Error Report\n";
  97. print MAIL "======================\n";
  98. print MAIL "Announced route : $prefix from $origin\n\n";
  99. print MAIL "@result";
  100. close MAIL;
  101. }