2012-07-05 61 views
1

在執行1個perl的文件..... m如果這個問題Perl的錯誤而寫SNMPGET命令功能

無法識別的字符\ X03;標誌着< - PK <後這裏 - 在mibwalk.pl線附近HERE列3 1

任何人有任何想法吧

Code---> 

#!usr/bin/perl 
use BER; 
use SNMP_util; 
use SNMP_Session; 
$MIB1 = 「sysLocation.0」; 
$HOST = 「192.168.1.150」; 
($value) = &snmpget(「public\@$HOST」,」$MIB1」); 
if($value) { 
      print 「Results :$MIB1: :$value:\n」; 
} 
    else { warn 「No response from host :$HOST:\n」; } 
+0

daxim嗨,這是我的代碼 – Gaurav 2012-07-06 11:54:52

回答

1

您必須以十六進制格式發送字符串。 在我的情況下,NET :: SNMP模塊不可行。

所以我改性瀝青這個模塊的源代碼,使之成爲可能。 我用的Net::SNMP以下版本:

SNMP.pm,v 5.3 2005/10/20 14:17:01 
Version v5.2.0 

看到這個職位的詳細信息的底部。

現在,您可以使用set_request()函數Net::SNMP和新的ASN.1類型HEX_STRING。

如:

#! /usr/local/bin/perl 
use strict; 
use warnings; 
use Net::SNMP; 

my $OID_sysContact = '1.3.6.1.2.1.1.4.0'; 

my ($session, $error) = Net::SNMP->session(
     -hostname => shift || 'localhost', 
     -community => shift || 'public', 
    ); 

my $rv = $session->set_request(-varbindlist => [ $OID_sysContact, HEX_STRING, 'FF0a1200' ],); 

$session->close(); 
exit 0; 


# ============================================================================ 
package Net::SNMP; 

# $Id: SNMP.pm,v 5.3 2005/10/20 14:17:01 dtown Rel $ 
# [...] 
# ============================================================================ 

use strict; 

## Validate the version of Perl 

BEGIN 
{ 
    die('Perl version 5.6.0 or greater is required') if ($] < 5.006); 
} 

## Version of the Net::SNMP module 

our $VERSION = v5.2.0; 

## Load our modules 

use Net::SNMP::Dispatcher(); 
use Net::SNMP::PDU qw(:ALL); 
use Net::SNMP::Security(); 
use Net::SNMP::Transport qw(:ports); 

## Handle importing/exporting of symbols 

use Exporter(); 

our @ISA = qw(Exporter); 

our @EXPORT = qw(
    INTEGER INTEGER32 OCTET_STRING OBJECT_IDENTIFIER IPADDRESS COUNTER 
    COUNTER32 GAUGE GAUGE32 UNSIGNED32 TIMETICKS OPAQUE COUNTER64 NOSUCHOBJECT 
    NOSUCHINSTANCE ENDOFMIBVIEW snmp_dispatcher HEX_STRING 
); 

our @EXPORT_OK = qw(snmp_event_loop oid_context_match); 

