Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by thanos1983 (Parson)
on Jun 04, 2014 at 12:16 UTC ( #1088620=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,

Thank you for your time and effort in advance. Well my question can be extremely simple to someone who has figure this out, but is seems a bit complicated to someone who is not familiar.

Two days ago I discovered this powerful module Net::SNMP::Interfaces.

The uniqueness and beauty of this tool is that it can detect non sequential interfaces. Which at my case is exactly what I was looking for.

While I was trying to experiment with the tool I read:

From: Net::SNMP::InterfacesThere is a also a fourth optional argument 'RaiseError' which determines the behaviour of the module in the event there is an error while creating the SNMP. Normally new() will return undef if there was an error but if RaiseError is set to a true value it will die() printing the error string to STDERR. If this is not set and an error occurs undef will be return and the variable $Net::SNMP::Interfaces::error will contain the test of the error. Because the interfaces are discovered in the constructor, if the module is to be used in a long running program to monitor a host where interfaces might be added or removed it is recommended that the object returned by new() is periodically destroyed and a new one constructed.

Well while I was trying to play around to see the error in case that I am using the RaiseError option active, I would expect to see the error printed.

Sample of working code displayed under:

#!/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, ) or die $Net::SNMP::Interfaces::error; my @ifnames = $interfaces->all_interfaces(); print Dumper (\@ifnames);

For testing purposes I have changed on the code the port to 1161, on the effort to produce an error and display it.

I assume that I am using wrong syntax on the RaiseError => 1 option so I also tried RaiseError => 'TRUE' but unfortunately not printing the error.

The output that I am getting is the following:

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

So my question is am I doing something wrong? Or I am asking too much details when it come to printing the error?

Thanks again for everyone's time and effort.

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

Replies are listed 'Best First'.
Re: Net::SNMP::Interfaces how to print the error()
by Mr. Muskrat (Canon) on Jun 04, 2014 at 19:45 UTC

    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);

      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!
Re: Net::SNMP::Interfaces how to print the error()
by hexcoder (Deacon) on Jun 04, 2014 at 13:41 UTC
    error is a method of this module, so I guess its
    Net::SNMP::Interfaces::error()
    instead of
    $Net::SNMP::Interfaces::error
      O sorry, my mistake in the previous post.
      $Net::SNMP::Interfaces::error _is_ also a variable, but it is only set when RaiseError is set to a false value (which is not the case here).

      So either set RaiseError to a true value (like '1' or 'True') and let it die with the error message
      or set RaiseError to a false value (like undef) and then use your own die with $Net::SNMP::Interfaces::error.

        To: hexcoder,

        Well I tried both solutions by miss using the port option (1 or TRUE), but the result is the same.

        This is the reason that I am curious. My code works fine, but it would be nice to see for example wrong Port or this port is not responding. Something that can assist the user to understand the error.

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

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1088620]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2020-08-15 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (79 votes). Check out past polls.

    Notices?