phpday 2027 - Call for Papers

snmp2_set

(PHP >= 5.2.0, PHP 7, PHP 8)

snmp2_set — Set the value of an SNMP object

Descrizione

function snmp2_set(
    string $hostname,
    string $community,
    array|string $object_id,
    array|string $type,
    array|string $value,
    int $timeout = -1,
    int $retries = -1
): bool

snmp2_set() is used to set the value of an SNMP object specified by the object_id.

Elenco dei parametri

hostname

The hostname of the SNMP agent (server).

community

The write community.

object_id

The SNMP object id.

type

Il MIB definisce il tipo di ciascun object id. Deve essere specificato come un singolo carattere della lista seguente.

tipi
=Il tipo viene preso dal MIB
iINTEGER
uINTEGER
sSTRING
xHEX STRING
dDECIMAL STRING
nNULLOBJ
oOBJID
tTIMETICKS
aIPADDRESS
bBITS

Se OPAQUE_SPECIAL_TYPES è stato definito durante la compilazione della libreria SNMP, sono validi anche i seguenti:

tipi
Uunsigned int64
Isigned int64
Ffloat
Ddouble

La maggior parte di questi userà l'ovvio tipo ASN.1 corrispondente. 's', 'x', 'd' e 'b' sono tutti modi diversi di specificare un valore OCTET STRING, e il tipo unsigned 'u' è usato anche per gestire i valori Gauge32.

Se i file MIB sono caricati nell'albero MIB con "snmp_read_mib" o specificandoli nella configurazione di libsnmp, '=' può essere usato come parametro type per tutti gli object id, poiché il tipo può allora essere letto automaticamente dal MIB.

Si noti che ci sono due modi per impostare una variabile di tipo BITS come ad es. "SYNTAX BITS {telnet(0), ftp(1), http(2), icmp(3), snmp(4), ssh(5), https(6)}":

  • Usando il tipo "b" e una lista di numeri di bit. Questo metodo non è raccomandato poiché una query GET per lo stesso OID restituirebbe ad es. 0xF8.

  • Usando il tipo "x" e un numero esadecimale ma senza(!) il consueto prefisso "0x".

Vedere la sezione degli esempi per maggiori dettagli.

value

The new value.

timeout

The number of microseconds until the first timeout.

retries

The number of times to retry if timeouts occur.

Valori restituiti

Restituisce true in caso di successo o false in caso di fallimento.

If the SNMP host rejects the data type, an E_WARNING message like "Warning: Error in packet. Reason: (badValue) The value given has the wrong type or length." is shown. If an unknown or invalid OID is specified the warning probably reads "Could not add variable".

Errori/Eccezioni

Throws a ValueError if object_id is an empty array.

Throws a TypeError if object_id is a string and type or value is an array. Throws a ValueError if object_id is a string and type is not a single character, or if type is an array and the type of an object ID is not a single character.

As of PHP 8.5.0, throws a ValueError if hostname or community contains null bytes, if hostname is 128 bytes or longer or contains a port that is not between 0 and 65535, if community is empty, or if timeout or retries is lower than -1 or too large.

Log delle modifiche

Versione Descrizione
8.5.0 Now throws a ValueError when hostname or community contains null bytes, when the hostname length is equal to or greater than 128 bytes, when community is empty, when the port is negative or greater than 65535, or when the timeout or retries values are lower than -1 or too large.
8.0.0 Now throws a ValueError if object_id is an empty array. Previously, an E_WARNING was raised and false was returned.
8.0.0 Now throws a TypeError if object_id is a string and type or value is an array, and a ValueError if a type is not a single character. Previously, an E_WARNING was raised and false was returned.

Esempi

Example #1 Using snmp2_set()

<?php
  snmp2_set("localhost", "public", "IF-MIB::ifAlias.3", "s", "foo");
?>

Example #2 Using snmp2_set() for setting BITS SNMP object id

<?php
  snmp2_set("localhost", "public", 'FOO-MIB::bar.42', 'b', '0 1 2 3 4');
// or
  snmp2_set("localhost", "public", 'FOO-MIB::bar.42', 'x', 'F0');
?>

Vedere anche:

+add a note

User Contributed Notes 1 note

up
0
jhuebel at gmail dot com
15 years ago
The "type" parameter must be one of the following, depending on the type of variable to set on the SNMP host:

i    INTEGER
u    unsigned INTEGER
t    TIMETICKS
a    IPADDRESS
o    OBJID
s    STRING
x    HEX STRING
d    DECIMAL STRING

If OPAQUE_SPECIAL_TYPES was defined while compiling the SNMP library, the following are also valid:

U    unsigned int64
I    signed int64
F    float
D    double
To Top