2

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.
PrakashG
  • 1,642
  • 5
  • 20
  • 30
  • 1
    Include a trivial perl script that tries to use the module but gives you the error? – Shawn Feb 14 '19 at 06:48
  • And the full error message, not just part of it. – Shawn Feb 14 '19 at 06:49
  • No wonder. You should be using `use Net::SNMP;`. Same for anywhere else the package name is used. – Shawn Feb 14 '19 at 07:01
  • So... This is random code you found, not something you wrote intending to use Net::SNMP? Then it looks like it uses the [SNMP](https://metacpan.org/pod/SNMP) module. You should be able to figure out the Ubuntu package name for that one given @duskwuff's answer. – Shawn Feb 14 '19 at 08:00
  • Nah, I don't do chat. What package did you install this time? – Shawn Feb 14 '19 at 08:05
  • `Net::SNMP` and `SNMP` are two different modules, with two different packages in Ubuntu. Again, see @duskwuff's answer for the rules to figure out the Ubuntu package name from a perl module name. – Shawn Feb 14 '19 at 08:24
  • Install the correct package? – Shawn Feb 14 '19 at 08:31
  • You installed `libsnmp-perl`? That's not what you said earlier (Deleting all your comments is making this **really** hard for me or anyone else to follow, btw.) – Shawn Feb 14 '19 at 08:55

2 Answers2

8

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

Vicky
  • 121
  • 5
  • I've you've installed `libsnmp-perl` then there's no need to install `SNMP` from CPAN as it has already been installed by `apt-get` :-) – Dave Cross Feb 16 '19 at 12:43
6

The package you need to install is called libnet-snmp-perl. Names of Perl module packages on Debian and related distributions always start with lib and end with -perl.

The files you've installed are a set of SNMP utilities, not a Perl module.