note 68754 added to function.snmprealwalk
| From: | scot at indievisible dot org | Date: | Wed, 09 Aug 2006 15:16:49 +0000 |
| Subject: | note 68754 added to function.snmprealwalk | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115730@lists.php.net to get a copy of this message | ||
Since PHP doesn't have a nice snmptable-like function... here is a quick-and-dirty hack that
works for me. Works on complete and sparse tables. The example oids are for the route (complete) and
interface (often sparse) tables.
<?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);
}
?>
----
Server IP: 216.194.113.175
Probable Submitter: 209.163.178.60
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.snmprealwalk.php
Edit -- https://master.php.net/note/edit/68754
Del: integrated -- https://master.php.net/note/delete/68754/integrated
Del: useless -- https://master.php.net/note/delete/68754/useless
Del: bad code -- https://master.php.net/note/delete/68754/bad+code
Del: spam -- https://master.php.net/note/delete/68754/spam
Del: non-english -- https://master.php.net/note/delete/68754/non-english
Del: in docs -- https://master.php.net/note/delete/68754/in+docs
Del: other reasons-- https://master.php.net/note/delete/68754
Reject -- https://master.php.net/note/reject/68754
Search -- https://master.php.net/manage/user-notes.php