PHP SNMP Windows YenCheng Chen Department of Information

  • Slides: 40
Download presentation
PHP SNMP (Windows) Yen-Cheng Chen Department of Information Management National Chi Nan University

PHP SNMP (Windows) Yen-Cheng Chen Department of Information Management National Chi Nan University

References n PHP n http: //www. php. net/ n PHP Manual: SNMP n http:

References n PHP n http: //www. php. net/ n PHP Manual: SNMP n http: //www. php. net/manual/en/book. snmp. php n Example Programs n http: //ycchen. im. ncnu. edu. tw/nm/phpsnmp. zip

PHP 5 Installation (Windows) n Download (Windows Installer) n http: //windows. php. net/downloads/releases/php-5. 4.

PHP 5 Installation (Windows) n Download (Windows Installer) n http: //windows. php. net/downloads/releases/php-5. 4. 15 -nts-Win 32 -VC 9 - x 86. zip n http: //ycchen. im. ncnu. edu. tw/php-5. 4. 15 -nts-Win 32 -VC 9 -x 86. zip

手動安裝PHP n Download (Manual Installation) http: //windows. php. net/downloads/releases/php-5. 2. 17 -nts-Win 32 -VC

手動安裝PHP n Download (Manual Installation) http: //windows. php. net/downloads/releases/php-5. 2. 17 -nts-Win 32 -VC 6 x 86. zip n Extract the distribution file into a directory. n C: php n add C: php to the PATH n Set up configuration file php. ini n Find php. ini-recommended in C: php n Copy to php. ini and modify it.

PHP SNMP Configuration n php. ini n ; extension=php_snmp. dll n extension_dir = "C:

PHP SNMP Configuration n php. ini n ; extension=php_snmp. dll n extension_dir = "C: /xampp/php/ext" n Copy mibs directory n Copy C: phpextrasmibs to C: usr n C: usrmibs n If you have troubles in using Get. If after installing php_snmp, n n http: //ycchen. im. ncnu. edu. tw/nm/macro. Removed. zip Uncompress it to C: usrmibs to overwrite some mib files.

C: >php -v

C: >php -v

