wapsnmp

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

README

WapSnmp : SNMP client for golang

Currently supported operations:

  • SNMP v1/v2c/v3 trap receiver with V3 EngineID auto discovery
  • SNMP v1/v2c Get, GetMultiple, GetNext, GetBulk, Walk
  • SNMP V3 Get, Set, Walk, GetNext, GetMultiple, GetBulk

Compile

make

This will compile the following binaries:

  • goget : get single SNMP mib using SNMP v3
  • gowalk : walk SNMP mibs using SNMP v3
  • trapd : this program is able to receive SNMP v2 and v3 traps (you need to configure users for SNMP v3 traps)

You can run "go test" to perform unit test.

Using the code

  • The *_test.go files provide good examples of how to use these functions
  • files under utils/ contain the main entry to the utility program. Then look at example.go and snmp.go to see how it works.

Not supported yet:

  • SNMP Informs receiver

Documentation

Overview

Package wapsnmp provides an SNMP query library.

Index

Constants

View Source
const (
	SNMP_AES  string = "AES"
	SNMP_DES  string = "DES"
	SNMP_SHA1 string = "SHA1"
	SNMP_MD5  string = "MD5"
)

Variables

This section is empty.

Functions

func DecodeCounter64

func DecodeCounter64(toparse []byte) (uint64, error)

func DecodeIPAddress

func DecodeIPAddress(toparse []byte) (string, error)

func DecodeInt64

func DecodeInt64(bytes []byte) (ret int64, err error)

DecodeInt64 treats the given bytes as a big-endian, signed integer and returns the result.

func DecodeInteger

func DecodeInteger(toparse []byte) (int, error)

DecodeInteger decodes an integer. This does not handle signed value.

Will error out if it's longer than 64 bits.

func DecodeLength

func DecodeLength(toparse []byte) (int, int, error)

DecodeLength returns the length and the length of the length or an error.

Caveats: Does not support indefinite length. Couldn't find any
SNMP packet dump actually using that.

func DecodeSequence

func DecodeSequence(toparse []byte) ([]interface{}, error)

DecodeSequence decodes BER binary data into *[]interface{}.

func DecodeTimeticks

func DecodeTimeticks(toparse []byte) (string, error)

func DecryptAESCFB

func DecryptAESCFB(dst, src, key, iv []byte) error

func DecryptDESCBC

func DecryptDESCBC(dst, src, key, iv []byte) error

func DoGetTableTest

func DoGetTableTest(target string)

func DoGetTest

func DoGetTest(target string)

func DoGetTestV3

func DoGetTestV3(target string, oidstr, username, authAlg, authKey, privAlg, privKey string)

func DoWalkTest

func DoWalkTest(target string)

func DoWalkTestV3

func DoWalkTestV3(target string, oidstr, username, authAlg, authKey, privAlg, privKey string)

func EncodeInteger

func EncodeInteger(toEncode int) []byte

EncodeInteger encodes an integer to BER format.

func EncodeLength

func EncodeLength(length int) []byte

EncodeLength encodes an integer value as a BER compliant length value.

func EncodeSequence

func EncodeSequence(toEncode []interface{}) ([]byte, error)

EncodeSequence will encode an []interface{} into an SNMP bytestream.

func EncodeUInteger32

func EncodeUInteger32(toEncode uint32) []byte

EncodeUInteger32 encodes an Unsigned32 integer to BER format.

func EncryptAESCFB

func EncryptAESCFB(dst, src, key, iv []byte) error

func EncryptDESCBC

func EncryptDESCBC(dst, src, key, iv []byte) error

func IsStringAsciiPrintable

func IsStringAsciiPrintable(s string) bool

IsStringAsciiPrintable checks if the given string is ASCII and is printable form. Returns boolean value

func NewUdpStub

func NewUdpStub(t *testing.T) *udpStub

NewUdpStub creates a new udpStub.

Types

type BERType

type BERType uint8

Constants for the Type of the TLV field.

