How to get SMNP information from router using PHP

First install SNMP package:

sudo apt-get install snmp

To use SNMP in PHP install php-snmp extention:

sudo apt-get install -y php-snmp

Get SNMP information from router user command:

snmpwalk -v2c -Cc -c public 192.168.1.1 > out.txt

This command will get all SNMP information from router 192.168.1.1 and save it into file out.txt

Now let’s create file index.php and write code that will get all MAC addresses from BDCOM P3310 OLT. To get all MAC addresses use OID: iso.3.6.1.2.1.17.7.1.2.2.1.1. In PHP file create function SnmpGet:

function SnmpGet($host, $community, $object_id){
    exec("snmpwalk  -v2c -Cc -c ".$community." ".$host." ".$object_id, $snmpWalkReturn);
    foreach($snmpWalkReturn as $value){
            $oid = explode("=", $value);
            $walk[trim($oid[0])] = trim($oid[1]);
    }
    return $walk;
}

Put SNMP result in variable $MacResult:

$MacResult = snmprealwalk2($hostname, $community, "iso.3.6.1.2.1.17.7.1.2.2.1.1");

In cycle foreach go through all elements and remove unnecessary data:

if(count($MacResult)){
    foreach($MacResult as $Item => $Value)
    {
        $Oid = str_replace("iso.3.6.1.2.1.17.7.1.2.2.1.1.", "", $Item);
        $Mac = str_replace("Hex-STRING: ", "", $Value);

        if(!strpos($Mac, "STRING:")){
            echo $Mac.' | '.$Oid;
        }
    }
}

Similarly get EPON interface id from OID: iso.3.6.1.2.1.17.7.1.2.2.1.2