473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Replace HP OV SNMP call to Perl SNMP API

4
how will I change the code the 1st code according to the 2nd one?
1st code
Expand|Select|Wrap|Line Numbers
  1. sub snmpGet(@)
  2. {
  3.     # Assign arguments
  4.     my ($deviceName)=$_[0];     # Device name to read from
  5.     my ($community)=$_[1];      # SNMP Community String
  6.     my ($timeout)=$_[2];        # Timeout value for SNMP-GET request
  7.     my ($type)=$_[3];           # SNMP Object Type
  8.     my ($oid)=$_[4];            # SNMP Object ID
  9.     my ($version)=$_[5];        # SNMP Version
  10.     my ($port)=$_[6];           # Port
  11.  
  12.     # Local variables
  13.     my ($result);               # Output from SNMP-GET
  14.  
  15.  
  16.     # Set default version and port
  17.     if ($version) { $version='-v ' . $version; } else { $version=''; }
  18.     if ($port) { $port='-p ' . $port; } else { $port=''; }
  19.  
  20.     # Check parameters
  21.     unless ($deviceName && $community && $timeout && $type && $oid)
  22.     {
  23.         # Missing parameter
  24.         rxnnmsLogError("${snmpErrorPrefix}snmpGet:missing parameter:snmpGet($deviceName,$community,$timeout,$type,$oid");
  25.         return;
  26.     }
  27.  
  28.     # Check for valid type
  29.     unless ($type eq $snmpIntegerType || $type eq $snmpIPAddressType ||
  30.             $type eq $snmpOctetStringType || $type eq $snmpGauge32Type ||
  31.             $type eq $snmpCounter64Type || $type eq $snmpTimeTicksType)
  32.     {
  33.         rxnnmsLogError("${snmpErrorPrefix}snmpGet:invalid type:$type");
  34.         return;
  35.     }
  36.  
  37.     # Generate the SNMP-GET using syntax of underlying SNMP implementation.
  38.     rxnnmsLogTrace("$snmpget $version $port -c $community -t $timeout $deviceName $oid");
  39.     $result=`$snmpget $version $port -c $community -t $timeout $deviceName $oid 2>&1`;
  40.  
  41.     # Trace output of snmpget command
  42.     chomp($result);
  43.     rxnnmsLogTrace("$snmpget $oid to $deviceName returned: $result");
  44.  
  45.    # If $type is 'integer' and $result contains 'INTEGER' or 'Unsigned' then SNMP-GET ran
  46.     # successfully, so return $result, etc. Otherwise return '$snmpErrorPrefix$result'
  47.     # (bad status).
  48.  
  49.     if (($type=$snmpIntegerType && $result=~/INTEGER/) ||
  50.         ($type=$snmpIntegerType && $result=~/Unsigned/) ||
  51.         ($type=$snmpIPAddressType && $result=~/IpAddress/) ||
  52.         ($type=$snmpOctetStringType && $result=~/DISPLAY STRING/) ||
  53.         ($type=$snmpOctetStringType && $result=~/OCTET STRING/) ||
  54.         ($type=$snmpGauge32Type && $result=~/Gauge32/) ||
  55.         ($type=$snmpCounter64Type && $result=~/Counter64/) ||
  56.         ($type=$snmpTimeTicksType && $result=~/Timeticks/))
  57.     {
  58.         # SNMP-GET successful. Return output.
  59.         rxnnmsLogTrace("$snmpget $oid from $deviceName was successful");
  60.         return($result);
  61.     }
  62.     else
  63.     {
  64.         # SNMP-GET failed. Return error.
  65.         rxnnmsLogTrace("$snmpget $oid from $deviceName failed: $result");
  66.         return("$snmpErrorPrefix$result");
  67.     }
  68.  
  69. } # End of snmpGet
  70.  
