(PHP 5 >= 5.4.0, PHP 7, PHP 8)
Представляет сессию протокола SNMP.
$version,$hostname,$community,$timeout = -1,$retries = -1SNMP_VALUE_LIBRARY Возвращаемые значения будут такими, как будто их вернула библиотека Net-SNMP.SNMP_VALUE_PLAIN Возвращаемые значения будут простыми, без информации о типах SNMP.SNMP_VALUE_OBJECT
Возвращаемые значения будут объектами со свойствами value и type,
где type будет содержать одну из констант: SNMP_OCTET_STR, SNMP_COUNTER и т. д.,
а value будет зависеть от того, установили
SNMP_VALUE_LIBRARY или
SNMP_VALUE_PLAIN .
quick_print
в библиотеке NET-SNMP
Устанавливает значение quick_print в библиотеке
NET-SNMP. если задали как (1), библиотека
SNMP будет возвращать значения «quick printed».
То есть будут напечатаны только значения. Если
параметр quick_print не разрешён (по умолчанию), библиотека
NET-SNMP будет печатать дополнительную информацию,
включая тип значения (то есть IpAddress или OID). Дополнительно, если
параметр quick_print не разрешён, библиотека будет печатать шестнадцатеричные значения
для каждой строки короче четырёх символов.
SNMP_OID_OUTPUT_FULL .iso.org.dod.internet.mgmt.mib-2.system.sysUpTime.sysUpTimeInstanceSNMP_OID_OUTPUT_NUMERIC .1.3.6.1.2.1.1.3.0 SNMP_OID_OUTPUT_MODULE DISMAN-EVENT-MIB::sysUpTimeInstanceSNMP_OID_OUTPUT_SUFFIX sysUpTimeInstanceSNMP_OID_OUTPUT_UCD system.sysUpTime.sysUpTimeInstanceSNMP_OID_OUTPUT_NONE Undefinedfalse ,
чтобы запретить эту проверку.
SNMP::ERRNO_* через побитовое ИЛИ.
По умолчанию SNMP не выбрасывает исключения.
SNMP::ERRNO_NOERROR SNMP::ERRNO_GENERIC SNMP::ERRNO_TIMEOUT SNMP::ERRNO_ERROR_IN_REPLY SNMP::ERRNO_OID_NOT_INCREASING SNMP::ERRNO_OID_PARSING_ERROR SNMP::ERRNO_MULTIPLE_SET_QUERIES SNMP::ERRNO_ANY Hopefully this helps someone else out because this was driving be bonkers for a good two hours. It looks like valueretrieval and enum_print work together in a way that I wasn't expecting, and after re-reading may be by design but I'm not sure. If you can't get enums to print, this might be why.
<?php
$snmp = new SNMP(SNMP::VERSION_2C,'192.168.1.9','test');
$snmp->oid_output_format = SNMP_OID_OUTPUT_SUFFIX;
$snmp->valueretrieval = SNMP_VALUE_PLAIN;
$snmp->enum_print = 0;
echo $snmp->get('IF-MIB::ifOperStatus.10110') . "\n";
$snmp->enum_print = 1;
echo $snmp->get('IF-MIB::ifOperStatus.10110') . "\n";
$snmp->quick_print = 1;
echo $snmp->get('IF-MIB::ifOperStatus.10110') . "\n";
$snmp->valueretrieval = SNMP_VALUE_LIBRARY;
echo $snmp->get('IF-MIB::ifOperStatus.10110') . "\n";
$snmp->enum_print = 0;
echo $snmp->get('IF-MIB::ifOperStatus.10110') . "\n";
?>
Output:
1
1
1
1
upPart 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:
<?php
/**
* 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
*
*
*/
class snmp_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 = new SNMP($this->VERSION, $this->HOST, $this->passwordRead);
}
/**
* Create instance with writing permissions.
*/
private function _write() {
$this->snmpInstance = new SNMP($this->VERSION, $this->HOST, $this->passwordWrite);
}
/**
* Close snmp session.
*
* @return boolean
*/
public function closeSession() {
return $this->snmpInstance->close();
}
/**
* Set integer 1 as value of defined pin.
*/
public function activate($relay) {
$this->snmpInstance->set($this->releys[$relay], 'i', '1');
}
/**
* Set integer 0 as value of defined pin.
*/
public function deactivate($relay) {
$this->snmpInstance->set($this->releys[$relay], 'i', '0');
}
/**
* Get pin status of all ports of the module.
*
* @return array
*/
public function getAllPortsStatus() {
$allPins = array();
foreach ($this->allPorts as $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 ($bitMapping as $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:
<?php
require_once 'snmp_lib.php';
$snmp = new snmp_lib('write');
$snmp->activate($getRelayNumber);
$snmp->closeSession();
?>
Turn off script:
<?php
require_once 'snmp_lib.php';
$snmp = new snmp_lib('write');
$snmp->deactivate($getRelayNumber);
$snmp->closeSession();
?>
Script that get all ports status:
<?php
require_once 'snmp_lib.php';
$snmp = new snmp_lib('read');
$getActive = $snmp->getAllPortsStatus();
?>