const (
	AsnBoolean     BERType = 0x01
	AsnInteger     BERType = 0x02
	AsnBitStr      BERType = 0x03
	AsnOctetStr    BERType = 0x04
	AsnNull        BERType = 0x05
	AsnObjectID    BERType = 0x06
	AsnSequence    BERType = 0x10
	AsnSet         BERType = 0x11
	AsnUniversal   BERType = 0x00
	AsnApplication BERType = 0x40
	AsnContext     BERType = 0x80
	AsnPrivate     BERType = 0xC0
	AsnPrimitive   BERType = 0x00
	AsnConstructor BERType = 0x20

	AsnLongLen     BERType = 0x80
	AsnExtensionID BERType = 0x1F
	AsnBit8        BERType = 0x80

	Integer     BERType = AsnUniversal | 0x02
	Integer32   BERType = AsnUniversal | 0x02
	Bitstring   BERType = AsnUniversal | 0x03
	Octetstring BERType = AsnUniversal | 0x04
	Null        BERType = AsnUniversal | 0x05
	UOid        BERType = AsnUniversal | 0x06
	Sequence    BERType = AsnConstructor | 0x10

	Ipaddress  BERType = AsnApplication | 0x00
	Counter    BERType = AsnApplication | 0x01
	Counter32  BERType = AsnApplication | 0x01
	Gauge      BERType = AsnApplication | 0x02
	Gauge32    BERType = AsnApplication | 0x02
	Timeticks  BERType = AsnApplication | 0x03
	Opaque     BERType = AsnApplication | 0x04
	Counter64  BERType = AsnApplication | 0x06
	Uinteger32 BERType = AsnApplication | 0x07

	AsnGetRequest     BERType = 0xa0
	AsnGetNextRequest BERType = 0xa1
	AsnGetResponse    BERType = 0xa2
	AsnSetRequest     BERType = 0xa3
	AsnTrap           BERType = 0xa4
	AsnGetBulkRequest BERType = 0xa5
	AsnInform         BERType = 0xa6
	AsnTrap2          BERType = 0xa7
	AsnReport         BERType = 0xa8

	NoSuchObject   BERType = 0x80
	NoSuchInstance BERType = 0x81
	EndOfMibView   BERType = 0x82
)

Constants for the different types of the TLV fields.

type IPAddress

type IPAddress []byte

The SNMP object identifier type.

func ParseIPv4

func ParseIPv4(ip string) IPAddress

type Oid

type Oid []int

The SNMP object identifier type.

func DecodeOid

func DecodeOid(raw []byte) (*Oid, error)

DecodeOid decodes a ASN.1 BER raw oid into an Oid instance.

func MustParseOid

func MustParseOid(o string) Oid

MustParseOid parses a string oid to an Oid instance. Panics on error.

func ParseOid

func ParseOid(oid string) (Oid, error)

ParseOid a text format oid into an Oid instance.

func (Oid) Copy

func (o Oid) Copy() Oid

Copy copies an oid into a new object instance.

func (Oid) Encode

func (o Oid) Encode() ([]byte, error)

Encode encodes the oid into an ASN.1 BER byte array.

func (Oid) String

func (o Oid) String() string

String returns the string representation for this oid object.

func (Oid) Within

func (o Oid) Within(other Oid) bool
Within determines if an oid has this oid instance as a prefix.

E.g. MustParseOid("1.2.3").Within("1.2") => true.

type SNMPError

type SNMPError uint8 // SNMPError is the type for standard SNMP errors.
const (
	NoError             SNMPError = iota // No error occurred. This code is also used in all request PDUs, since they have no error status to report.
	TooBig                               // The size of the Response-PDU would be too large to transport.
	NoSuchName                           // The name of a requested object was not found.
	BadValue                             // A value in the request didn't match the structure that the recipient of the request had for the object. For example, an object in the request was specified with an incorrect length or type.
	ReadOnly                             // An attempt was made to set a variable that has an Access value indicating that it is read-only.
	GenErr                               // An error occurred other than one indicated by a more specific error code in this table.
	NoAccess                             // Access was denied to the object for security reasons.
	WrongType                            // The object type in a variable binding is incorrect for the object.
	WrongLength                          // A variable binding specifies a length incorrect for the object.
	WrongEncoding                        // A variable binding specifies an encoding incorrect for the object.
	WrongValue                           // The value given in a variable binding is not possible for the object.
	NoCreation                           // A specified variable does not exist and cannot be created.
	InconsistentValue                    // A variable binding specifies a value that could be held by the variable but cannot be assigned to it at this time.
	ResourceUnavailable                  // An attempt to set a variable required a resource that is not available.
	CommitFailed                         // An attempt to set a particular variable failed.
	UndoFailed                           // An attempt to set a particular variable as part of a group of variables failed, and the attempt to then undo the setting of other variables was not successful.
	AuthorizationError                   // A problem occurred in authorization.
	NotWritable                          // The variable cannot be written or created.
	InconsistentName                     // The name in a variable binding specifies a variable that does not exist.
)

SNMP Errors

func (SNMPError) String

func (i SNMPError) String() string

type SNMPVersion

type SNMPVersion uint8

Type to indicate which SNMP version is in use.

const (
	SNMPv1  SNMPVersion = 0
	SNMPv2c SNMPVersion = 1
	SNMPv3  SNMPVersion = 3
)

List the supported snmp versions.

type SnmpV3MsgFlags

type SnmpV3MsgFlags uint8

SnmpV3MsgFlags contains various message flags to describe Authentication, Privacy, and whether a report PDU must be sent.

const (
	NoAuthNoPriv   SnmpV3MsgFlags = 0x0 // No authentication, and no privacy
	AuthNoPriv     SnmpV3MsgFlags = 0x1 // Authentication and no privacy
	AuthPriv       SnmpV3MsgFlags = 0x3 // Authentication and privacy
	Reportable     SnmpV3MsgFlags = 0x4 // Report PDU must be sent.
	AuthPrivReport SnmpV3MsgFlags = 0x7 //Authentication and privacy + report PDU
)

