PHP code example of terr17216 / snmpbox

1. Go to this page and download the library: Download terr17216/snmpbox library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

terr17216 / snmpbox example snippets


use terr17216\SnmpBox\SnmpBoxClient;
use terr17216\SnmpBox\SnmpBoxRequest;

// Настройка объекта запроса
$request = (new SnmpBoxRequest())
    ->setIpAddress('192.168.1.1')
    ->setCommand('walk')
    ->setSnmpVersion(2)
    ->setSnmpCommunity('public')
    ->setOid(['.1.3.6.1.2.1.1.5', '.1.3.6.1.2.1.1.3']); // SysName and Uptime
// А можно и более коротко:    
$request = SnmpBoxRequest::create('192.168.1.1', 'public', 'walk', ['.1.3.6.1.2.1.2.2.1.1', '.1.3.6.1.2.1.2.2.1.5']);

// Получение данных с контроллеров через Redis
// Передайте в конструктор клиента ваш Redis и Symfony MessageBus
$snmpClient = new SnmpBoxClient(redisInstance, messageBus, request);
// Отправка в очередь и ожидание результата
$result = snmpClient->sendRequestAndGetResult();

// Получение данных из Command - прямое взаимодействие
$action = new SnmpBoxAction($request);
$result = $action->execute();

if (\$result->isSuccess()) {
    print_r(\$result->getFullResult());
    print_r(\$result->setResult());
} else {
    echo "Ошибка: " . \$result->getError();
}

class SomeController extends AbstractController
{
    ...
    
    #[Route('/some/route/request', name: 'app_route_request')]
    public function methodRequest(): JsonResponse
    {
        return $this->json(['url' => $this->generateUrl('app_route_body',
            ['requestId' => $this->sendRequest($this->redis, $this->messageBus)])]);
    }
    
    #[Route('/some/route/{requestId}/body', name: 'app_route_body')]
    public function methodBody(string $requestId): Response
    {
        $snmpClient = new SnmpBoxClient($this->redis, $this->messageBus, new SnmpBoxRequest());
        $snmpClient->setRequestId($requestId); $result = $snmpClient->getResult();
        return new Response(\print_r(\$result->getFullResult()), Response::HTTP_OK,
        ['content-type' => 'text/html']);
    }
    
    public function sendRequest(\Redis $redis, MessageBusInterface $messageBus): string
    {
        $request = (new SnmpBoxRequest())
        ->setIpAddress('192.168.1.1')
        ->setCommand('walk')
        ->setSnmpVersion(2)
        ->setSnmpCommunity('public')
        ->setOid(['.1.3.6.1.2.1.1.5', '.1.3.6.1.2.1.1.3']); // SysName and Uptime
        $snmpClient = new SnmpBoxClient($redis, $messageBus, $this->getSnmpBoxRequest());
        $snmpClient->sendRequest();
        return $snmpClient->getRequestId();
    }
}


declare(strict_types=1);

namespace App\MessageHandler;

use terr17216\snmpbox\SnmpBoxHandler;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
class SnmpBoxRequestHandler extends SnmpBoxHandler
{
}

class SomeCommand extends Command
{
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $request = (new SnmpBoxRequest())
        ->setIpAddress('192.168.1.1')
        ->setCommand('walk')
        ->setSnmpVersion(2)
        ->setSnmpCommunity('public')
        ->setOid(['.1.3.6.1.2.1.1.5', '.1.3.6.1.2.1.1.3']);
        
        // Взаимодействие через Redis
        $snmpClient = new SnmpBoxClient($this->redis, $this->messageBus, $snmpRequest);
        $snmpBoxResult = $snmpClient->sendRequestAndGetResult();
    }
}

class SomeCommand extends Command
{
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $request = (new SnmpBoxRequest())
        ->setIpAddress('192.168.1.1')
        ->setCommand('walk')
        ->setSnmpVersion(2)
        ->setSnmpCommunity('public')
        ->setOid(['.1.3.6.1.2.1.1.5', '.1.3.6.1.2.1.1.3']);
        
        // Прямое взаимодействие
        $snmpBoxAction = new SnmpBoxAction($snmpRequest);
        $snmpBoxResult = $snmpBoxAction->execute();
    }
}

$request = (new SnmpBoxRequest())
   ->setIpAddress('192.168.1.1')
   ->setCommand('ping');
bash
php bin/console messenger:consume