php snmp,PHP: SNMP - Manual

Part of my diploma thesis was to create web interface to command device via SNMP. So I create my own level of abstraction over SNMP class:

* Snmp library which add one level of abstraction to snmp native library.

* It adds functionality to work with module PicoIP.With this library you can:

* 1.Activate/deactive defined pin;

* 2.Get status of all pins.

*

* When make an instance you should pass to the constructor key word which will

* make the library create an object with necessary properetis and access permissions.

*

* Private properties set some of configurations:

* Host is IP address of the divece which we will command.

* Two passwords are set for reading and writing.

* Version of snmp protocol that we will use is version 1.

*

* @author Radoslav Madjev

* @year 2016

* @version 1.0 beta

*

*

*/classsnmp_lib{

private$snmpInstance;

private$VERSION=SNMP::VERSION_1;

private$HOST='192.168.0.150';

private$passwordRead='000000000000';

private$passwordWrite='private';

private$releys= array(1=>'1.3.6.1.4.1.19865.1.2.1.1.0',2=>'1.3.6.1.4.1.19865.1.2.1.2.0');

private$allPorts= array('3'=>'1.3.6.1.4.1.19865.1.2.1.33.0','5'=>'1.3.6.1.4.1.19865.1.2.2.33.0');/**

* Create instance of SNMP native class, based on actions that we will

* perform.

*

* @param string $action

*/public function__construct($action) {

if (in_array($action, array('read','write'))) {

if (strcmp($action,'read') ===0) {$this->_read();

} else {$this->_write();

}

}

}/**

* Create instance with reading permissions.

*/private function_read() {$this->snmpInstance= newSNMP($this->VERSION,$this->HOST,$this->passwordRead);

}/**

* Create instance with writing permissions.

*/private function_write() {$this->snmpInstance= newSNMP($this->VERSION,$this->HOST,$this->passwordWrite);

}/**

* Close snmp session.

*

* @return boolean

*/public functioncloseSession() {

return$this->snmpInstance->close();

}/**

* Set integer 1 as value of defined pin.

*/public functionactivate($relay) {$this->snmpInstance->set($this->releys[$relay],'i','1');

}/**

* Set integer 0 as value of defined pin.

*/public functiondeactivate($relay) {$this->snmpInstance->set($this->releys[$relay],'i','0');

}/**

* Get pin status of all ports of the module.

*

* @return array

*/public functiongetAllPortsStatus() {$allPins= array();

foreach ($this->allPortsas$number=>$port) {//get active pins as 8-bit integer of defined port$getbits=$this->snmpInstance->get($port);$bits=str_replace('INTEGER: ','',$getbits);//get pins status$pinsStatus=$this->_getActivePins($bits);$allPins[$number] =$pinsStatus;

}

return$allPins;

}/**

* Make bitwise operation which will determine,

* which are active pins.

*

* @param int $bits

* @return array

*/private function_getActivePins($bits) {$bitMapping= array(1=>1,2=>2,3=>4,4=>8,5=>16,6=>32,7=>64,8=>128);$pinsStatus= array();

foreach ($bitMappingas$int=>$bit) {

if (($bits&$bit) ==$bit) {$pinsStatus[$int] =true;

continue;

}$pinsStatus[$int] =false;

}

return$pinsStatus;

}

}?>

I have one module that receive SNMP request and send a command to relays. Also these are example scripts that use this lib:

Turn on script:

<?phprequire_once 'snmp_lib.php';$snmp= newsnmp_lib('write');$snmp->activate($getRelayNumber);$snmp->closeSession();?>

Turn off script:

<?phprequire_once 'snmp_lib.php';$snmp= newsnmp_lib('write');$snmp->deactivate($getRelayNumber);$snmp->closeSession();?>

Script that get all ports status:

<?phprequire_once 'snmp_lib.php';$snmp= newsnmp_lib('read');$getActive=$snmp->getAllPortsStatus();?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值