Search code examples
perlubuntu-18.04net-snmp

Why I'm not able to install Perl Net-SNMP on Ubuntu 18.04


I'm trying to use Perl Net-SNMP for some monitoring scripts on Ubuntu 18.04.
But when I execute the Perl script it says: Can't locate SNMP.pm in @INC (you may need to install the SNMP module).

I tried all possible ways to install Perl Net-SNMP and it is not able to get install successfully.
Below are the commands which I tried to install Perl Net-SNMP on Ubuntu 18.04:

sudo apt-get install perl-net-snmp
sudo apt-get install net-snmp

I even tried using tar ball installation of tar ball [ net-snmp_5.7.3+dfsg.orig.tar.xz ] downloaded from: Here.

Seeking active help to get rid of this.
This question is not previously asked so please do well research before marking as duplicate.

My Perl script:

#!/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;

Complete Error:

Can't locate SNMP.pm in @INC (you may need to install the SNMP module) (@INC contains: /home/prak/perl5/lib/perl5/5.26.1/x86_64-linux-gnu-thread-multi /home/prak/perl5/lib/perl5/5.26.1 /home/prak/perl5/lib/perl5/x86_64-linux-gnu-thread-multi /home/prak/perl5/lib/perl5 /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26 /home/prak/perl5/lib/perl5/5.26.0 /home/prak/perl5/lib/perl5/5.26.0/x86_64-linux-gnu-thread-multi /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at snmp1.pl line 4.
BEGIN failed--compilation aborted at snmp1.pl line 4.

Solution

  • Try sudo apt-get install libsnmp-perl and then simply install SNMP from CPAN as CPAN SNMP.