文字

snmpwalk

(PHP 4, PHP 5)

snmpwalk从代理返回所有的 SNMP 对象

说明

array snmpwalk ( string $hostname , string $community , string $object_id [, int $timeout [, int $retries ]] )

返回由 object_id 作为根的 SNMP 对象值所组成的数组,错误则返回 FALSE

snmpwalk() 函数是用来读取所有由 hostname 指定的 SNMP 代理的值。Community 指定该代理中具有读权限的域。一个为 NULL object_id 将被看作 SNMP 对象树的根,而在此树下的所有对象将作为一个数组被返回。如果指定了 object_id,则返回所有在 object_id 下的 SNMP 对象。

<?php
$a 
snmpwalk ( "127.0.0.1" "public" "" ); 
?>

上边的函数调用将从运行于本机的 SNMP 代理那里返回所有的 SNMP 对象。可使用循环遍历这些值。

<?php
foreach ( $a  as  $val ) {
    echo 
" $val \n" ;
}
?>

用户评论:

[#1] http://mike.eire.ca [2005-03-23 10:54:45]

I found on Windows (PHP 5) an empty string did not return anything, it just timed out.  I had to use null instead:

<?php
$a 
snmpwalk("127.0.0.1""public"null);
?>

[#2] bobby [dot] clark [at] eku [dot] edu [2003-09-17 14:29:02]

I had to use an object_id like these.
'SNMPv2-MIB::system.sysDescr.0'
'IF-MIB::interfaces.ifTable.ifEntry.ifAdminStatus'

<?php
$host 
'192.168.1.1';
$community 'public';
$object_id 'IF-MIB::interfaces.ifTables.ifEntry.ifAdminStatus';

$sysdesc snmpwalk($host$community', $object_id);
print_r($sysdesc);
?>

[#3] Lars Troen [2003-03-14 03:18:46]

Note that there's different behaviuor in php snmpwalk and ucd snmpwalk. If you try to walk an oid that has one value not under a subkey of the walked oid, ucd snmpwalk will return the value while php's snmpwalk will not.

[#4] anders at ei dot nu [2003-01-29 10:11:47]

It would be nice to be able to specify what snmp version to use ( 1,2c,3 )

For now, I'ts hardcoded in ext/snmp/snmp.c

change session.version from 1 to 2c or 3 if you need for now..

i.e 

session.version = SNMP_VERSION_1;

to:
session.version = SNMP_VERSION_2c;

[#5] steve at ourabode dot org [2002-03-26 03:54:30]

Timeout is in MICRO seconds.
1,000,000 &micros = 1 s

[#6] layer2 at www dot com [2001-08-29 08:52:21]

Something to care about in dealing with snmpwalk:<BR>




While walking the MIB, snmpwalk puts info that gets into an array, and that is correct.<BR>




The trouble happened when snmpwalk needs to collect information from instances that contains subinstances (i.e. walking .1.2.3.4.5 and having instances like 1.1, 1.2, 1.3): in this case it gets info and passes into an array, but when walking the array, each value is preceeded by 'Counter32: '.<BR>




I've tested this in many ways and it always happened the same way.

[#7] john at antefacto dot com [2001-08-17 08:21:05]

Ah. That's why all of our SNMP stuff was timing out anytime there was any load on the system. Sigh. A waste of two weeks trying to debug snmp....

 Even the snmpcmd manpage doesn't give a
unit for timeout.

[#8] billf at freebsd dot org [2001-08-01 02:22:21]

for the poster wondering what the
timeout field was measured in:

from the ucd-snmp header file snmp_api.h:

    long    timeout; 
    

[#9] jmartinson(AT_nospam)info234.com [2001-06-20 07:41:26]

A quick router device view:

<?php
include "header.html";

$host "auscr1";
$community "tellme";
                     
$sysDescr snmpget("$host","$community","system.sysDescr.0");
$ifDescr snmpwalk("$host","$community","interfaces.ifTable.ifEntry.ifDescr");
$ifIndex snmpwalk("$host","$community","interfaces.ifTable.ifEntry.ifIndex");
$ifAdminStatus snmpwalk("$host","$community","interfaces.ifTable.ifEntry.ifAdminStatus");
$ifOperStatus snmpwalk("$host","$community","interfaces.ifTable.ifEntry.ifOperStatus");
$ifLastChange snmpwalk("$host","$community","interfaces.ifTable.ifEntry.ifLastChange");
                                          
print 
"<table border=1 bgcolor=#ffffff><tr><td>$host</td></tr></table><br>";
print 
"<table border=1 bgcolor=#ffffff><tr><td>$sysDescr</td></tr></table><br>";
print 
"<table border=1 bgcolor=#ffffff>";
print 
"<tr> 
        <td>ifIndex</td>
        <td>ifDescr</td>
        <td>ifAdminStatus</td>
        <td>ifOperStatus</td>
        <td>ifLastChange</td>
        </tr>"
;
             
for (
$i=0$i<count($ifIndex); $i++) {
        print 
"<tr>";
        print 
"<td>$ifIndex[$i]</td>";
        print 
"<td>$ifDescr[$i]</td>";
        print 
"<td>$ifAdminStatus[$i]</td>";
        print 
"<td>$ifOperStatus[$i]</td>";
        print 
"<td>$ifLastChange[$i]</td>";
        print 
"</tr>";
}            
print 
"</table>";

?>

上一篇: 下一篇: