rrlookup.pl
上传用户:xiaozhuqw
上传日期:2009-11-15
资源大小:1338k
文件大小:3k
- #! /usr/local/bin/perl
- ##
- ## Read BGPd logfile and lookup RR's whois database.
- ##
- ## Copyright (c) 1997 Kunihiro Ishiguro
- ##
- use Socket;
- ## Configuration variables
- $whois_host = "whois.jpix.ad.jp";
- #$mail_address = "toshio@iri.co.jp";
- $mail_address = "kunihiro@zebra.org";
- $mailer = "/usr/sbin/sendmail -oi";
- #$logfile = "/usr/local/sbin/logfile"
- $logfile = "logfile";
- $lookuplog = "lookuplog";
- ## mail routine
- {
- local ($prefix, $origin);
- open (LOG, $logfile) || die "can't open $logfile";
- open (LOOKUP, ">$lookuplog") || die "can't open $lookuplog";
-
- for (;;) {
- while (<LOG>) {
- if (/UpdateS+ ([d./]+) .* (d+) [ie?]/) {
- $prefix = $1;
- $origin = $2;
- $ret = &whois_check ($prefix, $origin);
- if ($ret) {
- print LOOKUP "$prefix AS$origin : Check OKn";
- } else {
- print LOOKUP "$prefix AS$origin : Errorn";
- }
- # fflush (LOOKUP);
- }
- }
- sleep (3);
- }
- }
- sub whois_check
- {
- local ($prefix, $origin) = @_;
- local ($rr_prefix, $rr_origin) = ();
- local (@result);
- $origin = "AS" . $origin;
- # print "$prefix $originn";
- @result = &whois ($prefix);
- foreach (@result) {
- if (/^route:.*s([d./]+)$/) {
- $rr_prefix = $1;
- }
- if (/^origin:.*s(AS[d]+)$/) {
- $rr_origin = $1;
- if ($prefix eq $rr_prefix and $origin eq $rr_origin) {
- return 1;
- }
- }
- }
- alarm_mail ($prefix, $origin, @result);
- return 0;
- }
- ## get port of whois
- sub get_whois_port
- {
- local ($name, $aliases, $port, $proto) = getservbyname ("whois", "tcp");
- return ($port, $proto);
- }
- ## whois lookup
- sub whois
- {
- local ($query) = @_;
- local ($port, $proto) = &get_whois_port;
- local (@result);
- if ($whois_host=~ /^s*d+.d+.d+.d+s*$/) {
- $address = pack ("C4",split(/./,$host));
- } else {
- $address = (gethostbyname ($whois_host))[4];
- }
- socket (SOCKET, PF_INET, SOCK_STREAM, $proto);
- if (connect (SOCKET, sockaddr_in ($port, $address))) {
- local ($oldhandle) = select (SOCKET);
- $| = 1;
- select($oldhandle);
- print SOCKET "$queryrn";
- @result = <SOCKET>;
- return @result;
- }
- }
- ##
- sub alarm_mail
- {
- local ($prefix, $origin, @result) = @_;
- open (MAIL, "|$mailer -t $mail_address") || die "can't open $mailer";
-
- print MAIL "From: root@rr1.jpix.ad.jpn";
- print MAIL "Subject: RR $origin $prefixn";
- print MAIL "MIME-Version: 1.0n";
- print MAIL "Content-Type: text/plain; charset=us-ascii nn";
- print MAIL "RR Lookup Error Reportn";
- print MAIL "======================n";
- print MAIL "Announced route : $prefix from $originnn";
- print MAIL "@result";
- close MAIL;
- }