100% encontró este documento útil (1 voto)
81 vistas40 páginas

Configuración y Ejemplos de PHP SNMP

Este documento proporciona instrucciones para instalar y configurar PHP SNMP en Windows. Explica cómo descargar e instalar PHP, configurar el archivo php.ini, copiar la carpeta mibs y agregar extensiones. También muestra ejemplos de código PHP para realizar operaciones SNMP como snmpget, snmpwalk y tablas.

Cargado por

Rizkhy Ramadhani
Derechos de autor
© All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PPT, PDF, TXT o lee en línea desde Scribd
100% encontró este documento útil (1 voto)
81 vistas40 páginas

Configuración y Ejemplos de PHP SNMP

Este documento proporciona instrucciones para instalar y configurar PHP SNMP en Windows. Explica cómo descargar e instalar PHP, configurar el archivo php.ini, copiar la carpeta mibs y agregar extensiones. También muestra ejemplos de código PHP para realizar operaciones SNMP como snmpget, snmpwalk y tablas.

Cargado por

Rizkhy Ramadhani
Derechos de autor
© All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PPT, PDF, TXT o lee en línea desde Scribd

PHP SNMP (Windows)

Yen-Cheng Chen
Department of Information Management
National Chi Nan University

References
PHP

[Link]

PHP Manual: SNMP

[Link]

Example Programs

[Link]

PHP 5 Installation (Windows)


Download (Windows Installer)
[Link]

p
[Link]

PHP
Download (Manual Installation)
[Link]

Extract the distribution file into a directory.

C:\php

add C:\php to the PATH


Set up configuration file [Link]

Find [Link]-recommended in C:\php


Copy to [Link] and modify it.

5
C:\php\;
2
3

PHP SNMP Configuration


[Link]

;extension=php_snmp.dll
extension=php_snmp.dll
extension_dir = "C:/xampp/php/ext"

Copy mibs directory

Copy C:\php\extras\mibs to C:\usr


C:\usr\mibs

If you have troubles in using GetIf after installing

php_snmp,

[Link]
Uncompress it to C:\usr\mibs to overwrite some mib files.

C:\>php -v

snmp_set_quick_print

[Link]

<?php
snmp_set_quick_print(true);
$sysUpTime=snmpget("[Link]", "public", "[Link].0");
$sOID=snmpget("[Link]", "public", "[Link].0");
echo "$sysUpTime\n";
echo "$sOID\n";
snmp_set_quick_print(false);
$sysUpTime=snmpget("[Link]", "public", "[Link].0");
$sOID=snmpget("[Link]", "public", "[Link].0");
echo "$sysUpTime\n";
echo "$sOID\n";
C:\snmp>php [Link]
?>
13:11:40:00.61
enterprises.[Link].1.1
Timeticks: (116520061) 13 days,
11:40:00.61
OID: enterprises.[Link].1.1

snmp_set_valueretrieval

[Link]

<?php
snmp_set_quick_print(true);
snmp_set_valueretrieval(SNMP_VALUE_LIBRARY);
$ipForwarding = snmpget("[Link]", "public", ".[Link].[Link].0");
echo "SNMP_VALUE_LIBRARY: $ipForwarding\n";
snmp_set_valueretrieval(SNMP_VALUE_PLAIN);
$ipForwarding = snmpget("[Link]", "public", ".[Link].[Link].0");
echo "SNMP_VALUE_PLAIN: $ipForwarding\n";
snmp_set_valueretrieval(SNMP_VALUE_OBJECT);
$ipForwarding = snmpget("[Link]", "public", ".[Link].[Link].0");
echo "SNMP_VALUE_OBJECT:\n";
SNMP_VALUE_LIBRARY: notForwarding
print_r($ipForwarding);
SNMP_VALUE_PLAIN: 2
//echo $ipForwarding->value;
SNMP_VALUE_OBJECT:
stdClass Object
?>
(
[type] => 2
[value] => 2
)