snmp_set_quick_print ssqp. php <? php snmp_set_quick_print(true); $sys. Up. Time=snmpget("127. 0. 0. 1", "public", "system.

snmp_set_quick_print ssqp. php <? php snmp_set_quick_print(true); $sys. Up. Time=snmpget("127. 0. 0. 1", "public", "system. sys. Up. Time. 0"); $s. OID=snmpget("127. 0. 0. 1", "public", "system. sys. Object. ID. 0"); echo "$sys. Up. Timen"; echo "$s. OIDn"; snmp_set_quick_print(false); $sys. Up. Time=snmpget("127. 0. 0. 1", "public", "system. sys. Up. Time. 0"); $s. OID=snmpget("127. 0. 0. 1", "public", "system. sys. Object. ID. 0"); echo "$sys. Up. Timen"; echo "$s. OIDn"; C: snmp>php ssqp. php ? > 13: 11: 40: 00. 61 enterprises. 311. 1. 1. 3. 1. 1 Timeticks: (116520061) 13 days, 11: 40: 00. 61 OID: enterprises. 311. 1. 1. 3. 1. 1

snmp_set_valueretrieval ssvr. php <? php snmp_set_quick_print(true); snmp_set_valueretrieval(SNMP_VALUE_LIBRARY); $ip. Forwarding = snmpget("127. 0. 0. 1",

snmp_set_valueretrieval ssvr. php <? php snmp_set_quick_print(true); snmp_set_valueretrieval(SNMP_VALUE_LIBRARY); $ip. Forwarding = snmpget("127. 0. 0. 1", "public", ". 1. 3. 6. 1. 2. 1. 4. 1. 0"); echo "SNMP_VALUE_LIBRARY: $ip. Forwardingn"; snmp_set_valueretrieval(SNMP_VALUE_PLAIN); $ip. Forwarding = snmpget("127. 0. 0. 1", "public", ". 1. 3. 6. 1. 2. 1. 4. 1. 0"); echo "SNMP_VALUE_PLAIN: $ip. Forwardingn"; snmp_set_valueretrieval(SNMP_VALUE_OBJECT); $ip. Forwarding = snmpget("127. 0. 0. 1", "public", ". 1. 3. 6. 1. 2. 1. 4. 1. 0"); echo "SNMP_VALUE_OBJECT: n"; SNMP_VALUE_LIBRARY: not. Forwarding print_r($ip. Forwarding); SNMP_VALUE_PLAIN: 2 //echo $ip. Forwarding->value; SNMP_VALUE_OBJECT: std. Class Object ? > ( [type] => 2 [value] => 2 )

ip. Forwarding OBJECT-TYPE SYNTAX INTEGER { forwarding(1), -- acting as a gateway not-forwarding(2) --

ip. Forwarding 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 1. php string snmpget ( string $hostname, string $community, string $object_id [, int

snmpget 1. php string snmpget ( string $hostname, string $community, string $object_id [, int $timeout [, int $retries ]] ) time unit of timeout: 10 -6 sec (μs) <? php $host = "127. 0. 0. 1"; $community = "public"; $descr = snmpget($host, $community, "sys. Descr. 0"); echo "$descrn"; $descr = snmpget($host, $community, "system. 1. 0"); echo "$descrn"; $descr = snmpget($host, $community, ". 1. 3. 6. 1. 2. 1. 1. sys. Descr. 0"); echo "$descrn"; ? > HOST-RESOURCES-MIB: : host. hr. System. 1. 0 HOST-RESOURCES-MIB: : hr. System. Uptime. 0

Use of $argv get 2. php <? php $host = $argv[1]; $community = $argv[2];

Use of $argv get 2. php <? php $host = $argv[1]; $community = $argv[2]; $oid=$argv[3]; $val = snmpget($host, $community, $oid); echo "$oid = $valn"; ? > $argv[0] $argv[1] $argv[2] $argv[3] C: php. SNMP>php get 2. php 127. 0. 0. 1 public ip. ip. Default. TTL. 0 = 128

snmpgetnext. php string snmpgetnext ( string $host , string $community, string $object_id [, int

snmpgetnext. php string snmpgetnext ( string $host , string $community, string $object_id [, int $timeout [, int $retries ]] ) <? php $host = "163. 22. 34. 197"; $community = "nm 2008"; snmp_set_quick_print(true); $sys. OID = snmpgetnext($host, $community, "sys. Descr. 0"); echo "$sys. OIDn"; ? > C: php. SNMP>php getnext. php enterprises. 171. 10. 36. 1. 11

snmpset set. php bool snmpset ( string $hostname , string $community , string $object_id

snmpset set. php bool snmpset ( string $hostname , string $community , string $object_id , string $type , mixed $value [, int $timeout [, int $retries ]] ) <? php $host = "127. 0. 0. 1"; $community = "private"; $oid = ". 1. 3. 6. 1. 2. 2. 1. if. Admin. Status. 65539"; $is. Set = snmpset($host, $community, $oid, "i", 2); if ($is. Set) { $admin. Status = snmpget($host, $community, $oid); echo "$admin. Statusn"; } ? > "x", "00 1 a 30 74 e 4 1 b" $type: i INTEGER u unsigned INTEGER t TIMETICKS a IPADDRESS o OBJID s STRING x HEX STRING d DECIMAL STRING

snmpwalk. php array snmpwalk ( string $hostname , string $community , string $object_id [,

snmpwalk. php array snmpwalk ( string $hostname , string $community , string $object_id [, int $timeout [, int $retries ]] ) <? php snmp_set_quick_print(true); $host = "127. 0. 0. 1"; $community = "public"; $oid = "interfaces. if. Table"; $arr = snmpwalk($host, $community, $oid); print_r($arr); ? >

Array ( [0] => 1 [1] => 65539 [2] => 65540 [3] => MS

Array ( [0] => 1 [1] => 65539 [2] => 65540 [3] => MS TCP Loopback interface [4] => Intel(R) PRO/Wireless 2200 BG Network Connection [5] => Realtek RTL 8139 Family PCI Fast Ethernet NIC [6] => software. Loopback [7] => ethernet. Csmacd [8] => ethernet. Csmacd [9] => 1520 [10] => 1500 [11] => 1500 [12] => 10000000 [13] => 54000000 [14] => 10000 [15] => [16] => 0: 12: f 0: 9 c: 1 d: 2 e [17] => 0: 13: d 4: 6 a: ea: 8 d …

snmprealwalk. php <? php snmp_set_quick_print(true); $host = "127. 0. 0. 1"; $community = "public";

snmprealwalk. php <? php snmp_set_quick_print(true); $host = "127. 0. 0. 1"; $community = "public"; $init. Oid = "interfaces. if. Table"; $arr = snmprealwalk($host, $community, $init. Oid); foreach ($arr as $oid => $val) { echo "$oid = $valn"; } // print_r($arr); ? >

interfaces. if. Table. if. Entry. if. Index. 1 = 1 interfaces. if. Table. if.

interfaces. if. Table. if. Entry. if. Index. 1 = 1 interfaces. if. Table. if. Entry. if. Index. 65539 = 65539 interfaces. if. Table. if. Entry. if. Index. 65540 = 65540 interfaces. if. Table. if. Entry. if. Descr. 1 = MS TCP Loopback interfaces. if. Table. if. Entry. if. Descr. 65539 = Intel(R) PRO/Wireless 2200 BG Net… interfaces. if. Table. if. Entry. if. Descr. 65540 = Realtek RTL 8139 Family PCI Fast … interfaces. if. Table. if. Entry. if. Type. 1 = software. Loopback interfaces. if. Table. if. Entry. if. Type. 65539 = ethernet. Csmacd interfaces. if. Table. if. Entry. if. Type. 65540 = ethernet. Csmacd interfaces. if. Table. if. Entry. if. Mtu. 1 = 1520 interfaces. if. Table. if. Entry. if. Mtu. 65539 = 1500 interfaces. if. Table. if. Entry. if. Mtu. 65540 = 1500 interfaces. if. Table. if. Entry. if. Speed. 1 = 10000000 interfaces. if. Table. if. Entry. if. Speed. 65539 = 54000000 interfaces. if. Table. if. Entry. if. Speed. 65540 = 10000 interfaces. if. Table. if. Entry. if. Phys. Address. 1 = interfaces. if. Table. if. Entry. if. Phys. Address. 65539 = 0: 12: f 0: 9 c: 1 d: 2 e interfaces. if. Table. if. Entry. if. Phys. Address. 65540 = 0: 13: d 4: 6 a: ea: 8 d …

Retrieval of a Table Define a function named “snmptable” function snmptable($host, $comm, $oid, $num.

Retrieval of a Table Define a function named “snmptable” function snmptable($host, $comm, $oid, $num. Cols) { for ($i=1; $i<=$num. Cols; $i++) { $arr[$i] = snmpwalk($host, $comm, "$oid. 1. $i"); $ret[$i] = $arr[$i]; } $num. Rows = count($ret[1]); for ($i=1; $i<=$num. Rows; $i++) { for ($j=1; $j<=$num. Cols; $j++) $table[$i][$j] = $ret[$j][$i-1]; } return $table; } $num. Cols: number of columns in the table

snmptable 1. php Table as a 2 -dimensioal Array snmp_set_quick_print(true); $host = "127. 0.

snmptable 1. php Table as a 2 -dimensioal Array snmp_set_quick_print(true); $host = "127. 0. 0. 1"; $community = “public"; $oid = "ip. Net. To. Media. Table"; $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: 9 b: a 9: a 8: cf 65540 0: 8: 9 b: a 9: a 8: b 7 65540 0: 1 a: 30: 74: e 4: 0 10. 13. 192 10. 13. 196 10. 13. 254 dynamic

snmptable Example Column 1 Row 1

snmptable Example Column 1 Row 1

function Pad. MAC($mac) { $mac_arr = explode(': ', $mac); foreach($mac_arr as $atom) { $atom

function Pad. MAC($mac) { $mac_arr = explode(': ', $mac); foreach($mac_arr as $atom) { $atom = trim($atom); $newarr[] = sprintf("%02 s", $atom); } $newmac = implode(': ', $newarr); return $newmac; } snmptable 2. php … for ($i=1; $i <= count($arr); $i++) { $mac = Pad. MAC($arr[$i][2]); echo "{$arr[$i][1]}t$mact{$arr[$i][3]}t{$arr[$i][4]}n"; } 65540 00: 08: 9 b: a 9: a 8: cf 10. 13. 192 65540 00: 08: 9 b: a 9: a 8: b 7 10. 13. 196 65540 00: 1 a: 30: 74: e 4: 00 10. 13. 254 dynamic

realtable function realtable($host, $comm, $oid, $num. Cols) { realtable. php for ($j=1; $j <=

realtable function realtable($host, $comm, $oid, $num. Cols) { realtable. php for ($j=1; $j <= $num. Cols; $j++) { $arr[$j] = snmprealwalk($host, $comm, "$oid. 1. $j"); $ret[$j] = $arr[$j]; } for ($j=1; $j <= $num. Cols; $j++) { $i=1; foreach ($ret[$j] as $oid => $val) { $o. Table[$i][$j] = explode(". ", $oid); $table[$i][$j] = $val; $i++; } function col($arr 1, $arr 2) { } $i=0; $cl = col($o. Table[1][1], $o. Table[1][2]); while ($arr 1[$i]==$arr 2[$i]) $table[0][0] = "Index"; $i++; for ($j=1; $j <= $num. Cols; $j++) return $i; $table[0][$j] = $o. Table[1][$j][$cl]; } for ($i=1; $i <= count($ret[1]); $i++) { $i. Id = ""; for ($j=($cl+1); $j<count($o. Table[$i][1]); $j++) $i. Id = $i. Id. ". ". $o. Table[$i][1][$j]; $table[$i][0] = substr($i. Id, 1); } return $table; }

realtable Example Column 0 (Instance Identifier) Row 0 C: snmpphp realtable. php > ip.

realtable Example Column 0 (Instance Identifier) Row 0 C: snmpphp realtable. php > ip. NTM. html

function get. Cols($host, $comm, $oid, $col. Arr) { $col. Arr $j=1; foreach ($col. Arr

function get. Cols($host, $comm, $oid, $col. Arr) { $col. Arr $j=1; foreach ($col. Arr as $id) { get. Cols. php $arr[$j] = snmprealwalk($host, $comm, "$oid. 1. $id"); $ret[$j] = $arr[$j]; $j++; } for ($j=1; $j <= count($col. Arr); $j++) { $i=1; foreach ($ret[$j] as $oid => $val) { $o. Table[$i][$j]=explode(". ", $oid); $table[$i][$j]=$val; $i++; $host = "127. 0. 0. 1"; } $community = “public"; } $oid = "ip. Route. Table"; $cl = col($o. Table[1][1], $o. Table[1][2]); $id. Arr = array(1, 2, 3, 11, 8, 7); $table[0][0] = "Index"; $arr = get. Cols($host, $community, $oid, $id. Arr); for ($j=1; $j <= count($col. Arr); $j++) $table[0][$j] = $o. Table[1][$j][$cl]; for ($i=1; $i <= count($ret[1]); $i++) { $i. Id =""; for ($j=($cl+1); $j < count($o. Table[$i][1]); $j++) $i. Id = $i. Id. ". ". $o. Table[$i][1][$j]; $table[$i][0] = substr($i. Id, 1); } $col. Arr: $col. Arr array of column IDs return $table; }

Get columns 1, 2, 3, 11, 8, and 7 of the ip. Route. Table

Get columns 1, 2, 3, 11, 8, and 7 of the ip. Route. Table table C: snmpphp get. Cols. php > ip. Route. html

snmp_read_mib readmib. php <? php snmp_set_quick_print(true); snmp_read_mib("Lan. Mgr-Mib-II-MIB"); $host = "127. 0. 0. 1";

snmp_read_mib readmib. php <? php snmp_set_quick_print(true); snmp_read_mib("Lan. Mgr-Mib-II-MIB"); $host = "127. 0. 0. 1"; $community = "public"; $oid = "Lan. Mgr-Mib-II-MIB: : sv. Svc. Table"; $arr = realtable($host, $community, $oid, 5); html. Table($arr); …

SNMPv 2 c string snmp 2_get(string host, string community, string object_id [, int timeout

SNMPv 2 c string snmp 2_get(string host, string community, string object_id [, int timeout [, int retries]]); string snmp 2_getnext(string host, string community, string object_id [, int timeout [, int retries]]); array snmp 2_walk(string host, string community, string object_id [, int timeout [, int retries]]); array snmp 2_real_walk(string host, string community, string object_id [, int timeout [, int retries]]); int snmp 2_set(string host, string community, string object_id, string type, mixed value [, int timeout [, int retries]]);

PHP SNMP on Web-based SNMP applications developed in php. n XAMPP n X (meaning

PHP SNMP on Web-based SNMP applications developed in php. n XAMPP n X (meaning any operating system) n Apache (web server) n My. SQL (database) n PHP n Perl n XAMPP for Windows n http: //www. apachefriends. org/en/xampp-windows. html n Download n http: //ycchen. im. ncnu. edu. tw/xampp-win 32 -1. 8. 1 -VC 9 -installer. exe

XAMPP Installation n Suppose XAMPP is installed in C: xampp Document Root: C: xampphtdocs

XAMPP Installation n Suppose XAMPP is installed in C: xampp Document Root: C: xampphtdocs httpd. conf: C: xamppapacheconfhttpd. conf php. ini: C: xamppphp. ini extension dir: C: xamppphpext MIB dir: C: usrmibs (copy C: xamppphpextrasmibs to C: usrmibs)

C: xamppphp. ini n ; extension=php_snmp. dll n Find php_snmp. dll in C: xamppphpext

C: xamppphp. ini n ; extension=php_snmp. dll n Find php_snmp. dll in C: xamppphpext n php_snmp. dll http: //www. im. ncnu. edu. tw/ycchen/nm/php_snmp. zip

httpd. conf Configuration n Configure a virtual directory for your SNMP application. n Suppose

httpd. conf Configuration n Configure a virtual directory for your SNMP application. n Suppose http: //127. 0. 0. 1/nmapp 1/mywalk. php n Virtual directory: nmapp 1 n Physical directory: C: snmpapp 1 <If. Module alias_module>. . . Alias /nmapp 1 "C: /snmp/app 1". . . </If. Module> <Directory "C: /snmp/app 1"> Allow. Override None Options None Require all granted </Directory>

Run XAMPP

Run XAMPP

walk. html <form method="post" post action="webwalk. php"> webwalk. php <table width=400 border=3 cellspacing=2 cellpadding=2

walk. html <form method="post" post action="webwalk. php"> webwalk. php <table width=400 border=3 cellspacing=2 cellpadding=2 bg. Color="#ffffcc" align=center> <tr><td align=right>Host: </td> <input type=text name="host"></td></tr> <tr><td align=right>Community Name: </td> <input type=text name="comm"></td></tr> <tr><td align=right>OID: </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>

http: //127. 0. 0. 1/nmapp 1/walk. html

http: //127. 0. 0. 1/nmapp 1/walk. html

webwalk. php <? php snmp_set_quick_print(true); $host = $_POST['host']; $comm = $_POST['comm']; $oid = $_POST['oid'];

webwalk. php <? 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 "<h 3 align=center>SNMPWALK of $host</h 3>"; echo "<table align=center cellspacing=2 border=2>"; foreach ($arr as $oid => $val) echo "<tr><td bg. Color=#ffffcc>$oid</td><td bg. Color=#ccffff>$val</td></tr>"; ? > </table>