Library snmp

SNMP library.

Authors:

  • Patrik Karlsson <patrik@cqure.net>
  • Gioacchino Mazzurco <gmazzurco89@gmail.com>

Copyright © Same as Nmap--See https://nmap.org/book/man-legal.html

Source: https://svn.nmap.org/nmap/nselib/snmp.lua

Script Arguments

snmp.version

The SNMP protocol version. Use "v1" or 0 for SNMPv1 (default) and "v2c" or 1 for SNMPv2c.

Functions

buildGetNextRequest (options, ...)

Create an SNMP Get Next Request PDU.

buildGetRequest (options, ...)

Create an SNMP Get Request PDU.

buildGetResponse (options, oid, value)

Create an SNMP Get Response PDU.

buildPacket (PDU, version, commStr)

Create an SNMP packet.

buildSetRequest (options, oid, value)

Create an SNMP Set Request PDU.

buildTrap (enterpriseOid, agentIp, genTrap, specTrap, timeStamp)

Create an SNMP Trap PDU.

connect (self)

Connect to the server For UDP ports, this doesn't send any packets, but it creates the socket and locks in the timeout.

decode (encStr, pos)

Decodes an SNMP packet or a part of it according to ASN.1 basic encoding rules.

encode (val)

Encodes a given value according to ASN.1 basic encoding rules for SNMP packet creation.

fetchResponseValues (resp)

Fetches values from a SNMP response.

get (self, options, ...)

Sends an SNMP Get request

getnext (self, options, ...)

Sends an SNMP Get Next request

ip2str (ip)

Transforms a table representing an IP to a string.

new (self, host, port, community, options)

Creates a new Helper instance

oid2str (oid)

Transforms a table representing an object identifier to a string.

request (self, message)

Communications helper Sends an SNMP message and receives a response.

set (self, options, oid, setparam, value)

Sends an SNMP Set request

str2ip (ipStr)

Transforms a string into an IP table.

str2oid (oidStr)

Transforms a string into an object identifier table.

walk (self, base_oid)

Walks the MIB Tree

Tables

snmp.options

SNMP options table

Functions

buildGetNextRequest (options, ...)

Create an SNMP Get Next Request PDU.

Parameters

options
SNMP options table
...
Object identifiers to be queried.

Return value:

Table representing PDU.

See also:

buildGetRequest (options, ...)

Create an SNMP Get Request PDU.

Parameters

options
SNMP options table
...
Object identifiers to be queried.

Return value:

Table representing PDU.

See also:

buildGetResponse (options, oid, value)

Create an SNMP Get Response PDU.

Takes one OID/value pair or an already prepared table.

Parameters

options
SNMP options table
oid
Object identifiers of object to be sent back.
value
If given a table, use the table instead of OID/value pair.

Return value:

Table representing PDU.

See also:

buildPacket (PDU, version, commStr)

Create an SNMP packet.

Parameters

PDU
SNMP Protocol Data Unit to be encapsulated in the packet.
version
SNMP version; defaults to script argument snmp.version
commStr
community string.
buildSetRequest (options, oid, value)

Create an SNMP Set Request PDU.

Takes one OID/value pair or an already prepared table.

Parameters

options
SNMP options table
oid
Object identifiers of object to be set.
value
To which value object should be set. If given a table, use the table instead of OID/value pair.

Return value:

Table representing PDU.

See also:

buildTrap (enterpriseOid, agentIp, genTrap, specTrap, timeStamp)

Create an SNMP Trap PDU.

Parameters

enterpriseOid
 
agentIp
 
genTrap
 
specTrap
 
timeStamp
 

Return value:

Table representing PDU
connect (self)

Connect to the server For UDP ports, this doesn't send any packets, but it creates the socket and locks in the timeout.

Parameters

self
 

Return value:

status true on success, false on failure
decode (encStr, pos)

Decodes an SNMP packet or a part of it according to ASN.1 basic encoding rules.

Parameters

encStr
Encoded string.
pos
Current position in the string.

Return values:

  1. The decoded value(s).
  2. The position after decoding
encode (val)

Encodes a given value according to ASN.1 basic encoding rules for SNMP packet creation.

Parameters

val
Value to be encoded.

Return value:

Encoded value.
fetchResponseValues (resp)

Fetches values from a SNMP response.

Parameters

resp
SNMP Response (will be decoded if necessary).

Return value:

Table with all decoded responses and their OIDs.
get (self, options, ...)

Sends an SNMP Get request

Parameters

self
 
options
SNMP options table
...
Object identifiers to be queried.

Return values:

  1. status False if error, true otherwise
  2. Table with all decoded responses and their OIDs.

See also:

getnext (self, options, ...)

Sends an SNMP Get Next request

Parameters

self
 
options
SNMP options table
...
Object identifiers to be queried.

Return values:

  1. status False if error, true otherwise
  2. Table with all decoded responses and their OIDs.

See also:

ip2str (ip)

Transforms a table representing an IP to a string.

Parameters

ip
IP table.

Return value:

IP string.
new (self, host, port, community, options)

Creates a new Helper instance

Parameters

self
 
host
string containing the host name or ip
port
table containing the port details to connect to
community
string containing SNMP community
options
A table with appropriate options: * timeout - the timeout in milliseconds (Default: 5000) * version - the SNMP version; defaults to script argument snmp.version.

Return value:

o a new instance of Helper
oid2str (oid)

Transforms a table representing an object identifier to a string.

Parameters

oid
Object identifier table.

Return value:

OID string.
request (self, message)

Communications helper Sends an SNMP message and receives a response.

Parameters

self
 
message
the result of one of the build*Request functions

Return values:

  1. status False if there was an error, true otherwise.
  2. response The raw response read from the socket.
set (self, options, oid, setparam, value)

Sends an SNMP Set request

Parameters

self
 
options
SNMP options table
oid
Object identifiers of object to be set.
setparam
 
value
To which value object should be set. If given a table, use the table instead of OID/value pair.

Return values:

  1. status False if error, true otherwise
  2. Table with all decoded responses and their OIDs.

See also:

str2ip (ipStr)

Transforms a string into an IP table.

Parameters

ipStr
IP as string.

Return value:

Table representing IP.
str2oid (oidStr)

Transforms a string into an object identifier table.

Parameters

oidStr
Object identifier as string, for example "1.3.6.1.2.1.1.1.0".

Return value:

Table representing OID.
walk (self, base_oid)

Walks the MIB Tree

Parameters

self
 
base_oid
string containing the base object ID to walk

Return values:

  1. status true on success, false on failure
  2. table containing oid and value

Tables

snmp.options

SNMP options table

Fields

reqId
Request ID.
err
Error.
errIdx
Error index.