PHP code example of surrealcristian / snmp-extension

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

    

surrealcristian / snmp-extension example snippets




urrealCristian\SnmpExtension\Builder;
use SurrealCristian\SimpleSnmp\Exception\SimpleSnmpException;
use SurrealCristian\SimpleSnmp\Exception\TimeoutException;

$host = '127.0.0.1';
$community = 'private';
$timeout = 1000000; // microseconds
$retries = 3;

$snmp = (new Builder)->getSimpleSnmpV2c();



try {
    $oid = '1.2.3.4.5.0';

    $res = $snmp->get($host, $community, $oid, $timeout, $retries);

    var_export($res);
} catch (TimeoutException $e) {
    // handle exception
} catch (SimpleSnmpException $e) {
    // handle exception
}

// array (
//   'oid' => '1.2.3.4.5.0',
//   'type' => 'STRING',
//   'value' => '"foo 0"',
// )



try {
    $oid = '1.2.3.4.5.0';

    $res = $snmp->getNext($host, $community, $oid, $timeout, $retries);

    var_export($res);
} catch (TimeoutException $e) {
    // handle exception
} catch (SimpleSnmpException $e) {
    // handle exception
}

// array (
//   'oid' => null,
//   'type' => 'STRING',
//   'value' => '"foo 1"',
// )



try {
    $oid = '1.2.3.4.5';

    $res = $snmp->walk($host, $community, $oid, $timeout, $retries);

    var_export($res);
} catch (TimeoutException $e) {
    // handle exception
} catch (SimpleSnmpException $e) {
    // handle exception
}

// array (
//   0 => array (
//     'oid' => '1.2.3.4.5.0',
//     'type' => 'STRING',
//     'value' => '"foo 0"',
//   ),
//   1 => array (
//     'oid' => '1.2.3.4.5.1',
//     'type' => 'STRING',
//     'value' => '"foo 1"',
//   ),
// )



try {
    $oid = '1.2.3.4.5';

    $res = $snmp->bulkWalk($host, $community, $oid, $timeout, $retries);

    var_export($res);
} catch (TimeoutException $e) {
    // handle exception
} catch (SimpleSnmpException $e) {
    // handle exception
}

// array (
//   0 => array (
//     'oid' => '1.2.3.4.5.0',
//     'type' => 'STRING',
//     'value' => '"foo 0"',
//   ),
//   1 => array (
//     'oid' => '1.2.3.4.5.1',
//     'type' => 'STRING',
//     'value' => '"foo 1"',
//   ),
// )



try {
    $oid = '1.2.3.4.6.0';

    $snmp->set($host, $community, $oid, 's', 'test', $timeout, $retries);
} catch (TimeoutException $e) {
    // handle exception
} catch (SimpleSnmpException $e) {
    // handle exception
}