our %EXPORT_TAGS = (
    asn1  => [ 
     qw(INTEGER INTEGER32 OCTET_STRING NULL OBJECT_IDENTIFIER SEQUENCE 
      IPADDRESS COUNTER COUNTER32 GAUGE GAUGE32 UNSIGNED32 TIMETICKS 
      OPAQUE COUNTER64 NOSUCHOBJECT NOSUCHINSTANCE ENDOFMIBVIEW 
      GET_REQUEST GET_NEXT_REQUEST GET_RESPONSE SET_REQUEST TRAP 
      GET_BULK_REQUEST INFORM_REQUEST SNMPV2_TRAP REPORT HEX_STRING) 
    ], 
# [...] 
# no more changes in source code of Net::SNMP 
# [...] only changes in description of Net::SNMP 
# [documentation] ------------------------------------------------------------ 

=head1 EXPORTS 

The Net::SNMP module uses the F<Exporter> module to export useful constants 
and subroutines. These exportable symbols are defined below and follow the 
rules and conventions of the F<Exporter> module (see L<Exporter>). 

=over 

=item Default 

&snmp_dispatcher, INTEGER, INTEGER32, OCTET_STRING, OBJECT_IDENTIFIER, 
IPADDRESS, COUNTER, COUNTER32, GAUGE, GAUGE32, UNSIGNED32, TIMETICKS, 
OPAQUE, COUNTER64, NOSUCHOBJECT, NOSUCHINSTANCE, ENDOFMIBVIEW HEX_STRING 

=item Exportable 

&snmp_debug, &snmp_dispatcher, &snmp_type_ntop, &oid_base_match, &oid_lex_sort, 
&ticks_to_time, INTEGER, INTEGER32, OCTET_STRING, NULL, OBJECT_IDENTIFIER, 
SEQUENCE, IPADDRESS, COUNTER, COUNTER32, GAUGE, GAUGE32, UNSIGNED32, TIMETICKS, 
OPAQUE, COUNTER64, NOSUCHOBJECT, NOSUCHINSTANCE, ENDOFMIBVIEW, GET_REQUEST, 
GET_NEXT_REQUEST, GET_RESPONSE, SET_REQUEST, TRAP, GET_BULK_REQUEST, 
INFORM_REQUEST, SNMPV2_TRAP, REPORT, DEBUG_ALL, DEBUG_NONE, DEBUG_MESSAGE, 
DEBUG_TRANSPORT, DEBUG_DISPATCHER,DEBUG_PROCESSING, DEBUG_SECURITY, COLD_START, 
WARM_START, LINK_DOWN, LINK_UP,AUTHENTICATION_FAILURE, EGP_NEIGHBOR_LOSS, 
ENTERPRISE_SPECIFIC, SNMP_VERSION_1,SNMP_VERSION_2C, SNMP_VERSION_3, SNMP_PORT, 
SNMP_TRAP_PORT, TRANSLATE_NONE,TRANSLATE_OCTET_STRING, TRANSLATE_NULL, 
TRANSLATE_TIMETICKS, TRANSLATE_OPAQUE,TRANSLATE_NOSUCHOBJECT, 
TRANSLATE_NOSUCHINSTANCE, TRANSLATE_ENDOFMIBVIEW, TRANSLATE_UNSIGNED, 
TRANSLATE_ALL HEX_STRING 

=item Tags 

=over 

=item :asn1 

INTEGER, INTEGER32, OCTET_STRING, NULL, OBJECT_IDENTIFIER, SEQUENCE, 
IPADDRESS, COUNTER, COUNTER32, GAUGE, GAUGE32, UNSIGNED32, TIMETICKS, OPAQUE, 
COUNTER64, NOSUCHOBJECT, NOSUCHINSTANCE, ENDOFMIBVIEW, GET_REQUEST, 
GET_NEXT_REQUEST, GET_RESPONSE, SET_REQUEST, TRAP, GET_BULK_REQUEST, 
INFORM_REQUEST, SNMPV2_TRAP, REPORT, HEX_STRING 

=item :debug 

&snmp_debug, DEBUG_ALL, DEBUG_NONE, DEBUG_MESSAGE, DEBUG_TRANSPORT, 
DEBUG_DISPATCHER, DEBUG_PROCESSING, DEBUG_SECURITY 

=item :generictrap 

COLD_START, WARM_START, LINK_DOWN, LINK_UP, AUTHENTICATION_FAILURE, 
EGP_NEIGHBOR_LOSS, ENTERPRISE_SPECIFIC 

=item :snmp 

&snmp_debug, &snmp_dispatcher, &snmp_type_ntop, &oid_base_match, &oid_lex_sort, 
&ticks_to_time, SNMP_VERSION_1, SNMP_VERSION_2C, SNMP_VERSION_3, SNMP_PORT, 
SNMP_TRAP_PORT 

=item :translate 

TRANSLATE_NONE, TRANSLATE_OCTET_STRING, TRANSLATE_NULL, TRANSLATE_TIMETICKS, 
TRANSLATE_OPAQUE, TRANSLATE_NOSUCHOBJECT, TRANSLATE_NOSUCHINSTANCE, 
TRANSLATE_ENDOFMIBVIEW, TRANSLATE_UNSIGNED, TRANSLATE_ALL 

=item :ALL 

All of the above exportable items. 

=back 

=back 

=head1 EXAMPLES 
# [...] 

# ============================================================================ 
1; # [end Net::SNMP] 

的Net :: SNMP ::消息

# ============================================================================ 
package Net::SNMP::Message; 

# $Id: Message.pm,v 2.3 2005/10/20 14:17:01 dtown Rel $ 

# Object used to represent a SNMP message. 

# Copyright (c) 2001-2005 David M. Town <[email protected]> 
# All rights reserved. 

# This program is free software; you may redistribute it and/or modify it 
# under the same terms as Perl itself. 

# ============================================================================ 

use strict; 
use bytes; 

use Math::BigInt(); 

## Version of the Net::SNMP::Message module 

our $VERSION = v2.0.3; 

## Handle importing/exporting of symbols 

use Exporter(); 

our @ISA = qw(Exporter); 

our @EXPORT_OK = qw(TRUE FALSE); 

our %EXPORT_TAGS = (
    generictrap => [ 
     qw(COLD_START WARM_START LINK_DOWN LINK_UP AUTHENTICATION_FAILURE 
      EGP_NEIGHBOR_LOSS ENTERPRISE_SPECIFIC) 
    ], 
    msgFlags  => [ 
     qw(MSG_FLAGS_NOAUTHNOPRIV MSG_FLAGS_AUTH MSG_FLAGS_PRIV 
      MSG_FLAGS_REPORTABLE MSG_FLAGS_MASK) 
    ], 
    securityLevels => [ 
     qw(SECURITY_LEVEL_NOAUTHNOPRIV SECURITY_LEVEL_AUTHNOPRIV 
      SECURITY_LEVEL_AUTHPRIV) 
    ], 
    securityModels => [ 
     qw(SECURITY_MODEL_ANY SECURITY_MODEL_SNMPV1 SECURITY_MODEL_SNMPV2C 
      SECURITY_MODEL_USM) 
    ], 
    translate  => [ 
     qw(TRANSLATE_NONE TRANSLATE_OCTET_STRING TRANSLATE_NULL 
      TRANSLATE_TIMETICKS TRANSLATE_OPAQUE TRANSLATE_NOSUCHOBJECT 
      TRANSLATE_NOSUCHINSTANCE TRANSLATE_ENDOFMIBVIEW TRANSLATE_UNSIGNED 
      TRANSLATE_ALL) 
    ], 
    types   => [ 
     qw(INTEGER INTEGER32 OCTET_STRING NULL OBJECT_IDENTIFIER SEQUENCE 
      IPADDRESS COUNTER COUNTER32 GAUGE GAUGE32 UNSIGNED32 TIMETICKS 
      OPAQUE COUNTER64 NOSUCHOBJECT NOSUCHINSTANCE ENDOFMIBVIEW 
      GET_REQUEST GET_NEXT_REQUEST GET_RESPONSE SET_REQUEST TRAP 
      GET_BULK_REQUEST INFORM_REQUEST SNMPV2_TRAP REPORT HEX_STRING) 
    ], 
    utilities  => [ qw(asn1_ticks_to_time asn1_itoa) ], 
    versions  => [ qw(SNMP_VERSION_1 SNMP_VERSION_2C SNMP_VERSION_3) ] 
); 

Exporter::export_ok_tags( 
    qw(generictrap msgFlags securityLevels securityModels translate types 
     utilities versions) 
); 

$EXPORT_TAGS{ALL} = [ @EXPORT_OK ]; 

## ASN.1 Basic Encoding Rules type definitions 

sub INTEGER()     { 0x02 } # INTEGER 
sub INTEGER32()    { 0x02 } # Integer32   - SNMPv2c 
sub OCTET_STRING()    { 0x04 } # OCTET STRING 
sub HEX_STRING()    { 0xfe } # OCTET STRING in HEX # Work a Round 
# [...] 
# [public methods] ----------------------------------------------------------- 

sub new 
{ 
    my ($class, %argv) = @_; 

    # Create a new data structure for the object 
    my $this = bless { 
     '_buffer'  => '',    # Serialized message buffer 
     '_error'  => undef,   # Error message 
     '_index'  => 0,    # Buffer index 
     '_leading_dot' => FALSE,   # Prepend leading dot on OIDs 
     '_length'  => 0,    # Buffer length 
     '_security' => undef,   # Security Model object 
     '_translate' => TRANSLATE_NONE, # Translation mode 
     '_transport' => undef,   # Transport Layer object 
     '_version'  => SNMP_VERSION_1 # SNMP version 
    }, $class; 

    # Validate the passed arguments 

    foreach (keys %argv) { 

     if (/^-?callback$/i) { 
     $this->callback($argv{$_}); 
     } elsif (/^-?debug$/i) { 
     $this->debug($argv{$_}); 
     } elsif (/^-?leadingdot$/i) { 
     $this->leading_dot($argv{$_}); 
     } elsif (/^-?msgid$/i) { 
     $this->msg_id($argv{$_}); 
     } elsif (/^-?requestid$/i) { 
     $this->request_id($argv{$_}); 
     } elsif (/^-?security$/i) { 
     $this->security($argv{$_}); 
     } elsif (/^-?translate$/i) { 
     $this->translate($argv{$_}); 
     } elsif (/^-?transport$/i) { 
     $this->transport($argv{$_}); 
     } elsif (/^-?version$/i) { 
     $this->version($argv{$_}); 
     } else { 
     $this->_error("Invalid argument '%s'", $_); 
     } 

     if (defined($this->{_error})) { 
     return wantarray ? (undef, $this->{_error}) : undef; 
     } 

    } 

    return wantarray ? ($this, '') : $this; 
} 

{ 
    my $prepare_methods = { 
     INTEGER,   \&_prepare_integer, 
     OCTET_STRING,  \&_prepare_octet_string, 
     NULL,    \&_prepare_null, 
     OBJECT_IDENTIFIER, \&_prepare_object_identifier, 
     SEQUENCE,   \&_prepare_sequence, 
     IPADDRESS,   \&_prepare_ipaddress, 
     COUNTER,   \&_prepare_counter, 
     GAUGE,    \&_prepare_gauge, 
     TIMETICKS,   \&_prepare_timeticks, 
     OPAQUE,    \&_prepare_opaque, 
     COUNTER64,   \&_prepare_counter64, 
     NOSUCHOBJECT,  \&_prepare_nosuchobject, 
     NOSUCHINSTANCE,  \&_prepare_nosuchinstance, 
     ENDOFMIBVIEW,  \&_prepare_endofmibview, 
     GET_REQUEST,  \&_prepare_get_request, 
     GET_NEXT_REQUEST, \&_prepare_get_next_request, 
     GET_RESPONSE,  \&_prepare_get_response, 
     SET_REQUEST,  \&_prepare_set_request, 
     TRAP,    \&_prepare_trap, 
     GET_BULK_REQUEST, \&_prepare_get_bulk_request, 
     INFORM_REQUEST,  \&_prepare_inform_request, 
     SNMPV2_TRAP,  \&_prepare_v2_trap, 
     REPORT,    \&_prepare_report, 
     HEX_STRING,   \&_prepare_hex_string 
    }; 

    sub prepare 
    { 
#  my ($this, $type, $value) = @_; 

     return $_[0]->_error('ASN.1 type not defined') unless (@_ > 1); 
     return $_[0]->_error if defined($_[0]->{_error}); 

     if (exists($prepare_methods->{$_[1]})) { 
     $_[0]->${\$prepare_methods->{$_[1]}}($_[2]); 
     } else { 
     $_[0]->_error('Unknown ASN.1 type [%s]', $_[1]); 
     } 
    } 
} 

{ 
    my $process_methods = { 
     INTEGER,   \&_process_integer32, 
     OCTET_STRING,  \&_process_octet_string, 
     NULL,    \&_process_null, 
     OBJECT_IDENTIFIER, \&_process_object_identifier, 
     SEQUENCE,   \&_process_sequence, 
     IPADDRESS,   \&_process_ipaddress, 
     COUNTER,   \&_process_counter, 
     GAUGE,    \&_process_gauge, 
     TIMETICKS,   \&_process_timeticks, 
     OPAQUE,    \&_process_opaque, 
     COUNTER64,   \&_process_counter64, 
     NOSUCHOBJECT,  \&_process_nosuchobject, 
     NOSUCHINSTANCE,  \&_process_nosuchinstance, 
     ENDOFMIBVIEW,  \&_process_endofmibview, 
     GET_REQUEST,  \&_process_get_request, 
     GET_NEXT_REQUEST, \&_process_get_next_request, 
     GET_RESPONSE,  \&_process_get_response, 
     SET_REQUEST,  \&_process_set_request, 
     TRAP,    \&_process_trap, 
     GET_BULK_REQUEST, \&_process_get_bulk_request, 
     INFORM_REQUEST,  \&_process_inform_request, 
     SNMPV2_TRAP,  \&_process_v2_trap, 
     REPORT,    \&_process_report 
    }; 

    sub process 
    { 
#  my ($this, $expected, $found) = @_; 

     return $_[0]->_error if defined($_[0]->{_error}); 
     return $_[0]->_error unless defined(my $type = $_[0]->_buffer_get(1)); 

     $type = unpack('C', $type); 

     if (exists($process_methods->{$type})) { 
     if ((@_ >= 2) && (defined($_[1])) && ($type != $_[1])) { 
      return $_[0]->_error(
       'Expected %s, but found %s', asn1_itoa($_[1]), asn1_itoa($type) 
      ); 
     } 
     $_[2] = $type if (@_ == 3); 
     $_[0]->${\$process_methods->{$type}}($type); 
     } else { 
     $_[0]->_error('Unknown ASN.1 type [0x%02x]', $type); 
     } 
    } 
} 

# [...] 
# [private methods] ---------------------------------------------------------- 

# 
# Basic Encoding Rules (BER) prepare methods 
# 

sub _prepare_type_length 
{ 
# my ($this, $type, $value) = @_; 

    return $_[0]->_error('ASN.1 type not defined') unless defined($_[1]); 

    my $length = CORE::length($_[2]); 

    if ($length < 0x80) { 
     $_[0]->_buffer_put(pack('C2', $_[1], $length) . $_[2]); 
    } elsif ($length <= 0xff) { 
     $_[0]->_buffer_put(pack('C3', $_[1], 0x81, $length) . $_[2]); 
    } elsif ($length <= 0xffff) { 
     $_[0]->_buffer_put(pack('CCn', $_[1], 0x82, $length) . $_[2]); 
    } else { 
     $_[0]->_error('Unable to prepare ASN.1 length'); 
    } 
} 

sub _prepare_integer 
{ 
    my ($this, $value) = @_; 

    return $this->_error('INTEGER value not defined') unless defined($value); 

    if ($value !~ /^-?\d+$/) { 
     return $this->_error('Expected numeric INTEGER value'); 
    } 

    $this->_prepare_integer32(INTEGER, $value); 
} 

sub _prepare_unsigned32 
{ 
    my ($this, $type, $value) = @_; 

    if (!defined($value)) { 
     return $this->_error('%s value not defined', asn1_itoa($type)); 
    } 

    if ($value !~ /^\d+$/) { 
     return $this->_error(
     'Expected positive numeric %s value', asn1_itoa($type) 
    ); 
    } 

    $this->_prepare_integer32($type, $value); 
} 

sub _prepare_integer32 
{ 
    my ($this, $type, $value) = @_; 

    # Determine if the value is positive or negative 
    my $negative = ($value =~ /^-/); 

    # Check to see if the most significant bit is set, if it is we 
    # need to prefix the encoding with a zero byte. 

    my $size = 4;  # Assuming 4 byte integers 
    my $prefix = FALSE; 
    my $bytes = ''; 

    if ((($value & 0xff000000) & 0x80000000) && (!$negative)) { 
     $size++; 
     $prefix = TRUE; 
    } 

    # Remove occurances of nine consecutive ones (if negative) or zeros 
    # from the most significant end of the two's complement integer. 

    while ((((!($value & 0xff800000))) || 
      ((($value & 0xff800000) == 0xff800000) && ($negative))) && 
      ($size > 1)) 
    { 
     $size--; 
     $value <<= 8; 
    } 

    # Add a zero byte so the integer is decoded as a positive value 
    if ($prefix) { 
     $bytes = pack('x'); 
     $size--; 
    } 

    # Build the integer 
    while ($size-- > 0) { 
     $bytes .= pack('C*', (($value & 0xff000000) >> 24)); 
     $value <<= 8; 
    } 

    # Encode ASN.1 header 
    $this->_prepare_type_length($type, $bytes); 
} 

sub _prepare_octet_string 
{ 
    my ($this, $value) = @_; 

    if (!defined($value)) { 
     return $this->_error('OCTET STRING value not defined'); 
    } 

    $this->_prepare_type_length(OCTET_STRING, $value); 
} 

sub _prepare_hex_string 
{ 
    my ($this, $value) = @_; 

    if (!defined($value)) { 
     return $this->_error('HEX STRING value not defined'); 
    } 

    $this->_prepare_type_length(OCTET_STRING, pack("H*", $value)); 
} 

sub _prepare_null 
{ 
    $_[0]->_prepare_type_length(NULL, ''); 
} 

# [...] 

# 
# Abstract Syntax Notation One (ASN.1) utility functions 
# 

{ 
    my $types = { 
     INTEGER,   'INTEGER', 
     OCTET_STRING,  'OCTET STRING', 
     NULL,    'NULL', 
     OBJECT_IDENTIFIER, 'OBJECT IDENTIFIER', 
     SEQUENCE,   'SEQUENCE', 
     IPADDRESS,   'IpAddress', 
     COUNTER,   'Counter', 
     GAUGE,    'Gauge', 
     TIMETICKS,   'TimeTicks', 
     OPAQUE,    'Opaque', 
     COUNTER64,   'Counter64', 
     NOSUCHOBJECT,  'noSuchObject', 
     NOSUCHINSTANCE,  'noSuchInstance', 
     ENDOFMIBVIEW,  'endOfMibView', 
     GET_REQUEST,  'GetRequest-PDU', 
     GET_NEXT_REQUEST, 'GetNextRequest-PDU', 
     GET_RESPONSE,  'GetResponse-PDU', 
     SET_REQUEST,  'SetRequest-PDU', 
     TRAP,    'Trap-PDU', 
     GET_BULK_REQUEST, 'GetBulkRequest-PDU', 
     INFORM_REQUEST,  'InformRequest-PDU', 
     SNMPV2_TRAP,  'SNMPv2-Trap-PDU', 
     REPORT,    'Report-PDU', 
     HEX_STRING,   'HEX STRING' 
    }; 

    sub asn1_itoa 
    { 
     my ($type) = @_; 

     return '??' unless (@_ == 1); 

     if (exists($types->{$type})) { 
     $types->{$type}; 
     } else { 
     sprintf('?? [0x%02x]', $type); 
     } 
    } 
} 

sub asn1_ticks_to_time 
{ 
    my $ticks = shift || 0; 

    my $days = int($ticks/(24 * 60 * 60 * 100)); 
    $ticks %= (24 * 60 * 60 * 100); 

    my $hours = int($ticks/(60 * 60 * 100)); 
    $ticks %= (60 * 60 * 100); 

    my $minutes = int($ticks/(60 * 100)); 
    $ticks %= (60 * 100); 

    my $seconds = ($ticks/100); 

    if ($days != 0){ 
     sprintf('%d day%s, %02d:%02d:%05.02f', $days, 
     ($days == 1 ? '' : 's'), $hours, $minutes, $seconds); 
    } elsif ($hours != 0) { 
     sprintf('%d hour%s, %02d:%05.02f', $hours, 
     ($hours == 1 ? '' : 's'), $minutes, $seconds); 
    } elsif ($minutes != 0) { 
     sprintf('%d minute%s, %05.02f', $minutes, 
     ($minutes == 1 ? '' : 's'), $seconds); 
    } else { 
     sprintf('%04.02f second%s', $seconds, ($seconds == 1 ? '' : 's')); 
    } 
} 

# 
# Error handlers 
# 

#[...] 
# ============================================================================ 
1; # [end Net::SNMP::Message] 
1

似乎有一個字符\ X03您文件。這是ASCII中的「文本結尾」。它可能是您的.pl文件本身或由它加載的東西。

如果你看到這樣^C^B你的文件的開頭,將其刪除。否則,您可能必須使用十六進制編輯器。這令我

一兩件事,雖然是「PK」的提。難道你是在試圖執行一個pk-zip文件嗎?

+0

斯萬嗨,如果ü看看我的代碼ü可以很容易地找到在我的文件中沒有/ x03並且沒有pk-zipped文件,我的文件名是snmpget.pl。 – Gaurav 2012-07-06 12:01:40

+0

我看到的是在指定的位置似乎有一個'/'丟失(第1行,第3列)。難道它已經被'\ x03'取代了,它是不可打印的?你也許應該用十六進制編輯器來驗證它。 – Svante 2012-07-06 18:36:05