php怎么返回一个对象,PHP 返回指定的所有对象,包括它们各自的对象 ID

用户评论:

aleksander dot sztramski at kpsi dot pl (2009-08-20 04:39:21)

If you wish to use version 2c or 3, use the following functions:

snmp v2c functions:

snmp2_get (string host, string community, string object_id [, int timeout [, int retries]])

snmp2_getnext (string host, string community, string object_id [, int timeout [, int retries]])

snmp2_walk (string host, string community, string object_id [, int timeout [, int retries]])

snmp2_real_walk (string host, string community, string object_id [, int timeout [, int retries]])

snmp2_set (string host, string community, string object_id, string type, mixed value [, int timeout [, int retries]])

snmp v3 functions:

snmp3_get (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_pr)

snmp3_getnext (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string pri)

snmp3_walk (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_p)

snmp3_real_walk (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string p)

snmp3_set (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_pr)

Anonymous (2008-05-21 10:19:37)

To check if there were any results found you have to use the empty() function. The count() function always returns a number 1 or larger.

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

if (empty($walk_result)) {

print "No network interfaces found.
\n";

exit(0);

}

scot at indievisible dot org (2006-08-09 12:10:51)

Some improvements based on testing a lot of OIDs on a lot of devices.

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

foreach ($rawas$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

}

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

// fill in holes and blanks the agent may "give" youforeach($retvalas$k=>$x) {

for ($i=1;$i<=$largest;$i++) {

if (! isset($retval[$k][$i])) {$retval[$k][$i] ='';

}

}ksort($retval[$k]);

}

return($retval);

}?>

scot at indievisible dot org (2006-08-09 08:16:49)

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.

$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);

functionsnmptable($host,$community,$oid) {// TODO: get original state and restore at bottomsnmp_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 ($rawas$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 inforeach ($retval[$firstrow] as$key=>$tmp) {

foreach($retvalas$check=>$tmp2) {

if (! isset($retval[$check][$key])) {$retval[$check][$key] ='';

}

}

}

return($retval);

}?>

railson at amixsi dot com dot br (2006-05-30 10:52:43)

Attention: timeout is in microseconds (multiply by 1,000,000 for seconds)

Stephen Cope (2004-09-09 20:38:09)

Here's a way to find the uptime and number of users on a machine. (Note that uptime is the uptime of the snmpd daemon, which should be fairly close to the uptime for the host.)

$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('

%s%s',$host,$desc);printf('up %s',$uptime);

if ($users)printf('

%d user%s',$users, ($users>1) ?'s':'');printf('
');?>

Lars Troen (2003-02-24 05:30:30)

snmprealwalk indexes the values using the oid instead of an integer. This is useful when you need data that is contained in the oid as well as the value.

Here's an example for retrieving and printing vlan info:

//

// I have collected the vlan identifiers earlier from the 3com mib and they are stored in the $vlan table.

//

for($n=0;$n

print $vlan[$n][id]." ".$vlan[$n][name]."
\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]
";

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值