ipForwarding OBJECT-TYPE
SYNTAX INTEGER {
forwarding(1),
-- acting as a gateway
not-forwarding(2) -- NOT acting as a gateway
}
ACCESS read-write
STATUS mandatory
DESCRIPTION
"The indication of whether this entity is acting
as an IP gateway in respect to the forwarding of
datagrams received by, but not addressed to, this
entity. IP gateways forward datagrams. ..."
::= { ip 1 }

snmpget

[Link]

string snmpget ( string $hostname, string $community,


string $object_id [, int $timeout [, int $retries ]] )
time unit of timeout: 10-6 sec (s)
<?php
$host = "[Link]";
$community = "public";
$descr = snmpget($host, $community, "sysDescr.0");
echo "$descr\n";
$descr = snmpget($host, $community, "system.1.0");
echo "$descr\n";
$descr = snmpget($host, $community, ".[Link].[Link].0");
echo "$descr\n";
?>
HOST-RESOURCES-MIB::[Link].1.0

HOST-RESOURCES-MIB::hrSystemUptime.0

Use of $argv

[Link]

<?php
$host = $argv[1];
$community = $argv[2];
$oid=$argv[3];
$val = snmpget($host, $community, $oid);
echo "$oid = $val\n";
?>
$argv[0]

$argv[1]

$argv[2] $argv[3]

C:\phpSNMP>php [Link] [Link] public [Link].0


[Link].0 = 128

snmpgetnext

[Link]

string snmpgetnext ( string $host , string $community,


string $object_id [, int $timeout [, int $retries ]] )
<?php
$host = "[Link]";
$community = "nm2008";
snmp_set_quick_print(true);
$sysOID = snmpgetnext($host, $community, "sysDescr.0");
echo "$sysOID\n";
?>
C:\phpSNMP>php [Link]
enterprises.[Link].11

snmpset

[Link]

bool snmpset ( string $hostname , string $community ,


string $object_id , string $type , mixed $value [, int
$timeout [, int $retries ]] )
<?php
$host = "[Link]";
$community = "private";
$oid = ".[Link].[Link].[Link].65539";
$isSet = snmpset($host, $community, $oid, "i", 2);
if ($isSet) {
$adminStatus = snmpget($host, $community, $oid);
echo "$adminStatus\n";
}
?>
"x", "00 1a 30 74 e4 1b"

$type:
i INTEGER
u unsigned INTEGER
t TIMETICKS
a IPADDRESS
o OBJID
s STRING
x HEX STRING
d DECIMAL STRING

snmpwalk

[Link]

array snmpwalk ( string $hostname , string


$community , string $object_id [, int $timeout
[, int $retries ]] )
<?php
snmp_set_quick_print(true);
$host = "[Link]";
$community = "public";
$oid = "[Link]";
$arr = snmpwalk($host, $community, $oid);
print_r($arr);
?>

