CongoGrey has asked for the wisdom of the Perl Monks concerning the following question:

I apologize if this is an obvious question but, I have been working on this and can't seem to get it working. I want to do an SNMP get then generate a trap with the information recieved from the get and some other information. I have stripped this code down to it bare essentials:
#!/opt/VRTSnc/bin/perl -w use Net::SNMP; my $NodeAddress = '10.72.36.1'; my $ReadCommunity = 'public'; my ($session1, @trapvars, $error) = Net::SNMP->session( -hostname => $NodeAddress, -community => $ReadCommunity); if (!defined($session1)){ printf("ERROR: %s.'n", $error); exit 1;} my $ifAlias = '.1.3.6.1.2.1.31.1.1.1.18.110'; my $result = $session1->get_request( -varbinds => [$ifAlias]); if (!defined($session1)){ printf("ERROR: %.'n", $error); exit 1;} my $ifDescr = $result->{$ifAlias}; my ($session2) = Net::SNMP->session( -hostname => '10.62.11.58', -port => '162', -community => 'public'); if (!defined($session2)){ printf("ERROR: %s.'n", $error); exit 1;} $trapvars[0] = ".1.3.6.1.4.1.78.0.100147"; $trapvars[1] = OCTET_STRING; $trapvars[2] = 'IFName'; $trapvars[3] = ".1.3.6.1.4.1.78.0.100147"; $trapvars[4] = OCTET_STRING; $trapvars[5] = $result->{$ifDescr}; $session2->trap( -enterprise => '1.3.6.1.4.1.78', -agentaddr => $NodeAddress, -generictrap => '6', -specifictrap => '100147', -varbindlist => \@trapvars); if (!defined($session2)){ printf("ERROR: %s.'n", $error); exit 1;} $session1->close;
If I print the array i see the following information: .1.3.6.1.4.1.78.0.100147 4 IFName .1.3.6.1.4.1.78.0.100147 4 HASH(0xeea8c)

Replies are listed 'Best First'.
Re: SNMP Get and Trap
by AcidHawk (Vicar) on Jan 10, 2003 at 06:47 UTC

    I gave this a bash and found several funnies. Firstly I dont know what mib you are using so I tested with MIB2 and used system uptime. (I.e. .1.3.6.1.2.1.1.3.0)

    Line 13 and 14 seem unnecessary as you are checking if $session1 is defined earlier in line 8 and 9.

    I added a print after the $ifDescr and found that it was giving me the correct uptime for my machine. I then changed line 27 $trapvars[5] = $result->{$ifDescr}; to $trapvars[5] = $ifDescr; add instead of getting the HASH stuff I was getting the correct uptime.

    Bellow is my code and a few formatting adjustments for my readability.

    #!/usr/bin/perl use strict; use warnings; use Net::SNMP; my $NodeAddress = 'localhost'; my $ReadCommunity = 'public'; my ($session1, @trapvars, $error) = Net::SNMP->session( -hostname => $ +NodeAddress, -community => $ReadCommunity); if (!defined($session1)){ printf("ERROR: %s.\n", $error); exit 1; } #my $ifAlias = '.1.3.6.1.2.1.31.1.1.1.18.110'; my $ifAlias = '.1.3.6.1.2.1.1.3.0'; my $result = $session1->get_request($ifAlias); if (!defined($session1)){ printf("ERROR: %s.\n", $error); exit 1; } print "Result = $result\n"; my $ifDescr = $result->{$ifAlias}; print "ifDescr = $ifDescr\n"; my ($session2) = Net::SNMP->session( -hostname => 'localhost', -port => '162', -community => 'public'); if (!defined($session2)){ printf("ERROR: %s.'n", $error); exit 1; } $trapvars[0] = ".1.3.6.1.4.1.78.0.100147"; $trapvars[1] = OCTET_STRING; $trapvars[2] = 'IFName'; $trapvars[3] = ".1.3.6.1.4.1.78.0.100147"; $trapvars[4] = OCTET_STRING; $trapvars[5] = $ifDescr; print "Array = @trapvars\n"; $session2->trap( -enterprise => '1.3.6.1.4.1.78', -agentaddr => $NodeAddress, -generictrap => '6', -specifictrap => '100147', -varbindlist => \@trapvars); if (!defined($session2)){ printf("ERROR: %s.\n", $error); exit 1; } $session1->close; $session2->close;
    -----
    Of all the things I've lost in my life, its my mind I miss the most.