大网数据广告活动
PHP 语言与运行手册

SNMP::__construct · Creates SNMP instance representing session to remote SNMP agent

· 52CE 我爱测

SNMP::__construct — Creates SNMP instance representing session to remote SNMP agent

SNMP::__construct

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

SNMP::__construct — Creates SNMP instance representing session to remote SNMP agent

Description

public function SNMP::__construct(
    int $version,
    string $hostname,
    string $community,
    int $timeout = -1,
    int $retries = -1
)

Creates SNMP instance representing session to remote SNMP agent.

Parameters

version

SNMP protocol version: SNMP::VERSION_1, SNMP::VERSION_2C, SNMP::VERSION_3.

hostname

The SNMP agent. hostname may be suffixed with optional SNMP agent port after colon. IPv6 addresses must be enclosed in square brackets if used with port. If FQDN is used for hostname it will be resolved by PHP SNMP extension, not by Net-SNMP engine. Usage of IPv6 addresses when specifying FQDN may be forced by enclosing FQDN into square brackets. Here are some examples:

IPv4 with default port127.0.0.1
IPv6 with default port::1 or [::1]
IPv4 with specific port127.0.0.1:1161
IPv6 with specific port[::1]:1161
FQDN with default porthost.domain
FQDN with specific porthost.domain:1161
FQDN with default port, force usage of IPv6 address[host.domain]
FQDN with specific port, force usage of IPv6 address[host.domain]:1161
community

The community string, or, for SNMP::VERSION_3, the security name. The purpose of the community access string is SNMP version specific:

SNMP::VERSION_1 public for read-only permission or private for read-write
SNMP::VERSION_2C public for read-only permission or private for read-write
SNMP::VERSION_3 The security name, usually some kind of username.

SNMPv3 requires to configure security-related session parameters with the SNMP::setSecurity() method.

timeout

The number of microseconds until the first timeout.

retries

The number of retries in case timeout occurs.

Errors/Exceptions

SNMP::__construct() throws an exception when parameters count or types are wrong or unknown SNMP protocol version specified.

Examples

Example #1 Fetching the physical location of the host

<?php

$session = new SNMP(SNMP::VERSION_1, "127.0.0.1", "public");
$sysdescr = $session->get("sysDescr.0");
echo "$sysdescr\n";

?>

The above example will output something similar to:

STRING: Test server

See Also

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。