PHP code example of ocolin / easysnmp

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

    

ocolin / easysnmp example snippets


use Ocolin\EasySNMP\Config;
use Ocolin\EasySNMP\EasySNMP;

$config = new Config(
    host:      '10.0.0.1',
    community: 'public'
);

$snmp = new EasySNMP( config: $config );

// Reads from EASY_SNMP_* environment variables
$snmp = new EasySNMP( config: new Config() );
// Or alternatively
$snmp = new EasySNMP();

$juniper  = new EasySNMP( config: new Config( prefix: 'JUNIPER' ) );
$mikrotik = new EasySNMP( config: new Config( prefix: 'MIKROTIK' ) );

$system = $snmp->getSystem();

echo $system->name;      // Router-01
echo $system->descr;     // Linux Router 5.15.0
echo $system->location;  // Server Room A
echo $system->contact;   // [email protected]
echo $system->upTime;    // TimeTicks integer
echo $system->objectId;  // 1.3.6.1.4.1.14988.1

// Formatted version:
$system = Format::System( $snmp->getSystem());

$interfaces = $snmp->getIfTable();

foreach( $interfaces as $interface ) {
    echo $interface->index;        // Interface index
    echo $interface->description;  // Interface description
    echo $interface->speed;        // Speed in bps
    echo $interface->adminStatus;  // Raw integer (1=up, 2=down, 3=testing)
    echo $interface->operStatus;   // Raw integer (1=up, 2=down, etc.)
    echo $interface->macAddress;   // Raw MAC address
}


// Formatted version:
$interfaces = Format::IfTables( $snmp->getIfTable());

$interfaces = $snmp->getIfTable( columns: ['description', 'operStatus'] );

$interfaces = $snmp->getIfXTable();

foreach( $interfaces as $interface ) {
    echo $interface->name;        // Interface name
    echo $interface->alias;       // Administrator-set alias
    echo $interface->highSpeed;   // Speed in Mbps
    echo $interface->inHcOctets;  // 64-bit input byte counter
    echo $interface->outHcOctets; // 64-bit output byte counter
}

// Formatted version:
$interfaces = Format::IfXTables( $snmp->getIfXTable());

$arp = $snmp->getArpTable();

foreach( $arp as $entry ) {
    echo $entry->interface;  // Interface index
    echo $entry->ipAddress;  // IP address
    echo $entry->mac;        // Raw MAC address
    echo $entry->type;       // Raw integer (1=other, 2=invalid, 3=dynamic, 4=static)
}

// Formatted version:
$arp = Format::ArpTables( $snmp->getArpTable());

$lldp_rem = $snmp->getLldpRemTable();
foreach( $arp as $entry ) {
    echo $entry->localPort;     // Local port index
    echo $entry->chassisIdType; // Chassis ID subtype
    echo $entry->chassisId;     // Chassis identifier
    echo $entry->portIdType;    // Port ID subtype
    echo $entry->portId;        // Remote port identifier
    echo $entry->portDesc;      // Remote port description
    echo $entry->sysName;       // Remote system name
    echo $entry->sysDesc;       // Remote system description
    echo $entry->capSupported;  // Supported capabilities
    echo $entry->capEnabled;    // Enabled capabilities
}

// Formatted values:
$lldp_rem = Format::LldpRemTable( $snmp->getLldpRemTable());

$addresses = $snmp->getIpAddrTable();

foreach( $addresses as $entry ) {
    echo $entry->address;    // 10.0.0.1
    echo $entry->interface;  // Interface index — matches ifTable
    echo $entry->netmask;    // 255.255.255.0
    echo $entry->bcast;      // Broadcast address bit
    echo $entry->reasmMaxSize; // Max datagram size for reassembly
}

// Fetch only specific columns
$addresses = $snmp->getIpAddrTable( columns: ['address', 'interface', 'netmask'] );

$macs = $snmp->getMacTable();

foreach( $macs as $entry ) {
    echo $entry->mac;        // Raw MAC address
    echo $entry->bridge;     // Bridge port number
    echo $entry->interface;  // Interface index — matches ifTable index
    echo $entry->status;     // Raw integer (3=learned, 4=self etc.)
}

// Formatted version
$macs = Format::macTables( $snmp->getMacTable() );

$routes = $snmp->getIpForwardTable();

foreach( $routes as $route ) {
    echo $route->destination;  // Destination network
    echo $route->mask;         // Subnet mask
    echo $route->nextHop;      // Next hop IP address
    echo $route->interface;    // Interface index — matches ifTable
    echo $route->type;         // Raw integer. Use SnmpHelper::formatIpForwardType()
    echo $route->protocol;     // Raw integer. Use SnmpHelper::formatIpForwardProto()
    echo $route->status;       // Raw integer. Use SnmpHelper::formatRowStatus()
}

// Fetch additional columns
$routes = $snmp->getIpForwardTable( columns: [
    'interface', 'type', 'protocol', 'metric1', 'status',
    'age', 'nextHopAs', 'metric2', 'metric3', 'metric4', 'metric5'
]);

// Formatted version
$routes = Format::ipForwardTables( $snmp->getIpForwardTable() );

$ports = $snmp->getStpPorts();

foreach( $ports as $port ) {
    echo $port->bridge;    // Bridge port number — matches MacEntry bridge
    echo $port->priority;  // Port priority (0-255)
    echo $port->state;     // Raw integer. Use SnmpHelper::formatStpPortState()
    echo $port->enable;    // Raw integer. Use SnmpHelper::formatStpPortEnable()
    echo $port->pathCost;  // STP path cost
    echo $port->desRoot;   // Designated root bridge ID (8 bytes binary)
    echo $port->desCost;   // Designated path cost to root
    echo $port->desBridge; // Designated bridge ID (8 bytes binary)
    echo $port->desPort;   // Designated port ID (2 bytes binary)
}

// Formatted version
$ports = Format::stpPortEntries( $snmp->getStpPorts() );

use Ocolin\EasySNMP\SnmpHelper;

// Interface status
SnmpHelper::formatOperStatus( $interface->operStatus );
// "up", "down", "testing", "unknown", "dormant", "notPresent", "lowerLayerDown"

SnmpHelper::formatAdminStatus( $interface->adminStatus );
// "up", "down", "testing"

// Interface type
SnmpHelper::formatIfType( $interface->type );
// "ethernet", "softwareLoopback", "tunnel", "ieee8023adLag", etc.

// MAC address formatting
SnmpHelper::formatMacAddress( $interface->macAddress );
// "00:11:22:33:44:55"

SnmpHelper::formatMacAddress( $entry->mac );
// "00:11:22:33:44:55"

// ARP entry type
SnmpHelper::formatArpType( $entry->type );
// "other", "invalid", "dynamic", "static"

namespace Ocolin\Hyconext;

use Ocolin\EasySNMP\EasySNMP;
use Ocolin\Hyconext\Traits\PoeTrait;

class HyconextSNMP extends EasySNMP
{
    use PoeTrait;
}

// Fetch specific OIDs
$result = $snmp->get( '1.3.6.1.2.1.1.1.0', '1.3.6.1.2.1.1.5.0' );

// Fetch next OID
$result = $snmp->getNext( '1.3.6.1.2.1.1.1.0' );

// Walk an OID subtree
$walk = $snmp->walk( startAt: '1.3.6.1.2.1.1' );

// Bulk walk an OID subtree — returns paginated Oid[] array
$oids = $snmp->bulkWalk( oid: '1.3.6.1.2.1.2.2.1' );