array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'uk', ), 'this' => array ( 0 => 'function.snmp2-set.php', 1 => 'snmp2_set', 2 => 'Set the value of an SNMP object', ), 'up' => array ( 0 => 'ref.snmp.php', 1 => 'SNMP Функції', ), 'prev' => array ( 0 => 'function.snmp2-real-walk.php', 1 => 'snmp2_real_walk', ), 'next' => array ( 0 => 'function.snmp2-walk.php', 1 => 'snmp2_walk', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/snmp/functions/snmp2-set.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>

snmp2_set

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

snmp2_setSet the value of an SNMP object

Опис

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.

Параметри

hostname
The hostname of the SNMP agent (server).
community
The write community.
object_id
The SNMP object id.
type

MIB дає означення типу для ID об'єкта. Тип можна вказувати одним з символів:

types
=Тип, взятий з MIB
iINTEGER
uINTEGER
sSTRING
xHEX STRING
dDECIMAL STRING
nNULLOBJ
oOBJID
tTIMETICKS
aIPADDRESS
bBITS

Якщо під час компіляції бібліотеки SNMP задано OPAQUE_SPECIAL_TYPES, будуть доступні наступні типи:

types
Uбеззнаковий int64
Iзнаковий int64
Fдесятковий дріб (float)
Dдесятковий дріб подвійної точності (double)

Більшість з них мають зрозумілий ASN.1-тип. "s", "x", "d" та "b" вказують на один тип — OCTET STRING, а беззнаковий тип "u" використовується для обробки значень Gauge32.

Якщо MIB-файли підключені до дерева MIB, бо вказані в налаштуваннях libsnmp або за допомогою "snmp_read_mib", то потрібно задавати параметр type зі значенням "=" для усіх ID об'єктів, оскільки тип можна автоматично зчитувати з MIB.

Варто зазначити, що є два способи задати значення змінній, яка має тип BITS, як от "SYNTAX BITS {telnet(0), ftp(1), http(2), icmp(3), snmp(4), ssh(5), https(6)}":

  • Використавши тип "b" та список бітових чисел. Цей спосіб не рекомендований, оскільки на GET-запит для однакових OID може бути відповідь, напр., 0xF8.
  • Використавши тип "x" та шістнадцяткове число, проте без(!) звичного префікса "0x".

Докладніше це описано у параграфі з прикладами.

value
The new value.
timeout
The number of microseconds until the first timeout.
retries
The number of times to retry if timeouts occur.

Значення, що повертаються

Повертає true у разі успіху або false в разі помилки.

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".

Журнал змін

Версія Опис
8.5.0 Now throws a ValueError when the hostname length is equal to or greater than 128 bytes, when the port is negative or greater than 65535, or when the timeout or retries values are lower than -1 or too large.

Приклади

Приклад #1 Using snmp2_set()

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

Приклад #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');
?>

Прогляньте також