Welcome to PCCS-Linux.COM Hello, and welcome to the PCCS-Linux Home Page.
Your last visit was !!!
Your Browser is running on Other
 
SNMP Functions
I started using this function in an effort to grab dynamic data from the router and switches I help manage.

The functions that I use the most are;

To use these function you must compile php by running configure;

        ./configure --with-snmp --enable-ucd-snmp-hack

        snmpget();
        snmpwalk();
        snmpwalkoid();
        snmpset();

Using some of the above functions

<?
function queryRouter($hostname) {

/* This snmpwalk functions allows me to create an array of the interface
on the device */

$_mibvars =
snmpwalk("$hostname","$community","interfaces.ifTable.ifEntry.ifIndex");

/* Do this until we run out of interfaces */
for($i=0; count($_mibvars); $i++) 
        {

        /* Using snmpget */
        $sysdescr = snmpget("$hostname","$community","system.sysDescr.0");
        $syscontact =
snmpget("$hostname","$community","system.sysContact.0");
        $sysuptime =
snmpget("$hostname","$community","system.sysUptime.0");
        $syslocation =
snmpget("$hostname","$community","system.sysLocation.0");
        $interface[] =
snmpget("$hostname","$community","interfaces.ifTable.ifEntry.ifDescr.$_mibvars[$i]");
        $interface_type[] =
snmpget("$hostname","$community","interfaces.ifTable.ifEntry.ifType.$_mibvars[$i]");

        }

print"
<table width=600 cellspacing=2>
<tr>
<td>$sysdescr</td><td>$syscontact</td>
<td>$sysuptime</td><td>$syslocation</td>
<td>$sysdescr</td><td>$syscontact</td>
<td>$sysuptime</td><td>$syslocation</td>
</tr>
</table>
";


print "
<table width=600 cellspacing=2>
<tr>
<td>";
for($i=0; $i<count(interface); $i++) {
        echo $interface[$i];
}
print "
</td><td>";
for($i=0; $i<count(interface); $i++) {
        echo $interface_type[$i];
}
print "
</td>
</tr>
</table>
";
}

$hostname = "";
        queryRouter($hostname);


?>

There are times when you may want to modify values remotely
Create a form with a contact and location input name and post them to the the example code below..
<?

$updated_contact = snmpset("$hostname","$community","system.sysContact.0",
s , "$contact");
$updated_location =
snmpset("$hostname","$community","system.sysLocation.0", s , "$location");

echo "Contact Changed to: $updated_contact <BR>\n";
echo "Location Changed to: $updated_location <BR>\n";

?>

Here I will create a function and pass it a ip address
Then create a variable and assign it a value, and using echo to display the data.

Using the snmpwalkoid() function

function snmpwalkhost($ipaddr) {
$a = snmpwalkoid("$ipaddr", "public", "");
for (reset($a); $i = key($a); next($a)) {
       echo "$i: $a[$i]<br>\n";
    }
 } 
print "<p>";   
snmpwalkhost($ipaddr);

>> Comments/FeedBack


Page hits 122 last request on :02:37



This web site is prepared and maintained by Chauncey Thorn.
The information contained in this site was valid at the time of posting.
Chauncey Thorn, assumes no liability for damages incurred directly or indirectly as a result of errors, omissions or discrepancies.

Remember most of the content here are my notes...

All logos and trademarks in this site are property of their respective owner. All the rest © by PCCS-Linux.COM

PCCS-Linux.COM ::ource Advocate Articles catalogue
2000 2002