FreeDSx / SNMP

A Pure PHP SNMP Library.
MIT License
58 stars 15 forks source link

SnmpWalk method not working #13

Closed jozefrebjak closed 2 years ago

jozefrebjak commented 2 years ago

Hello @ChadSikorra,

I am taking look at this libary and I would like to use it in my project. But, how to use SnmpWalk ?

For example I wan to read values of oid 1.3.6.1.2.1.2.2.1.1. With classic snmpwalk I can get values with:

snmpwalk -v2c -c ${COMMUNITY) ${HOST_IP} 1.3.6.1.2.1.2.2.1.1
IF-MIB::ifIndex.1 = INTEGER: 1
IF-MIB::ifIndex.2 = INTEGER: 2
IF-MIB::ifIndex.3 = INTEGER: 3
IF-MIB::ifIndex.4 = INTEGER: 4
IF-MIB::ifIndex.5 = INTEGER: 5
IF-MIB::ifIndex.6 = INTEGER: 6
IF-MIB::ifIndex.7 = INTEGER: 7
IF-MIB::ifIndex.8 = INTEGER: 8
IF-MIB::ifIndex.9 = INTEGER: 9
IF-MIB::ifIndex.10 = INTEGER: 10
IF-MIB::ifIndex.11 = INTEGER: 11
IF-MIB::ifIndex.12 = INTEGER: 12
IF-MIB::ifIndex.13 = INTEGER: 13
IF-MIB::ifIndex.14 = INTEGER: 14
IF-MIB::ifIndex.15 = INTEGER: 15
IF-MIB::ifIndex.16 = INTEGER: 16
IF-MIB::ifIndex.17 = INTEGER: 17
IF-MIB::ifIndex.18 = INTEGER: 18
IF-MIB::ifIndex.19 = INTEGER: 19
IF-MIB::ifIndex.20 = INTEGER: 20
IF-MIB::ifIndex.21 = INTEGER: 21
IF-MIB::ifIndex.22 = INTEGER: 22
IF-MIB::ifIndex.23 = INTEGER: 23
IF-MIB::ifIndex.24 = INTEGER: 24
IF-MIB::ifIndex.25 = INTEGER: 25
IF-MIB::ifIndex.26 = INTEGER: 26
IF-MIB::ifIndex.27 = INTEGER: 27
IF-MIB::ifIndex.28 = INTEGER: 28

but when I am trying to get values with SnmpWalk function from this libaray I can't get values for example:

    public function get()
    {
        $snmp = new SnmpClient([
            'host' => ${HOST_IP},
            'version' => 2,
            'community' => ${COMMUNITY},
        ]);

        $walk = $snmp->walk('1.3.6.1.2.1.2.2.1.1');

        return $walk;
    }

I expect that count can't be 0

Snímka obrazovky 2021-12-14 o 15 56 19

@ChadSikorra can you help me what I am doing wrong ?

Thank you.

Guyverix commented 2 years ago

Still learning this library myself, but I notice that you do not have the loop right after the walk to get all the returns..

I think you still need this from the example:

# Keep the walk going until there are no more OIDs left
while($walk->hasOids()) {
    try {
        # Get the next OID in the walk
        $oid = $walk->next();
        echo sprintf("%s = %s", $oid->getOid(), $oid->getValue()).PHP_EOL;
    } catch (\Exception $e) {
        # If we had an issue, display it here (network timeout, etc)
        echo "Unable to retrieve OID. ".$e->getMessage().PHP_EOL;
    }
}
php ./test2.php 
1.3.6.1.2.1.2.2.1.1.1 = 1
1.3.6.1.2.1.2.2.1.1.2 = 2
1.3.6.1.2.1.2.2.1.1.3 = 3
ChadSikorra commented 2 years ago

Still learning this library myself, but I notice that you do not have the loop right after the walk to get all the returns..

I think you still need this from the example:

# Keep the walk going until there are no more OIDs left
while($walk->hasOids()) {
    try {
        # Get the next OID in the walk
        $oid = $walk->next();
        echo sprintf("%s = %s", $oid->getOid(), $oid->getValue()).PHP_EOL;
    } catch (\Exception $e) {
        # If we had an issue, display it here (network timeout, etc)
        echo "Unable to retrieve OID. ".$e->getMessage().PHP_EOL;
    }
}
php ./test2.php 
1.3.6.1.2.1.2.2.1.1.1 = 1
1.3.6.1.2.1.2.2.1.1.2 = 2
1.3.6.1.2.1.2.2.1.1.3 = 3

Yes, this is the intended way of performing a walk for his case. It's interactive, so just instantiating it is not enough. You must iteratively call hasOids() and next().

ChadSikorra commented 2 years ago

Closing this out since it was answered. Please re-open if you're still having issues. Thanks!