PHP 8.4.2 Released!

snmpset

(PHP 4, PHP 5, PHP 7, PHP 8)

snmpset设置 SNMP 对象的值

说明

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

snmpset() 用于通过 object_id 设置 SNMP 对象的值。

参数

hostname

SNMP 代理(服务器)的主机名。

community

The write community.

object_id

SNMP 对象 ID。

type

MIB 定义了各个对象 id 的类型。必须是下面列出的单个字符之一。

类型
=MIB类型
iINTEGER
uINTEGER
sSTRING
xHEX STRING
dDECIMAL STRING
nNULLOBJ
oOBJID
tTIMETICKS
aIPADDRESS
bBITS

如果在编译 SNMP 库时定义了 OPAQUE_SPECIAL_TYPES,那么下列值是合法的:

types
Uunsigned int64
Isigned int64
Ffloat
Ddouble

这些值大都会使用与 ASN.1 相符的类型。's','x','d' 以及 'b' 都是指定一个八字节字符串值的方式。并且 'u' 无符号类型也可用于处理 Gauge32 值。

如果 MIB 文件是用 "snmp_read_mib" 或者通过在 libsnmp config 中指定而加载入 MIB 树时, '=' 可以被用作为所有对象的 type 参数,因为 type 可以被自动从 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

新值。

timeout

第一次超时前的微秒数。

retries

发生超时时重试的次数。

返回值

成功时返回 true, 或者在失败时返回 false

如果 SNMP 主机拒绝该数据类型,则会出现一条 E_WARNING 消息,例如显示“Warning: Error in packet. Reason:(badValue)The value given has the wrong type or length.”。如果指定了未知或无效的 OID,警告可能会显示为“Could not add variable”。

示例

示例 #1 使用 snmpset()

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

示例 #2 使用 snmpset() 设置 BITS SNMP 对象 ID

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

参见

添加备注

用户贡献的备注 3 notes

up
0
ch at lathspell dot de
14 years ago
Note that there are two ways to set a variable of the type BITS like e.g.:
SYNTAX BITS {telnet(0), ftp(1), http(2), icmp(3), snmp(4), ssh(5), https(6)}

1. Using type "b" and a list of bit numbers like:
snmpset('FOO-MIB::bar.42', 'b', '0 1 2 3 4');
with the disadvantage that the success is not easily verifyable as an snmpget() for the same OID would return e.g. 0xF8.

2. Using type "x" and a hex number but without(!) the usual "0x" prefix:
snmpset('FOO-MIB::bar.42', 'x', 'F0');
up
0
deivis dot jakstas at gmail dot com
17 years ago
If you setting hex values correct format is:
snmpset($source_ip,$community,"$oid","x","10 10 10 10");
up
0
slawrance at technologist dot com
25 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
n NULLOBJ
b BITS

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

As an example, using "i" would set an integer, and "s" would set a string. If the SNMP host rejects the data type, you might get the following message: "Warning: Error in packet. Reason: (badValue) The value given has the wrong type or length."

If you specify an unknown or invalid OID, you might get a "Could not add variable" message. When specifying an absolute OID (one that is already resolved) that is completely numeric, prepend it with a period. For example, an OID that could enable/disable Ethernet ports on an Asante hub might be "1.3.6.1.2.1.22.1.3.1.1.3.6.4.0", but you would need to use ".1.3.6.1.2.1.22.1.3.1.1.3.6.4.0" in the OID parameter so that the SNMP library won't try to resolve an already resolved OID. Friendly, unresolved OIDs do not need the period prepended, such as "system.SysContact.0"
To Top