PHP之SNMP函数:snmpget/snmp2_get/snmp3_getsnmpget/snmp2_get/snmp3_get说明:获取一个 SNMP 对象(snmpget:php4/5;snmp2_get:php5>=5.2.0;snmp3_get:php4/5)
string snmpget ( string $hostname , string $community , string $object_id [, int $timeout [, int $retries ]] )
string snmp2_get ( string $host , string $community , string $object_id [, string $timeout = 1000000 [, string $retries = 5 ]] )
string snmp3_get ( string $host , string $sec_name , string $sec_level , string $auth_protocol , string $auth_passphrase , string $priv_protocol , string $priv_passphrase , string $object_id [, string $timeout = 1000000 [, string $retries = 5 ]] )
示例1:snmpget()
- <?php
- $syscontact = snmpget("localhost","public","sysContact.0");
- $sysname = snmpget("localhost","public","sysName.0");
- echo $syscontact;
- echo "\r\n";
- echo $sysname;
- echo "\r\n";
- ?>
输出结果:
STRING: MAOS
(configure /etc/snmp/snmp.local.conf)
STRING: enochost
示例2:snmp3_get()点击(此处)折叠或打开
- <?php
- $nameOfSecondInterface = snmp3_get('localhost', 'james', 'authPriv', 'SHA', 'secret007', 'AES', 'secret007', 'IF-MIB::ifName.2');
- var_dump($nameOfSecondInterface);
- ?>
示例3: snmp2_get()
点击(此处)折叠或打开
- <?php
- function get_server_info($host,$snmpkey,$oid){
- $c = snmp2_get($host,$snmpkey,$oid);
- // echo $c;
- $tmp = explode(":",$c);
- // var_dump($tmp);
- if(count($tmp)>1){$c = trim($tmp[1]);}
- return $c;
- }
- $host="localhost";
- $snmpkey="public";
- $load1 = get_server_info($host,$snmpkey,".1.3.6.1.4.1.2021.10.1.3.1");
- echo $load1;
- $sysDescr = get_server_info($host,$snmpkey,".1.3.6.1.2.1.1.1.0");
- echo $sysDescr;
- ?>
说明:
1. 定义一个function get_server_info(),通过snmp2_get()取出相应的字串存入$c中,通过":"分割$c存入数组$tmp,如果$tmp数组有多个值,截取$tmp数组的第二个值(下标为1)存回$c,并返回$c的值;
2. 可以去除注释来查看理解函数的执行过程;
示例4:一个适应snmp各个版本的封装类(wrapper class),点击(此处)折叠或打开
- <?php
- class SNMP_Wrapper {
- protected $_host;
- protected $_community;
- protected $_version;
- public function __construct($host='localhost',$community='public',$version=1)
- {
- $this->_host = $host;
- $this->_community = $community;
- switch ($version) {
- case 2:
- $this->_version = '2';
- break;
- case 3:
- $this->_version = '3';
- default:
- $this->_version = '';
- break;
- }
- }
- public function __call($func,$args)
- {
- $func = strtolower(preg_replace('/([A-Z])/', '_$1', $func));
- $function = 'snmp' . $this->_version . (empty($this->_version) ? '' : '_') . $func;
- if (function_exists($function)) {
- return call_user_func_array($function, array_merge(array($this->_host,$this->_community),$args));
- }
- }
- }
- // Testing it
- $snmp = new SNMP_Wrapper('localhost','public','2');
- print_r($snmp->realWalk('IF-MIB::ifName'));
- ?>
阅读(3334) | 评论(0) | 转发(0) |