2024 日本 PHP 研討會

snmprealwalk

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

snmprealwalk 傳回所有物件,包含它們在指定物件 ID 下的各自物件 ID

說明

snmprealwalk(
    字串 $hostname,
    字串 $community,
    陣列|字串 $object_id,
    整數 $timeout = -1,
    整數 $retries = -1
): 陣列|false

snmprealwalk() 函式用於從 object_id 開始遍歷多個 SNMP 物件,並不僅返回其值,還返回其物件 ID。

參數

hostname

SNMP 代理程式(伺服器)的主機名稱。

community

讀取社群。

object_id

位於所需 SNMP 物件 ID 之前的 SNMP 物件 ID。

timeout

第一次逾時之前的微秒數。

retries

發生逾時時重試的次數。

返回值

成功時返回包含 SNMP 物件 ID 及其值的關聯陣列,錯誤時返回 false。如果發生錯誤,會顯示 E_WARNING 訊息。

範例

範例 #1 使用 snmprealwalk()

<?php
print_r
(snmprealwalk("localhost", "public", "IF-MIB::ifName"));
?>

以上程式碼將輸出類似以下內容

Array
      (
      [IF-MIB::ifName.1] => STRING: lo
      [IF-MIB::ifName.2] => STRING: eth0
      [IF-MIB::ifName.3] => STRING: eth2
      [IF-MIB::ifName.4] => STRING: sit0
      [IF-MIB::ifName.5] => STRING: sixxs
    )

另請參閱

  • snmpwalk() - 從代理程式擷取所有 SNMP 物件

新增註釋

使用者貢獻的註釋 7 則註釋

scot at indievisible dot org
18 年前
由於 PHP 沒有類似 snmptable 的好用函式... 以下是一個對我來說可用的快速且簡陋的技巧。適用於完整和稀疏的表格。範例 OID 適用於路由 (完整) 和介面 (通常是稀疏的) 表格。

<?php
$a
= snmptable("10.1.1.1", "public", ".1.3.6.1.2.1.4.21") or die("error");
print_r($a);
$a = snmptable("10.1.1.1", "public", ".1.3.6.1.2.1.2.2") or die("error");
print_r($a);

function
snmptable($host, $community, $oid) {
// TODO: get original state and restore at bottom
snmp_set_oid_numeric_print(TRUE);
snmp_set_quick_print(TRUE);
snmp_set_enum_print(TRUE);

$retval = array();
$raw = snmprealwalk($host, $community, $oid) or die("snmptable: unable to walk OID $oid");

$prefix_length = 0;

foreach (
$raw as $key => $value) {
if (
$prefix_length == 0) {
// don't just use $oid's length since it may be non-numeric
$prefix_elements = count(explode('.',$oid));
$tmp = '.' . strtok($key, '.');
while (
$prefix_elements > 1) {
$tmp .= '.' . strtok('.');
$prefix_elements--;
}
$tmp .= '.';
$prefix_length = strlen($tmp);
}
$key = substr($key, $prefix_length);
$index = explode('.', $key, 2);
isset(
$retval[$index[1]]) or $retval[$index[1]] = array();
isset(
$firstrow) or $firstrow = $index[1];
$retval[$index[1]][$index[0]] = $value;
}

// check for holes in the table and fill them in
foreach ($retval[$firstrow] as $key => $tmp) {
foreach(
$retval as $check => $tmp2) {
if (! isset(
$retval[$check][$key])) {
$retval[$check][$key] = '';
}
}
}

return(
$retval);
}
?>
匿名
16 年前
要檢查是否有任何找到的結果,您必須使用 empty() 函式。count() 函式始終返回數字 1 或更大。

$walk_result = snmprealwalk($machine_ip, $community, $snmpcodes['interface_names']);

if (empty($walk_result)) {
print "找不到網路介面。<br>\n";
exit(0);
}
Stephen Cope
20 年前
以下是如何找到機器上的正常運行時間和使用者人數的方法。(請注意,正常運行時間是 snmpd 常駐程式的正常運行時間,應該相當接近主機的正常運行時間。)

<?php
$state
= snmprealwalk($host, "public", ".1.3.6.1.2.1.25.1", 50, 1);
$uptime
= ereg_replace("^.*\) ([0-9]+ .*):[0-9][0-9]\.[0-9]{2}.*$", "\\1", $state['host.hrSystem.hrSystemUptime.0']);
$users
= (int)ereg_replace("Gauge32: ", "", $state['host.hrSystem.hrSystemNumUsers.0']);
printf
('<div class="machine"><dt>%s</dt><dd>%s</dd>', $host, $desc);
printf
('<dd>up %s</dd>', $uptime);
if (
$users ) printf('<dd>%d user%s</dd>', $users, ($users > 1) ? 's' : '');
printf
('</div>');
?>
Lars Troen
21 年前
snmprealwalk 使用 oid 而非整數來索引值。當您需要包含在 oid 中的數據以及值時,這會很有用。