Array
(
[0] => 1
[1] => 65539
[2] => 65540
[3] => MS TCP Loopback interface
[4] => Intel(R) PRO/Wireless 2200BG Network Connection
[5] => Realtek RTL8139 Family PCI Fast Ethernet NIC
[6] => softwareLoopback
[7] => ethernetCsmacd
[8] => ethernetCsmacd
[9] => 1520
[10] => 1500
[11] => 1500
[12] => 10000000
[13] => 54000000
[14] => 100000000
[15] =>
[16] => 0:12:f0:9c:1d:2e
[17] => 0:13:d4:6a:ea:8d

snmprealwalk

[Link]

<?php
snmp_set_quick_print(true);
$host = "[Link]";
$community = "public";
$initOid = "[Link]";
$arr = snmprealwalk($host, $community, $initOid);
foreach ($arr as $oid => $val) {
echo "$oid = $val\n";
}
// print_r($arr);
?>

[Link].1 = 1
[Link].65539 = 65539
[Link].65540 = 65540
[Link].1 = MS TCP Loopback interface
[Link].65539 = Intel(R) PRO/Wireless 2200BG Net
[Link].65540 = Realtek RTL8139 Family PCI Fast
[Link].1 = softwareLoopback
[Link].65539 = ethernetCsmacd
[Link].65540 = ethernetCsmacd
[Link].1 = 1520
[Link].65539 = 1500
[Link].65540 = 1500
[Link].1 = 10000000
[Link].65539 = 54000000
[Link].65540 = 100000000
[Link].1 =
[Link].65539 = 0:12:f0:9c:1d:2e
[Link].65540 = 0:13:d4:6a:ea:8d

Retrieval of a Table
Define a function named snmptable
function snmptable($host, $comm, $oid, $numCols) {
for ($i=1;$i<=$numCols;$i++) {
$arr[$i] = snmpwalk($host, $comm, "$oid.1.$i");
$ret[$i] = $arr[$i];
}
$numRows = count($ret[1]);
for ($i=1; $i<=$numRows; $i++) {
for ($j=1;$j<=$numCols;$j++)
$table[$i][$j] = $ret[$j][$i-1];
}
return $table;
}

$numCols: number of columns in the table

[Link]

Table as a 2-dimensioal Array

snmp_set_quick_print(true);
$host = "[Link]";
$community = public";
$oid = "[Link]";
$arr = snmptable($host, $community, $oid, 4);
for ($i=1; $i<=count($arr); $i++)
echo "{$arr[$i][1]}\t{$arr[$i][2]}\t{$arr[$i][3]}\t{$arr[$i][4]}\n";

65540 0:8:9b:a9:a8:cf
65540 0:8:9b:a9:a8:b7
65540 0:1a:30:74:e4:0

[Link]
[Link]
[Link]

dynamic
dynamic
dynamic

snmptable Example
Column 1
Row 1

function PadMAC($mac) {
$mac_arr = explode(':',$mac);
foreach($mac_arr as $atom) {
$atom = trim($atom);
$newarr[] = sprintf("%02s",$atom);
}
$newmac = implode(':',$newarr);
return $newmac;
}

[Link]

for ($i=1; $i <= count($arr); $i++) {


$mac = PadMAC($arr[$i][2]);
echo "{$arr[$i][1]}\t$mac\t{$arr[$i][3]}\t{$arr[$i][4]}\n";
}
65540 00:08:9b:a9:a8:cf [Link]
65540 00:08:9b:a9:a8:b7 [Link]
65540 00:1a:30:74:e4:00 [Link]

dynamic
dynamic
dynamic

realtable
function realtable($host, $comm, $oid, $numCols) {

for ($j=1; $j <= $numCols; $j++) {


[Link]
$arr[$j] = snmprealwalk($host, $comm, "$oid.1.$j");
$ret[$j] = $arr[$j];
}
for ($j=1; $j <= $numCols; $j++) {
$i=1;
foreach ($ret[$j] as $oid => $val) {
$oTable[$i][$j] = explode(".", $oid);
$table[$i][$j] = $val;
$i++;
}
function col($arr1, $arr2) {
}
$i=0;
$cl = col($oTable[1][1], $oTable[1][2]);
while
$table[0][0] = "Index";
($arr1[$i]==$arr2[$i])
for ($j=1; $j <= $numCols; $j++)
$i++;
$table[0][$j] = $oTable[1][$j][$cl];
return $i;
for ($i=1; $i <= count($ret[1]); $i++) {
}
$iId = "";
for ($j=($cl+1); $j<count($oTable[$i][1]); $j++)
$iId = $iId . "." . $oTable[$i][1][$j];
$table[$i][0] = substr($iId,1);
}
return $table;

realtable Example
Column 0 (Instance Identifier)
Row 0

C:\snmp\php [Link] > [Link]

function getCols($host, $comm, $oid, $colArr)


$colArr {
$j=1;
foreach ($colArr as $id) {
[Link]
$arr[$j] = snmprealwalk($host, $comm, "$oid.1.$id");
$ret[$j] = $arr[$j];
$j++;
}
for ($j=1; $j <= count($colArr); $j++) {
$i=1;
foreach ($ret[$j] as $oid => $val) {
$oTable[$i][$j]=explode(".",$oid);
$table[$i][$j]=$val;
$i++;
$host = "[Link]";
}
$community = public";
}
$oid = "ipRouteTable";
$cl = col($oTable[1][1], $oTable[1][2]);
$idArr = array(1, 2, 3, 11, 8, 7);
$table[0][0] = "Index";
$arr = getCols($host, $community, $oid, $idArr);
for ($j=1; $j <= count($colArr); $j++)
$table[0][$j] = $oTable[1][$j][$cl];
for ($i=1;$i <= count($ret[1]);$i++) {
$iId ="";
for ($j=($cl+1);$j < count($oTable[$i][1]);$j++)
$iId = $iId . "." . $oTable[$i][1][$j];
$table[$i][0] = substr($iId,1);
}
$colArr:
$colArr array of column IDs
return $table;
}

Get columns 1, 2, 3, 11, 8, and 7 of the ipRouteTable table

C:\snmp\php [Link] > [Link]

snmp_read_mib

[Link]

<?php

snmp_set_quick_print(true);
snmp_read_mib("LanMgr-Mib-II-MIB");
$host = "[Link]";
$community = "public";
$oid = "LanMgr-Mib-II-MIB::svSvcTable";
$arr = realtable($host, $community, $oid, 5);
htmlTable($arr);

SNMPv2c
string snmp2_get(string host, string community, string
object_id [, int timeout [, int retries]]);
string snmp2_getnext(string host, string community, string
object_id [, int timeout [, int retries]]);
array snmp2_walk(string host, string community, string
object_id [, int timeout [, int retries]]);
array snmp2_real_walk(string host, string community,
string object_id [, int timeout [, int retries]]);
int snmp2_set(string host, string community, string
object_id, string type, mixed value [, int timeout [, int
retries]]);

PHP SNMP on Web


Web-based SNMP applications developed in php.
XAMPP

X (meaning any operating system)


Apache (web server)
MySQL (database)
PHP
Perl

XAMPP for Windows

[Link]

Download

[Link]

XAMPP Installation
Suppose XAMPP is installed in C:\xampp
Document Root: C:\xampp\htdocs
[Link]: C:\xampp\apache\conf\[Link]
[Link]: C:\xampp\php\[Link]
extension dir: C:\xampp\php\ext
MIB dir: C:\usr\mibs
(copy C:\xampp\php\extras\mibs to C:\usr\mibs)

C:\xampp\php\[Link]
;extension=php_snmp.dll
extension=php_snmp.dll

Find php_snmp.dll in C:\xampp\php\ext

php_snmp.dll
[Link]

[Link] Configuration
Configure a virtual directory for your SNMP application.
Suppose

[Link]
Virtual directory: nmapp1
Physical directory: C:\snmp\app1
<IfModule alias_module>
...
Alias /nmapp1 "C:/snmp/app1"
...
</IfModule>
<Directory "C:/snmp/app1">
AllowOverride None
Options None
Require all granted
</Directory>

Run XAMPP

[Link]
<form method="post"
post action="[Link]">
[Link]
<table width=400 border=3 cellspacing=2 cellpadding=2
bgColor="#ffffcc" align=center>
<tr><td align=right>Host: </td>
<td> <input type=text name="host"></td></tr>
<tr><td align=right>Community Name: </td>
<td> <input type=text name="comm"></td></tr>
<tr><td align=right>OID: </td>
<td> <input type=text name="oid"></td></tr>
<tr><td align=center colspan=2>
<input type=submit value="OK">
<input type=reset value="Cancel"></td></tr>
</table>
</form>

[Link]

[Link]
<?php
snmp_set_quick_print(true);
$host = $_POST['host'];
$comm = $_POST['comm'];
$oid = $_POST['oid'];
if ($_POST['comm']=="") $comm="public";
$arr = snmprealwalk($host, $comm, $oid);
echo "<h3 align=center>SNMPWALK of $host</h3>";
echo "<table align=center cellspacing=2 border=2>";
foreach ($arr as $oid => $val)
echo "<tr><td bgColor=#ffffcc>$oid</td><td
bgColor=#ccffff>$val</td></tr>";
?>
</table>

También podría gustarte