PHP code example of ndum / laravel-snmp

1. Go to this page and download the library: Download ndum/laravel-snmp 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/ */

    

ndum / laravel-snmp example snippets


use Ndum\Laravel\Snmp;

$snmp = new Snmp();
$snmp->newClient('servername', 2, 'secret');
$result = $snmp->getValue('1.3.6.1.2.1.1.5.0'); ## hostname
dd($result);

use Ndum\Laravel\Facades\Snmp;

$snmp = Snmp::newClient('servername', 2, 'secret');
$result = $snmp->getValue('1.3.6.1.2.1.1.5.0'); ## hostname
dd($result);

use Ndum\Laravel\Snmp;

$snmp = new Snmp();
$snmp->newClient('servername', 2, 'secret');
$snmp->setTimeoutConnectValue(5); # set a value for timeout_connect
$snmp->setTimeoutReadValue(10); # set a value for timeout_read

$result = $snmp->getValue('1.3.6.1.2.1.1.5.0'); ## hostname
dd($result);


$snmp = new Snmp();
$snmp->newClient('targetserver', 1, 'secret', 162); 

# Parameters:
# The enterprise OID to trigger (string)
# The IP address. (string)
# The generic trap type (int)
# The specific trap type (int)
# The system uptime (in seconds/int)
# The OIDs and their values (string/int)
$snmp->sendTrapV1('1.3.6.1.4.1.2021.251.1','localhost', 0, 0, 60, '1.3.6.1.2.1.1.3', 60);

use Ndum\Laravel\Snmp;

$snmp = new Snmp();
$snmp->newClient('targetserver', 2, 'secret', 162);

# Parameters:
# The system uptime (in seconds/int)
# The trap OID (string)
# The OIDs and their values (string/int)
$snmp->sendTrap(60, '1.3.6.1.4.1.2021.251.1', '1.3.6.1.2.1.1.3', 60));

use Ndum\Laravel\Snmp;

$snmp = new Snmp();
$snmp->newClient('targetserver', 2, 'secret', 162);

# Parameters:
# The system uptime (in seconds/int)
# The trap OID (string)
# The OIDs and their values (string/int)
$snmp->sendInform(60, '1.3.6.1.4.1.2021.251.1', '1.3.6.1.2.1.1.3', 60));

use Ndum\Laravel\SnmpTrapServer; # don't forget to use your listener also!

# default options
$options = [
        'ip' => '0.0.0.0',
        'port' => 162,
        'transport' => 'udp',
        'version' => null,
        'community' => null,
        'whitelist' => null,
        'timeout_connect' => 5,
    ];

$listener = new TrapListener(); ### your in step 1 created listener-class

$server = new SnmpTrapServer()
$server->prepare($listener, $options) # $options only needed if other than default
$server->listen();

# in addition: (only if needed)
$server->getOptions(); # to get the options
$server->setOptions($options); # to set the options