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 > out.txt

This command will get all SNMP information from router 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. 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.");

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

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

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

Similarly get EPON interface id from OID: iso.