以下是如何擷取和列印 VLAN 資訊的範例
//
// 我先前已從 3com MIB 收集 VLAN 識別碼,它們儲存在 $vlan 表格中。
//
for($n=0;$n<count($vlan);$n++){
print $vlan[$n][id]." ".$vlan[$n][name]."<br>\n";
$ifStackStatusTable=@snmprealwalk($switch, $community, ".1.3.6.1.2.1.31.1.2.1.3.".$vlan[$n][id]); // ifMIB.ifMIBObjects.ifStackTable.ifStackEntry.ifStackStatus
for(reset($ifStackStatusTable); $port = key($ifStackStatusTable); next($ifStackStatusTable)){
print "$port=$ifStackStatusTable[$port]<br>";
}
aleksander dot sztramski at kpsi dot pl
15 年前
如果您想使用版本 2c 或 3,請使用以下函式

snmp v2c 函式

snmp2_get (字串 主機, 字串 群組, 字串 物件_id [, 整數 超時 [, 整數 重試次數]])
snmp2_getnext (字串 主機, 字串 群組, 字串 物件_id [, 整數 超時 [, 整數 重試次數]])
snmp2_walk (字串 主機, 字串 群組, 字串 物件_id [, 整數 超時 [, 整數 重試次數]])
snmp2_real_walk (字串 主機, 字串 群組, 字串 物件_id [, 整數 超時 [, 整數 重試次數]])
snmp2_set (字串 主機, 字串 群組, 字串 物件_id, 字串 類型, 混合 值 [, 整數 超時 [, 整數 重試次數]])

snmp v3 函式

snmp3_get (字串 主機, 字串 安全性_名稱, 字串 安全性_層級, 字串 驗證_協定, 字串 驗證_通行碼, 字串 私密_pr)
snmp3_getnext (字串 主機, 字串 安全性_名稱, 字串 安全性_層級, 字串 驗證_協定, 字串 驗證_通行碼, 字串 私密)
snmp3_walk (字串 主機, 字串 安全名稱, 字串 安全等級, 字串 驗證協議, 字串 驗證通行碼, 字串 私密通行碼)
snmp3_real_walk (字串 主機, 字串 安全名稱, 字串 安全等級, 字串 驗證協議, 字串 驗證通行碼, 字串 私密通行碼)
snmp3_set (字串 主機, 字串 安全名稱, 字串 安全等級, 字串 驗證協議, 字串 驗證通行碼, 字串 私密通行碼)
railson at amixsi dot com dot br
18 年前
注意:逾時單位為微秒(乘以 1,000,000 即為秒數)
scot at indievisible dot org
18 年前
基於在許多設備上測試大量 OID 後的一些改進。

<?php
function snmptable($host, $community, $oid) {
// TODO: get original state and restore at bottom
snmp_set_oid_numeric_print(TRUE);
snmp_set_quick_print(TRUE);
snmp_set_enum_print(TRUE);

$retval = array();
$raw = snmprealwalk($host, $community, $oid);
if (
count($raw) == 0) return ($retval); // no data

$prefix_length = 0;
$largest = 0;
foreach (
$raw as $key => $value) {
if (
$prefix_length == 0) {
// don't just use $oid's length since it may be non-numeric
$prefix_elements = count(explode('.',$oid));
$tmp = '.' . strtok($key, '.');
while (
$prefix_elements > 1) {
$tmp .= '.' . strtok('.');
$prefix_elements--;
}
$tmp .= '.';
$prefix_length = strlen($tmp);
}
$key = substr($key, $prefix_length);
$index = explode('.', $key, 2);
isset(
$retval[$index[1]]) or $retval[$index[1]] = array();
if (
$largest < $index[0]) $largest = $index[0];
$retval[$index[1]][$index[0]] = $value;
}

if (
count($retval) == 0) return ($retval); // no data

// fill in holes and blanks the agent may "give" you
foreach($retval as $k => $x) {
for (
$i = 1; $i <= $largest; $i++) {
if (! isset(
$retval[$k][$i])) {
$retval[$k][$i] = '';
}
}
ksort($retval[$k]);
}
return(
$retval);
}
?>
To Top