http://qs321.pair.com?node_id=1088729


in reply to Net::SNMP::Interfaces how to print the error()

To clarify what hexcoder said...

Based on the documentation, $Net::SNMP::Interfaces::error is only set if you haven't used RaiseError => 1. You should be able to do one of the following:

#!/usr/bin/perl use strict; use warnings; use Net::SNMP::Interfaces; use Data::Dumper qw(Dumper); my $interfaces = Net::SNMP::Interfaces->new( Hostname => '127.0.0.1', Community => 'public', Port => '1161', RaiseError => 1, # Errors are fatal ); my @ifnames = $interfaces->all_interfaces(); print Dumper (\@ifnames);
#!/usr/bin/perl use strict; use warnings; use Net::SNMP::Interfaces; use Data::Dumper qw(Dumper); my $interfaces = Net::SNMP::Interfaces->new( Hostname => '127.0.0.1', Community => 'public', Port => '1161', ) or die $Net::SNMP::Interfaces::error; my @ifnames = $interfaces->all_interfaces(); print Dumper (\@ifnames);

Replies are listed 'Best First'.
Re^2: Net::SNMP::Interfaces how to print the error()
by thanos1983 (Parson) on Jun 04, 2014 at 20:41 UTC

    To: Mr. Muskrat,

    Thanks for the clarification part, I was not sure if I did understand correctly.

    Well in conclusion, by using the die command:

    my $interfaces = Net::SNMP::Interfaces->new( Hostname => '192.168.18 +4.11', Community => 'public', Port => '1161', #RaiseError => 1, )or die $Net::SNMP::Interfaces::error; my @ifnames = $interfaces->all_interfaces(); print Dumper (\@ifnames);

    The output is:

    No response from remote host "127.0.0.1" at test.pl line 10

    By changing the code to:

    my $interfaces = Net::SNMP::Interfaces->new( Hostname => '127.0.0.1' +, Community => 'public', Port => '1161', RaiseError => 1, ); my @ifnames = $interfaces->all_interfaces();

    The output is:

    Net::SNMP::Interfaces: at test.pl line 10.

    The whole point of my question was that I would like to see a printout like:

    22:19:01.537280 IP 0.0.0.0 > 0.0.0.0: ICMP 0.0.0.0 udp port 1161 unrea +chable, length 36

    Which the actual output of TCPDUMP, in order to inform the user that the port or community or what ever the error is wrong.

    Both solutions indicate that the error comes from line 10 which is Net::SNMP::Interfaces request so there is an indication, I was just looking for something extra since the documentation said that there might give more information if the user include the RaiseError => 1 command.

    Update:

    By removing the RaiseError => 1 the (HASH %arg) like:

    my $interfaces = Net::SNMP::Interfaces->new( Hostname => '192.168.18 +4.11', Community => 'public', Port => '1161', ); my @ifnames = $interfaces->all_interfaces(); print Dumper (\@ifnames);

    The error becomes:

    Can't call method "all_interfaces" on an undefined value at test.pl li +ne 12.

    Which points out to the array, which makes the user wonder what is the error, well in this case I would suggest to use one of the upper solutions so at least the user get's an idea of the error and not be looking at the wrong direction.

    Thank you all for your time and effort, replying to my question.

    Seeking for Perl wisdom...on the process...not there...yet!