aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteinar H. Gunderson <sgunderson@bigfoot.com>2014-04-06 11:46:56 +0200
committerSteinar H. Gunderson <sgunderson@bigfoot.com>2014-04-06 11:46:56 +0200
commit7ea6c6205ef652018f5fdfc54d76d6212dc2b59a (patch)
tree6fdc972c138198fdcf1cb57ba5afee6fd69b8bad
parent47b37365d5946b3821ec9ade3bb04c2bc64b8b63 (diff)
Add a common utility function to create an SNMP session, that also supports SNMPv3.
-rw-r--r--include/nms.pm35
1 files changed, 34 insertions, 1 deletions
diff --git a/include/nms.pm b/include/nms.pm
index 9567bde..047c243 100644
--- a/include/nms.pm
+++ b/include/nms.pm
@@ -4,10 +4,10 @@ use warnings;
use DBI;
use Net::Telnet;
use Data::Dumper;
+use Net::SNMP;
use FileHandle;
package nms;
-
use base 'Exporter';
our @EXPORT = qw(switch_disconnect switch_connect switch_exec switch_timeout db_connect);
@@ -101,4 +101,37 @@ sub switch_disconnect {
$conn->close;
}
+sub snmp_open_session {
+ my ($ip, $community) = @_;
+
+ my $domain = ($ip =~ /:/) ? 'udp6' : 'udp4';
+ my $version;
+ my %options = (
+ -hostname => $ip,
+ -domain => $domain,
+ );
+
+ if ($community =~ /^snmpv3:(.*)$/) {
+ my ($username, $authprotocol, $authpassword, $privprotocol, $privpassword) = split /\//, $1;
+
+ $options{'-username'} = $username;
+ $options{'-authprotocol'} = $authprotocol;
+ $options{'-authpassword'} = $authpassword;
+
+ if (defined($privprotocol) && defined($privpassword)) {
+ $options{'-privprotocol'} = $privprotocol;
+ $options{'-privpassword'} = $privpassword;
+ }
+
+ $options{'-version'} = 3;
+ } else {
+ $options{'-version'} = 2;
+ }
+
+ my ($session, $error) = Net::SNMP->session(%options);
+ die "SNMP session failed: " . $error if (!defined($session));
+
+ return $session;
+}
+
1;