2nd code
Expand|Select|Wrap|Line Numbers
  1. sub snmpget ($@) {
  2.   my($host, @vars) = @_;
  3.   my(@enoid, $var, $response, $bindings, $binding, $value, $oid, @retvals);
  4.   my $session;
  5.  
  6.   $session = &snmpopen($host, 0, \@vars);
  7.   if (!defined($session)) {
  8.     carp "SNMPGET Problem for $host\n"
  9.       unless ($SNMP_Session::suppress_warnings > 1);
  10.     return undef;
  11.   }
  12.  
  13.   @enoid = &toOID(@vars);
  14.   return undef unless defined $enoid[0];
  15.  
  16.   if ($session->get_request_response(@enoid)) {
  17.     $response = $session->pdu_buffer;
  18.     ($bindings) = $session->decode_get_response($response);
  19.     while ($bindings) {
  20.       ($binding, $bindings) = decode_sequence($bindings);
  21.       ($oid, $value) = decode_by_template($binding, "%O%@");
  22.       my $tempo = pretty_print($value);
  23.       push @retvals, $tempo;
  24.     }
  25.     return(@retvals);
  26.   }
  27.   $var = join(' ', @vars);
  28.   carp "SNMPGET Problem for $var on $host\n"
  29.     unless ($SNMP_Session::suppress_warnings > 1);
  30.   return undef;
  31. }
  32.  
Sep 19 '08 #1
7 3509
numberwhun
3,509 Expert Mod 2GB
First, you need to please use code tags around any code you post into the forums. There is a sample in the "Reply Guidelines" next to your reply window.

Also, you need to please better explain what it is you are looking for or to do as I don't know what you want.

Regards,

Jeff (Moderator)

NOTE:: I have deleted your second AND third posts as they are duplicates of this one. Just because you have a short timeline and what you are working on is urgent, please know that that urgency does not translate into an urgency for us. We are all volunteers here and are not under your same time crunch. Please DO NOT double post your questions just because you do not get an answer as quick as you expect.
Sep 19 '08 #2
KevinADC
4,059 Expert 2GB
I don't even understand what the question/problem is, so there is no way to begin to answer it.
Sep 19 '08 #3
moon1
4
hi,
First of all the 2nd and third posts were different codes from the 1st post.They were not the same codes.I value your and mine time.
The 1st code is from a HP OV CommonSnmp.pm.The 2nd code is from snmp_util.pm.
I just wanted the 1st code to be transparent to the second code. i.e. the parameters that are passed by the 1st code must be same as the 2nd code. Also the operations of the 1st code to be same as the 2nd code. So that when I use CommonSnmp.pm it works alike like snmp_util.pm.
Sep 22 '08 #4
moon1
4
Also the 2nd code has called the subroutines like snmpopen & toOID.the definations are as follows:

