how will I change the code the 1st code according to the 2nd one?
1st code
2nd code
1st code
Code:
sub snmpGet(@)
{
# Assign arguments
my ($deviceName)=$_[0]; # Device name to read from
my ($community)=$_[1]; # SNMP Community String
my ($timeout)=$_[2]; # Timeout value for SNMP-GET request
my ($type)=$_[3]; # SNMP Object Type
my ($oid)=$_[4]; # SNMP Object ID
my ($version)=$_[5]; # SNMP Version
my ($port)=$_[6]; # Port
# Local variables
my ($result); # Output from SNMP-GET
# Set default version and port
if ($version) { $version='-v ' . $version; } else { $version=''; }
if ($port) { $port='-p ' . $port; } else { $port=''; }
# Check parameters
unless ($deviceName && $community && $timeout && $type && $oid)
{
# Missing parameter
rxnnmsLogError("${snmpErrorPrefix}snmpGet:missing parameter:snmpGet($deviceName,$community,$timeout,$type,$oid");
return;
}
# Check for valid type
unless ($type eq $snmpIntegerType || $type eq $snmpIPAddressType ||
$type eq $snmpOctetStringType || $type eq $snmpGauge32Type ||
$type eq $snmpCounter64Type || $type eq $snmpTimeTicksType)
{
rxnnmsLogError("${snmpErrorPrefix}snmpGet:invalid type:$type");
return;
}
# Generate the SNMP-GET using syntax of underlying SNMP implementation.
rxnnmsLogTrace("$snmpget $version $port -c $community -t $timeout $deviceName $oid");
$result=`$snmpget $version $port -c $community -t $timeout $deviceName $oid 2>&1`;
# Trace output of snmpget command
chomp($result);
rxnnmsLogTrace("$snmpget $oid to $deviceName returned: $result");
# If $type is 'integer' and $result contains 'INTEGER' or 'Unsigned' then SNMP-GET ran
# successfully, so return $result, etc. Otherwise return '$snmpErrorPrefix$result'
# (bad status).
if (($type=$snmpIntegerType && $result=~/INTEGER/) ||
($type=$snmpIntegerType && $result=~/Unsigned/) ||
($type=$snmpIPAddressType && $result=~/IpAddress/) ||
($type=$snmpOctetStringType && $result=~/DISPLAY STRING/) ||
($type=$snmpOctetStringType && $result=~/OCTET STRING/) ||
($type=$snmpGauge32Type && $result=~/Gauge32/) ||
($type=$snmpCounter64Type && $result=~/Counter64/) ||
($type=$snmpTimeTicksType && $result=~/Timeticks/))
{
# SNMP-GET successful. Return output.
rxnnmsLogTrace("$snmpget $oid from $deviceName was successful");
return($result);
}
else
{
# SNMP-GET failed. Return error.
rxnnmsLogTrace("$snmpget $oid from $deviceName failed: $result");
return("$snmpErrorPrefix$result");
}
} # End of snmpGet
Code:
sub snmpget ($@) {
my($host, @vars) = @_;
my(@enoid, $var, $response, $bindings, $binding, $value, $oid, @retvals);
my $session;
$session = &snmpopen($host, 0, \@vars);
if (!defined($session)) {
carp "SNMPGET Problem for $host\n"
unless ($SNMP_Session::suppress_warnings > 1);
return undef;
}
@enoid = &toOID(@vars);
return undef unless defined $enoid[0];
if ($session->get_request_response(@enoid)) {
$response = $session->pdu_buffer;
($bindings) = $session->decode_get_response($response);
while ($bindings) {
($binding, $bindings) = decode_sequence($bindings);
($oid, $value) = decode_by_template($binding, "%O%@");
my $tempo = pretty_print($value);
push @retvals, $tempo;
}
return(@retvals);
}
$var = join(' ', @vars);
carp "SNMPGET Problem for $var on $host\n"
unless ($SNMP_Session::suppress_warnings > 1);
return undef;
}
Comment