|
@@ -0,0 +1,141 @@
|
|
|
+#! /usr/bin/perl
|
|
|
+
|
|
|
+use strict;
|
|
|
+
|
|
|
+my $baseoid = ".1.3.6.1.2.1.31.1.1.1.18";
|
|
|
+
|
|
|
+my $cache_secs = 60;
|
|
|
+
|
|
|
+# ----------------------------------
|
|
|
+
|
|
|
+my $mib;
|
|
|
+my $mibtime;
|
|
|
+
|
|
|
+$| = 1;
|
|
|
+
|
|
|
+while (my $cmd = <STDIN>) {
|
|
|
+ chomp $cmd;
|
|
|
+ if ($cmd eq "PING") {
|
|
|
+ print "PONG\n";
|
|
|
+ } elsif ($cmd eq "get") {
|
|
|
+ my $oid_in = <STDIN>;
|
|
|
+ chomp($oid_in);
|
|
|
+ my $oid = get_oid($oid_in);
|
|
|
+ my $mib = create_alias_mib();
|
|
|
+
|
|
|
+ if ($oid != 0 && defined{$mib->{$oid}}) {
|
|
|
+ print "$baseoid.$oid\n";
|
|
|
+ print $mib->{$oid}[0]."\n";
|
|
|
+ print $mib->{$oid}[1]."\n";
|
|
|
+ } else {
|
|
|
+ print "NONE\n";
|
|
|
+ }
|
|
|
+
|
|
|
+ } elsif ($cmd eq "getnext") {
|
|
|
+ my $oid_in = <STDIN>;
|
|
|
+
|
|
|
+ my $oid = get_oid($oid_in);
|
|
|
+ my $found = 0;
|
|
|
+
|
|
|
+ my $mib = create_alias_mib();
|
|
|
+ my @s = sort { oidcmp($a, $b) } keys %{ $mib };
|
|
|
+ for (my $i = 0; $i < @s; $i++) {
|
|
|
+ if (oidcmp($oid, $s[$i]) == -1) {
|
|
|
+ print "$baseoid.".$s[$i]."\n";
|
|
|
+ print $mib->{$s[$i]}[0]."\n";
|
|
|
+ print $mib->{$s[$i]}[1]."\n";
|
|
|
+ $found = 1;
|
|
|
+ last;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!$found) {
|
|
|
+ print "NONE\n";
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ # Unknown command
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+exit 0;
|
|
|
+
|
|
|
+
|
|
|
+sub get_oid
|
|
|
+{
|
|
|
+
|
|
|
+ my ($oid) = @_;
|
|
|
+ chomp $oid;
|
|
|
+
|
|
|
+ my $base = $baseoid;
|
|
|
+ $base =~ s/\./\\./g;
|
|
|
+
|
|
|
+ if ($oid !~ /^$base(\.|$)/) {
|
|
|
+ # Requested oid doesn't match base oid
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ $oid =~ s/^$base\.?//;
|
|
|
+ return $oid;
|
|
|
+}
|
|
|
+
|
|
|
+sub oidcmp {
|
|
|
+ my ($x, $y) = @_;
|
|
|
+
|
|
|
+ my @a = split /\./, $x;
|
|
|
+ my @b = split /\./, $y;
|
|
|
+
|
|
|
+ my $i = 0;
|
|
|
+
|
|
|
+ while (1) {
|
|
|
+
|
|
|
+ if ($i > $#a) {
|
|
|
+ if ($i > $#b) {
|
|
|
+ return 0;
|
|
|
+ } else {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ } elsif ($i > $#b) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($a[$i] < $b[$i]) {
|
|
|
+ return -1;
|
|
|
+ } elsif ($a[$i] > $b[$i]) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ $i++;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+sub create_alias_mib
|
|
|
+{
|
|
|
+ # We cache the results for $cache_secs seconds
|
|
|
+ if (time - $mibtime < $cache_secs) {
|
|
|
+ return $mib;
|
|
|
+ }
|
|
|
+
|
|
|
+ my %name;
|
|
|
+ my %alias;
|
|
|
+
|
|
|
+ # retrieve the interface alias & number
|
|
|
+ open Q, "/usr/local/sbin/birdc sh interfaces |";
|
|
|
+ while(my $l = <Q>) {
|
|
|
+ chomp($l);
|
|
|
+ if ($l =~ /^([^[:space:]]+)[[:space:]]([^[:space:]]+)[[:space:]]\(index=(\d+)\).*/) {
|
|
|
+ $name{$1} = [ $3 ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ close Q;
|
|
|
+
|
|
|
+ open Q, "( if [ -e /etc/network/interfaces ]; then cat /etc/network/interfaces; elif [ -e /etc/rc.conf ]; then cat /etc/rc.conf; fi) |";
|
|
|
+ while(my $l = <Q>) {
|
|
|
+ chomp($l);
|
|
|
+ if ($l =~ /^# ([^:]*): (.*)$/) {
|
|
|
+ $alias{$name{$1}[0]} = [ "string", $2 ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ close Q;
|
|
|
+ $mib = \%alias;
|
|
|
+ $mibtime = time;
|
|
|
+ return $mib;
|
|
|
+}
|