Expand|Select|Wrap|Line Numbers
  1. sub snmpopen  ($$$) {
  2.   my($host, $type, $vars) = @_;
  3.   my($nhost, $port, $community, $lhost, $lport, $nlhost);
  4.   my($timeout, $retries, $backoff, $version);
  5.   my $v4onlystr;
  6.  
  7.   $type = 0 if (!defined($type));
  8.   $community = "public";
  9.   $nlhost = "";
  10.  
  11.   ($community, $host) = ($1, $2) if ($host =~ /^(.*)@([^@]+)$/);
  12.  
  13.   # We can't split on the : character because a numeric IPv6
  14.   # address contains a variable number of :'s
  15.   my $opts;
  16.  if( ($host =~ /^(\[.*\]):(.*)$/) || ($host =~ /^(\[.*\])$/) ) {
  17.     # Numeric IPv6 address between []
  18.     ($host, $opts) = ($1, $2);
  19.   } else {
  20.     # Hostname or numeric IPv4 address
  21.     ($host, $opts) = split(':', $host, 2);
  22.   }
  23.   ($port, $timeout, $retries, $backoff, $version, $v4onlystr) = split(':', $opts, 6)
  24.     if(defined($opts) && (length $opts > 0) );
  25.  
  26.   undef($version) if (defined($version) && length($version) <= 0);
  27.   $v4onlystr = "" unless defined $v4onlystr;
  28.   $version = '1' unless defined $version;
  29.   if (defined($port) && ($port =~ /^([^!]*)!(.*)$/)) {
  30.     ($port, $lhost) = ($1, $2);
  31.     $nlhost = $lhost;
  32.     ($lhost, $lport) = ($1, $2) if ($lhost =~ /^(.*)!(.*)$/);
  33.     undef($lhost) if (defined($lhost) && (length($lhost) <= 0));
  34.     undef($lport) if (defined($lport) && (length($lport) <= 0));
  35.   }
  36.   undef($port) if (defined($port) && length($port) <= 0);
  37.   $port = 162 if ($type == 1 && !defined($port));
  38.   $nhost = "$community\@$host";
  39.   $nhost .= ":" . $port if (defined($port));
  40.  
  41.   if ((!defined($SNMP_util::Session))
  42.     || ($SNMP_util::Host ne $nhost)
  43.     || ($SNMP_util::Version ne $version)
  44.     || ($SNMP_util::LHost ne $nlhost)
  45.     || ($SNMP_util::IPv4only ne $v4onlystr)) {
  46.     if (defined($SNMP_util::Session)) {
  47.       $SNMP_util::Session->close();
  48.       undef $SNMP_util::Session;
  49.       undef $SNMP_util::Host;
  50.       undef $SNMP_util::Version;
  51.       undef $SNMP_util::LHost;
  52.       undef $SNMP_util::IPv4only;
  53.     }
  54.     $SNMP_util::Session = ($version =~ /^2c?$/i)
  55.       ? SNMPv2c_Session->open($host, $community, $port, undef,
  56.                 $lport, undef, $lhost, ($v4onlystr eq 'v4only') ? 1:0 )
  57.       : SNMP_Session->open($host, $community, $port, undef,
  58.                 $lport, undef, $lhost, ($v4onlystr eq 'v4only') ? 1:0 );
  59.     ($SNMP_util::Host = $nhost, $SNMP_util::Version = $version,
  60.       $SNMP_util::LHost = $nlhost, $SNMP_util::IPv4only = $v4onlystr) if defined($SNMP_util::Session);
  61.   }
  62.  
  63.   if (defined($SNMP_util::Session)) {
  64.     if (ref $vars->[0] eq 'HASH') {
  65.       my $opts = shift @$vars;
  66.       foreach $type (keys %$opts) {
  67.     if ($type eq 'return_array_refs') {
  68.       $SNMP_util::Return_array_refs = $opts->{$type};
  69.     }
  70.     else {
  71.       if (exists $SNMP_util::Session->{$type}) {
  72.         if ($type eq 'timeout') {
  73.           $SNMP_util::Session->set_timeout($opts->{$type});
  74.         } elsif ($type eq 'retries') {
  75.           $SNMP_util::Session->set_retries($opts->{$type});
  76.         } elsif ($type eq 'backoff') {
  77.           $SNMP_util::Session->set_backoff($opts->{$type});
  78.         } else {
  79.           $SNMP_util::Session->{$type} = $opts->{$type};
  80.         }
  81.       } else {
  82.         carp "SNMPopen Unknown SNMP Option Key '$type'\n"
  83.           unless ($SNMP_Session::suppress_warnings > 1);
  84.       }
  85.     }
  86.       }
  87.     }
  88.     $SNMP_util::Session->set_timeout($timeout)
  89.       if (defined($timeout) && (length($timeout) > 0));
  90.     $SNMP_util::Session->set_retries($retries)
  91.       if (defined($retries) && (length($retries) > 0));
  92.     $SNMP_util::Session->set_backoff($backoff)
  93.       if (defined($backoff) && (length($backoff) > 0));
  94.   }
  95.   return $SNMP_util::Session;
  96. }
  97.  
  98. sub toOID(@) {
  99.   my(@vars) = @_;
  100.   my($oid, $var, $tmp, $tmpv, @retvar);
  101.  
  102.   undef @retvar;
  103.   foreach $var (@vars) {
  104.     ($oid, $tmp) = &Check_OID($var);
  105.     if (!$oid && $SNMP_util::CacheLoaded == 0) {
  106.       $tmp = $SNMP_Session::suppress_warnings;
  107.       $SNMP_Session::suppress_warnings = 1000;
  108.  
  109.       &snmpLoad_OID_Cache($SNMP_util::CacheFile);
  110.  
  111.       $SNMP_util::CacheLoaded = 1;
  112.       $SNMP_Session::suppress_warnings = $tmp;
  113.  
  114.       ($oid, $tmp) = &Check_OID($var);
  115.     }
  116.     while (!$oid && $#SNMP_util::MIB_Files >= 0) {
  117.       $tmp = $SNMP_Session::suppress_warnings;
  118.       $SNMP_Session::suppress_warnings = 1000;
  119.  
  120.       snmpMIB_to_OID(shift(@SNMP_util::MIB_Files));
  121.  
  122.       $SNMP_Session::suppress_warnings = $tmp;
  123.  
  124.       ($oid, $tmp) = &Check_OID($var);
  125.       if ($oid) {
  126.     open(CACHE, ">>$SNMP_util::CacheFile");
  127.     print CACHE "$tmp\t$oid\n";
  128.     close(CACHE);
  129.       }
  130.     }
  131.     if ($oid) {
  132.       $var =~ s/^$tmp/$oid/;
  133.     } else {
  134.       carp "Unknown SNMP var $var\n"
  135.       unless ($SNMP_Session::suppress_warnings > 1);
  136.       next;
  137.     }
  138.     while ($var =~ /\"([^\"]*)\"/) {
  139.       $tmp = sprintf("%d.%s", length($1), join(".", map(ord, split(//, $1))));
  140.       $var =~ s/\"$1\"/$tmp/;
  141.     }
  142.     print "toOID: $var\n" if $SNMP_util::Debug;
  143.     $tmp = encode_oid_with_errmsg($var);
  144.     return undef unless defined $tmp;
  145.     push(@retvar, $tmp);
  146.   }
  147.   return @retvar;
  148. }
  149.  
Sep 22 '08 #5
KevinADC
4,059 Expert 2GB
Sorry mate, I have no idea.
Sep 22 '08 #6
moon1
4
toOID subroutine has depemdencies on the following subroutines Check_OID :
Expand|Select|Wrap|Line Numbers
  1. sub Check_OID  ($) {
  2.   my($var) = @_;
  3.   my($tmp, $tmpv, $oid);
  4.  
  5.   if ($var =~ /^(([a-zA-Z][a-zA-Z\d\-]*\.)*([a-zA-Z][a-zA-Z\d\-]*))/)
  6.   {
  7.     $tmp = $&;
  8.     $tmpv = $tmp;
  9.     for (;;) {
  10.       last if defined($SNMP_util::OIDS{$tmpv});
  11.       last if !($tmpv =~ s/^[^\.]*\.//);
  12.     }
  13.     $oid = $SNMP_util::OIDS{$tmpv};
  14.     if ($oid) {
  15.       return ($oid, $tmp);
  16.     } else {
  17.       return undef;
  18.     }
  19.   }
  20.   return ($var, $var);
  21. }
  22.  
Sep 22 '08 #7
numberwhun
3,509 Expert Mod 2GB
Moon1:

I have asked you nicely to please use code tags in a previous post AND I have even sent you a PM (private message) relating to your lack of code tags use. This is your last warning, use code tags or I will be forced to ban you for 1 week for failure to comply with site guidelines AND not listening to the suggestions of Moderator(s).

Regards,

Moderator
Sep 22 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Matthew Bell | last post by:
Hi, I'm looking for a high-performance SNMP manager toolkit (SNMPv1 / v2, GET / GETNEXT / GETBULK) I can use with Python on Windows2K/XP. I wonder if anyone has got any suggestions? Commercial...
1
by: dan byers | last post by:
hey everyone, i'm working on a system to monitor devices through SNMP. i'm using the Net::SNMP modules by D.Town. i try to load the snmp session request with a set of OIDs to make the system...
3
by: Kevin Ingram | last post by:
Is it possible to connect to devices and read SNMP data with ASP? I did a search and all I could find was utilities for sale to do it, wondered if it can be done using straight ASP. We are using...
6
by: Danny | last post by:
I need an asp command to strip out from a string all extra punctuation such as apostrophe, comma, period, spaces dashes, etc etc and just leave the letters. Can anybody give me some ideas? ...
5
by: mony | last post by:
I am looking a VB.NET example on how to reference the SNMP API information. I am unable to add references to SNMPAPI.LIB or SNMPAPI.DLL. My goal is to write a 100% VB.NET example (no C or C#...
2
by: TiToine | last post by:
Hi, I'am using the SnmpMgrOpen() api function. ####################################################################### LPSNMP_MGR_SESSION SnmpMgrOpen( LPSTR lpAgentAddress, // name and...
1
by: Schroeder, AJ | last post by:
Hello group, I performed binary installs of Net-SNMP and PHP 4.4.4 on my Windows XP laptop so that I can do some offline development/testing away from my Linux server. Anyway, I attempted to do...
11
by: kr99 | last post by:
I am trying to run a scrpt and I get an error indicating that the Perl Net::SNMP module may not be loaded. #perl udpInOut.pl localhost 161 public 1 "Can't locate object method "session" via...
1
by: Dirk Van de moortel | last post by:
{ OOPS, posted this to moderated group perl.cpan.discuss as well, so it can take a while to get through, if it gets through at all Otherwise sorry for duplicate posting } We need to set up a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.