use SNMP; # This is Net-SNMP. use Net::SNMP; # This is NOT. DO NOT USE WITH THIS TUTORIAL! #### #!/usr/bin/perl use warnings; use strict; use SNMP; use Socket; # VARIABLES YOU SHOULD EDIT. my $comm = 'public'; # EDIT ME! my $dest = 'localhost'; # EDIT ME! my $mib = 'sysDescr'; # Toy with this to get different # results. my $sver = '2'; # EDIT ME! # VARIABLES YOU SHOULD LEAVE ALONE. my $sess; # The SNMP::Session object that does the work. my $var; # Used to hold the individual responses. my $vb; # The Varbind object used for the 'real' query. # Initialize the MIB (else you can't do queries). &SNMP::initMib(); my %snmpparms; $snmpparms{Community} = $comm; $snmpparms{DestHost} = inet_ntoa(inet_aton($dest)); $snmpparms{Version} = $sver; $snmpparms{UseSprintValue} = '1'; $sess = new SNMP::Session(%snmpparms); # Turn the MIB object into something we can actually use. $vb = new SNMP::Varbind([$mib,'0']); # '0' is the instance. $var = $sess->get($vb); # Get exactly what we asked for. if ($sess->{ErrorNum}) { die "Got $sess->{ErrorStr} querying $dest for $mib.\n"; # Done as a block since you may not always want to die # in here. You could set up another query, just go on, # or whatever... } print $vb->tag, ".", $vb->iid, " : $var\n"; # Now let's show a MIB that might return multiple instances. $mib = 'ipNetToMediaPhysAddress'; # The ARP table! $vb = new SNMP::Varbind([$mib]); # No instance this time. # I prefer this loop method. YMMV. for ( $var = $sess->getnext($vb); ($vb->tag eq $mib) and not ($sess->{ErrorNum}); $var = $sess->getnext($vb) ) { print $vb->tag, ".", $vb->iid, " : ", $var, "\n"; } if ($sess->{ErrorNum}) { die "Got $sess->{ErrorStr} querying $dest for $mib.\n"; } exit; #### &SNMP::addMibFiles("/path/to/some/file.mib"); &SNMP::initMib(); #### # Where $destdir is /usr/share/snmp/mibs in most cases... system "mkdir -p $destdir/JScan/everything ; cd $destdir/JScan/everything ; find .. -type f -exec ln -sf {} \\;"; #### # Add the search directory. &SNMP::addMibDirs("/usr/share/snmp/mibs/JScan/everything"); # Load the modules AND their pre-reqs. &SNMP::loadModules('S5-CHASSIS-MIB', 'RAPID-CITY'); # Wonder-Twin powers, ACTIVATE! &SNMP::initMib(); #### Cannot find module (THIS-AND-THAT): At line 1 in (none) #### my $comm = 'ihavepower'; # Use read/write community. my $dest = '10.1.1.1'; # IP or DNS will work. my $sver = '2'; # Use 1 for simple devices, and 3 if you # really know your SNMP security. my %snmpparms; $snmpparms{Community} = $comm; $snmpparms{DestHost} = inet_ntoa(inet_aton($dest)); $snmpparms{Version} = $sver; $snmpparms{UseSprintValue} = 1; my $sess = new SNMP::Session(%snmpparms); my $mib = 'sysName'; my $instance = '0'; # There's only one instance of sysName. my $value = "New system name."; my $vb = new SNMP::Varbind([$mib,$instance,$value]); # This does it! $sess->set($vb); if ( $sess->{ErrorNum} ) { print "Got $sess->{ErrorStr} setting $mib on $host.\n"; } #### my $comm = 'ihavepower'; # Use read/write community. my $dest = '10.1.1.1'; # IP or DNS will work. my $sver = '2'; # Use 1 for simple devices, and 3 if you # really know your SNMP security. my %snmpparms; $snmpparms{Community} = $comm; $snmpparms{DestHost} = inet_ntoa(inet_aton($dest)); $snmpparms{Version} = $sver; $snmpparms{UseSprintValue} = 0; ### NEW SESSION REQUIRED! my $sess2 = new SNMP::Session(%snmpparms); my $mib = 'some32BitMib'; # Suppose it takes a packed IP. my $instance = '0'; # Will vary with the MIB object. my $value = inet_aton($ipaddr); my $vb = new SNMP::Varbind([$mib,$instance,$value]); $sess2->set($vb); if ( $sess2->{ErrorNum} ) { print "Got $sess2->{ErrorStr} setting $mib on $host.\n"; } #### $MIB[0] = 'sysName'; $MIB[1] = 'sysDescr'; $MIB[2] = 'sysLocation'; $vl = new SNMP::VarList([$MIB[0], 0], [$MIB[1], 0], [$MIB[2], 0]); @ANSWERS = $sess->get($vl); #### @MIBS = ('sysName', 'sysDescr', 'sysLocation'); foreach $mib ( @MIBS ) { push @bunchovbs, new SNMP::Varbind([$mib,0]); } # Now the magic! $vl = new SNMP::VarList(@bunchovbs); @ANSWERS = $sess->get($vl); #### @MIBS = ('ifAdminStatus', 'ifOperStatus', 'ifSpeed'); $vl = new SNMP::VarList([$MIBS[0]], [$MIBS[1]], [$MIBS[2]]); for ( $ifindex = 1 ; $ifindex < 25 ; $ifindex++ ) { @STATUS = $sess->getnext($vl); if ( $STATUS[0] eq 'up' ) { print "Port $ifindex enabled.\n"; } else next; print " Link status is $STATUS[1]. Speed is $STATUS[2].\n"; } #### $sess->getbulk(, , ) do an SNMP GETBULK, from the list of Varbinds, the single next lex- ico instance is fetched for the first n Varbinds as defined by . For remaining Varbinds, the m lexico instances are retrieved each of the remaining Varbinds, where m is . #### use SNMP; use Socket; # Set up the SNMP session. my %snmpparms = (); $snmpparms{DestHost} = inet_ntoa(inet_aton($host)); $snmpparms{Community} = 'public'; $snmpparms{UseSprintValue} = '1'; # readable! $snmpparms{Version} = '2'; # MUST USE 2 for GETBULK! my $sess = new SNMP::Session(%snmpparms); # I prefer to use VarLists for this sort of thing, since # we have enough confusion without making the actual # getbulk() call complicated. my @vbs = (); foreach my $mib ( 'sysName', 'sysDescr', 'sysLocation', 'sysUpTime', 'ifIndex', 'ifAdminStatus', 'ifOperStatus' ) { push @vbs, new SNMP::Varbind([$mib]); } my $vl = new SNMP::VarList(@vbs); # We'll keep our answers in these. my %sysStuff; my @ANSWERS; # Query the first four objects ONCE EACH, and store the # answers in the appropriate places in %sysStuff. # Then get ifIndex and ifAdminStatus 24 times and store # all of those reponses in @ANSWERS. ($sysStuff{Name}, $sysStuff{Descr}, $sysStuff{Location}, $sysStuff{UpTime}, @ANSWERS) = $sess->getbulk(4, 24, $vl); # AT LAST! # Always, always, always... if ( $sess->{ErrorNum} ) { die "Got ", $sess->{ErrStr}, " during getbulk.\n"; } # So $ANSWERS[0] now contains the first value of ifIndex. # $ANSWERS[1] contains the FIRST VALUE OF ifAdminStatus, # NOT the second value of ifIndex. # The remaining code could be MUCH simpler, but it's done # this way to illustrate how the answers are returned. my @INDEXES; my @STATUS; for ( my $x = 0 ; @ANSWERS ; $x++ ) { # Smart people would probably use map() for this. # I'm not that smart... $INDEXES[@INDEXES] = shift @ANSWERS; $STATUS[@STATUS] = shift @ANSWERS; # So we round-robin between @INDEXES and @STATUS, # thus "unstriping" the responses stored in @ANSWERS. } print "Name: $sysStuff{Name}\n"; print "Description: $sysStuff{Descr}\n"; print "Location: $sysStuff{Location}\n"; print "Uptime: $sysStuff{UpTime}\n"; print "\n"; # This now prints out clearly. for ( my $x = 0 ; $x <= $#INDEXES ; $x++ ) { print " INDEX: $INDEXES[$x] STATUS: $STATUS[$x]\n"; }