#!/usr/bin/perl # $Id: snmptool.pl,v 1.2 2000/09/09 18:25:16 pische Exp pische $ # Tool zum Sammeln von Schnittstellendaten per SNMP # Author: Ernst Pisch, 9.9.2000 ###################################################################### use Getopt::Long; use Net::SNMP; &GetOptions ("host=s", "community=s", "help"); $ERRFLAG = 0; if ( $opt_help == 1 ) { &usage; exit 1; } if ( !$opt_host ) { print "Option '--host=IP-Adresse' ist zwingend erforderlich\n"; $ERRFLAG = 1; } if ( !$opt_community ) { print "Option '--community=name' ist zwingend erforderlich\n"; $ERRFLAG = 1; } if ( $ERRFLAG ) { &usage; exit 1; } $hostname = $opt_host; $community = $opt_community; $\ = "\n"; #### Benötigte Werte aus der MIB $sysDescr = "1.3.6.1.2.1.1.1.0"; $sysName = "1.3.6.1.2.1.1.5.0"; $ifDescr = "1.3.6.1.2.1.2.2.1.2.1"; # erstes Interface $ifType = "1.3.6.1.2.1.2.2.1.3.1"; # erstes Interface $ifMtu = "1.3.6.1.2.1.2.2.1.4.1"; # erstes Interface $ifSpeed = "1.3.6.1.2.1.2.2.1.5.1"; # erstes Interface $ifPhysAddress = "1.3.6.1.2.1.2.2.1.6.1"; # erstes Interface $ifAdminStatus = "1.3.6.1.2.1.2.2.1.7.1"; # erstes Interface $ipAdEntIfIndex = "1.3.6.1.2.1.4.20.1.2.0.0.0.0"; # IP-Adr. 0.0.0.0 (Start für get_next_request) #### Liste der Interface-Typen %iftypes = ( 1 => "unbekannt", 2 => "regular1822", 3 => "hdh1822", 4 => "dn-x25", 5 => "rfc877-x25", 6 => "ethernet-csmacd", 7 => "iso88023-csmacd", 8 => "iso88024-tokenBus", 9 => "iso88025-tokenRing", 10 => "iso88026-man", 11 => "starLan", 12 => "proteon-10MBit", 13 => "proteon-80MBit", 14 => "hyperchannel", 15 => "fddi", 16 => "lapb", 17 => "sdlc", 18 => "t1-carrier", 19 => "cept", 20 => "basicIsdn", 21 => "primaryIsdn", 22 => "propPointToPointSerial", 23 => "ppp", 24 => "softwareLoopback", 25 => "eon", 26 => "ethernet-3Mbit", 27 => "nsip", 28 => "slip", 29 => "ultra", 30 => "ds3", 31 => "sip", 32 => "frame-Relay" ); # Liste der Interface Zustände %ifstati = ( 1 => "up", 2 => "down", 3 => "test" ); ($session, $error) = Net::SNMP->session( # -debug => 1, -translate => 1, -hostname => $hostname, -community => $community); if (!defined($session)) { print "ERROR: ", $error, "\n"; exit 1; } $response = $session->get_request($sysDescr); # Ersten Request auf Erfolg testen: if (!defined($response)) { print "ERROR: ", $session->error, "\n"; $session->close; exit 1; } $SystemBeschreibung = $response->{$sysDescr}; $response = $session->get_request($sysName); $SystemName = $response->{$sysName}; $idx = 0; $response = $session->get_request($ifDescr); $InterfaceName[$idx++] = $response->{$ifDescr}; $response = $session->get_next_request($ifDescr); @nextid = keys %$response; while ( $nextid[0] =~ m/^1\.3\.6\.1\.2\.1\.2\.2\.1\.2\./ ) { $InterfaceName[$idx++] = $response->{$nextid[0]}; $response = $session->get_next_request($nextid[0]); @nextid = keys %$response; } $idx = 0; $response = $session->get_request($ifType); $InterfaceTyp[$idx++] = $iftypes{$response->{$ifType}}; $response = $session->get_next_request($ifType); @nextid = keys %$response; while ( $nextid[0] =~ m/^1\.3\.6\.1\.2\.1\.2\.2\.1\.3\./ ) { $InterfaceTyp[$idx++] = $iftypes{$response->{$nextid[0]}}; $response = $session->get_next_request($nextid[0]); @nextid = keys %$response; } $idx = 0; $response = $session->get_request($ifMtu); $InterfaceMTU[$idx++] = $response->{$ifMtu}; $response = $session->get_next_request($ifMtu); @nextid = keys %$response; while ( $nextid[0] =~ m/^1\.3\.6\.1\.2\.1\.2\.2\.1\.4\./ ) { $InterfaceMTU[$idx++] = $response->{$nextid[0]}; $response = $session->get_next_request($nextid[0]); @nextid = keys %$response; } $idx = 0; $response = $session->get_request($ifSpeed); $InterfaceSpeed[$idx++] = $response->{$ifSpeed}; $response = $session->get_next_request($ifSpeed); @nextid = keys %$response; while ( $nextid[0] =~ m/^1\.3\.6\.1\.2\.1\.2\.2\.1\.5\./ ) { $InterfaceSpeed[$idx++] = $response->{$nextid[0]}; $response = $session->get_next_request($nextid[0]); @nextid = keys %$response; } $idx = 0; $response = $session->get_request($ifPhysAddress); $InterfaceMAC[$idx++] = $response->{$ifPhysAddress}; $response = $session->get_next_request($ifPhysAddress); @nextid = keys %$response; while ( $nextid[0] =~ m/^1\.3\.6\.1\.2\.1\.2\.2\.1\.6\./ ) { $InterfaceMAC[$idx++] = $response->{$nextid[0]}; $response = $session->get_next_request($nextid[0]); @nextid = keys %$response; } $idx = 0; $response = $session->get_request($ifAdminStatus); $InterfaceAdminStatus[$idx++] = $ifstati{$response->{$ifAdminStatus}}; $response = $session->get_next_request($ifAdminStatus); @nextid = keys %$response; while ( $nextid[0] =~ m/^1\.3\.6\.1\.2\.1\.2\.2\.1\.7\./ ) { $InterfaceAdminStatus[$idx++] = $ifstati{$response->{$nextid[0]}}; $response = $session->get_next_request($nextid[0]); @nextid = keys %$response; } $response = $session->get_next_request($ipAdEntIfIndex); @nextid = keys %$response; while ( $nextid[0] =~ m/^1\.3\.6\.1\.2\.1\.4\.20\.1\.2\./ ) { $ipaddress = $nextid[0]; $ipaddress =~ s/^1\.3\.6\.1\.2\.1\.4\.20\.1\.2\.//; $idx = ($response->{$nextid[0]})-1; if ( $IpAddress[$idx] ne "" ) { $IpAddress[$idx] = $IpAddress[$idx]."\n"; } $IpAddress[$idx] = $IpAddress[$idx]." ".$ipaddress; $response = $session->get_request("1.3.6.1.2.1.4.20.1.3.".$ipaddress); $IpAddress[$idx] = $IpAddress[$idx]."/".$response->{"1.3.6.1.2.1.4.20.1.3.".$ipaddress}; $response = $session->get_next_request($nextid[0]); @nextid = keys %$response; } $session->close; $idx = 0; print "\nSystem-Name: ", $SystemName; print $SystemBeschreibung, "\n"; printf "%-9s %-22s %5s %9s %6s %s\n", "Interface","Schnittstellen-Typ", "MTU","Bits/Sek","Status","MAC-Adr."; print " IP-Adresse/Subnetmask"; print "-------------------------------------------------------------------------"; while ( $InterfaceName[$idx] ne "" ) { printf "%-9s %-22s %5s %9s %-6s %s\n", $InterfaceName[$idx], $InterfaceTyp[$idx], $InterfaceMTU[$idx], $InterfaceSpeed[$idx], $InterfaceAdminStatus[$idx], $InterfaceMAC[$idx]; printf "%s\n\n", $IpAddress[$idx++]; } sub usage() { print "Verwendung: $0 --host=[IP-Adresse|Hostname] --community=name\n"; print " Für 'host' kann sowohl die IP-Adresse als auch\n"; print " der Hostname angegeben werden.\n"; }