Possible values of SnmpV3MsgFlags

type SnmpV3SecurityModel

type SnmpV3SecurityModel int

SnmpV3SecurityModel describes the security model used by a SnmpV3 connection

const (
	UserSecurityModel SnmpV3SecurityModel = 0x03
)

UserSecurityModel is the only SnmpV3SecurityModel currently implemented.

type V3user

type V3user struct {
	User    string
	AuthAlg string //MD5 or SHA1
	AuthPwd string
	PrivAlg string //AES or DES
	PrivPwd string
}

type WapSNMP

type WapSNMP struct {
	Target    string      // Target device for these SNMP events.
	Community string      // Community to use to contact the device.
	Version   SNMPVersion // SNMPVersion to encode in the packets.

	//SNMP V3 variables
	User    string
	AuthAlg string //MD5 or SHA1
	AuthPwd string
	PrivAlg string //AES or DES
	PrivPwd string

	MessageFlags SnmpV3MsgFlags
	//V3 temp variables
	AuthKey string
	PrivKey string

	Trapusers []V3user
	// contains filtered or unexported fields
}

The object type that lets you do SNMP requests.

func NewWapSNMP

func NewWapSNMP(target, community string, version SNMPVersion, timeout time.Duration, retries int) (*WapSNMP, error)

NewWapSNMP creates a new WapSNMP object. Opens a udp connection to the device that will be used for the SNMP packets.

func NewWapSNMPOnConn

func NewWapSNMPOnConn(target, community string, version SNMPVersion, timeout time.Duration, retries int, conn net.Conn) *WapSNMP
NewWapSNMPOnConn creates a new WapSNMP object from an existing net.Conn.

It does not check if the provided target is valid.

func NewWapSNMPv3

func NewWapSNMPv3(w *WapSNMP, timeout time.Duration, retries int) (*WapSNMP, error)

func (WapSNMP) Close

func (w WapSNMP) Close() error

Close the net.conn in WapSNMP.

func (*WapSNMP) Discover

func (w *WapSNMP) Discover() error

SNMP V3 requires a discover packet being sent before a request being sent,

so that agent's engineID and other parameters can be automatically detected

func (WapSNMP) Get

func (w WapSNMP) Get(oid Oid) (interface{}, error)

Get sends an SNMP get request requesting the value for an oid.

func (WapSNMP) GetBulk

func (w WapSNMP) GetBulk(oid Oid, maxRepetitions int) (map[string]interface{}, *orderedmap.OrderedMap[string, interface{}], error)

GetBulk is semantically the same as maxRepetitions getnext requests, but in a single GETBULK SNMP packet.

Caveat: many devices will silently drop GETBULK requests for more than some number of maxrepetitions, if
it doesn't work, try with a lower value and/or use GetTable.

func (*WapSNMP) GetBulkV3

func (w *WapSNMP) GetBulkV3(oid Oid, maxRepetitions int) (map[string]interface{}, *orderedmap.OrderedMap[string, interface{}], error)

GetBulk snmpv3 request.

func (WapSNMP) GetMultiple

func (w WapSNMP) GetMultiple(oids []Oid) (map[string]interface{}, error)

GetMultiple issues a single GET SNMP request requesting multiple values

func (*WapSNMP) GetMultipleV3

func (w *WapSNMP) GetMultipleV3(oids []Oid) (map[string]interface{}, error)

A function that does GetMultiple for SNMP V3

func (WapSNMP) GetNext

func (w WapSNMP) GetNext(oid Oid) (string, interface{}, error)

GetNext issues a GETNEXT SNMP request.

func (*WapSNMP) GetNextV3

func (w *WapSNMP) GetNextV3(oid Oid) (string, interface{}, error)

GetNext issues a GETNEXT SNMP request.

func (WapSNMP) GetTable

func (w WapSNMP) GetTable(oid Oid) (*orderedmap.OrderedMap[string, interface{}], error)

GetTable efficiently gets an entire table from an SNMP agent. Uses GETBULK requests to go fast.

func (*WapSNMP) GetV3

func (w *WapSNMP) GetV3(oid Oid) (interface{}, error)

Get

func (WapSNMP) ParseTrap

func (w WapSNMP) ParseTrap(response []byte) error

ParseTrap parses a received SNMP trap and returns a map of oid to objects

func (WapSNMP) Set

func (w WapSNMP) Set(oid Oid, value interface{}) (interface{}, error)

Set sends an SNMP set request to change the value associated with an oid.

func (*WapSNMP) SetV3

func (w *WapSNMP) SetV3(oid Oid, value interface{}) (interface{}, error)

SetV3 sends an SNMP V3 set request to change the value associated with an oid.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL