Development of SNMP management terminal based on PHP

Source: Internet
Author: User
Tags snmp snmpget snmpset snmpwalk

A System environment:

Operating system: CentOS 5.4 Kernel: linux_2.6

Compilation environment: GCC 4.1.2 Code version: php-5.2.8.tar.gz

two. Install PHP

1. first of all, PHP support SNMP, as follows configuration method (red part is about SNMP , other parameters depending on the situation)

#./configure--prefix=/usr/local/php--with-apxs2=/usr/local/apache2/bin/apxs--enable-trace-vars-with-mysql=/var /lib/mysql--enable-soap--with-xmlrpc--enable-mbstring --enable-sockets--with-snmp

#make

#make Install

Three function Introduction

after the installation is complete, PHP The following function can be called in

  3.1 Snmpget gets the specified class identification code.

Syntax: Snmpget (string hostname, string community, String object_id);

return value: OID corresponding type (personal feel can be understood as a string)

Function Type: Network system

Description: This function can be used to obtain the specified class identification code (OID, Object Identifier). The parameter hostname is the specified machine. The parameter community is a community name, public or private, and so on. The parameter object_id is the OID class, which is the class defined by the MIB. The return value is the OID value and returns False if it fails.

Use examples, below are some examples

<?php

Snmpget ("127.0.0.1", "public", "1.3.6.1.2.1.1.4.0");

?>

3.2snmpset set the corresponding writable OID the value

Syntax: bool Snmpset (string hostname, string community, String object_id, String type, mixed value, int timeout, int retrie s)

Return value: Logical value (0/1)

Function Type: Network system

Description: This function can be used to set the specified class identification code (OID, Object Identifier). The parameter hostname is the specified machine. The parameter community is a writable community name, with public or private, and so on. The type is the OID corresponding to the data type, and the parameter object_id is the OID class, which is the class defined by the MIB. The set success return value is Ture, and false if it fails.

Examples of Use:

<?php

Snmpset ("127.0.0.1", "223323", " 1.3.6.1.2.1.1.4.0", "string", " [email protected]", 3,1);

?>

  3.3 Snmpwalk get all classes.

Syntax: Array snmpwalk (string hostname, string community, string object_id, int [timeout], int [retries]);

return value: Array

Function Type: Network system

Description: This function can be used to obtain all class identification code (OID, Object Identifier), to obtain the class is the root class (root) start of all MIB classes. The parameter hostname is the specified machine. The parameter community is a community name, public or private, and so on. The parameter object_id is the OID class, which is the class defined by the MIB, as long as an empty string is entered. The return value is an OID array and returns False if it fails.

Use examples below for some examples

<?php

$a = Snmpwalk ("127.0.0.1", "public", " 1.3.6.1.2.1.1");

for ($i =0; $i <count ($a); $i + +) {

echo $a [$i]. "<br>n";

}

?>

  3.4 snmpwalkoid Get the Network ontology tree information.

Syntax: Array snmpwalkoid (string hostname, string community, string object_id, int [timeout], int [retries]);

return value: Array

Function Type: Network system

Description: This function can be used to obtain all class identification code (OID, Object Identifier), to obtain the class is the root class (root) start of all MIB classes and their values. The parameter hostname is the specified machine. The parameter community is a community name, public or private, and so on. The parameter object_id is the OID class, which is the class defined by the MIB, as long as an empty string is entered. The return value is an array and returns False if it fails.

Use examples, below are some examples

<?php

$a = snmpwalkoid ("127.0.0.1", "public", " " ");

for (reset ($a); $i = key ($a); next ($a)) {

echo $i.": ". $a [$i]. "<br>n";

}

?>

  3.5 Snmp_get_quick_print Get SNMP in the function library Quick_print values.

Syntax: Boolean snmp_get_quick_print (void);

Return Value: Boolean value

Function Type: Network system

Description: This function can be used to obtain the Quick_print value in the UCD SNMP function library, which cannot function on the Windows NT system. This function does not require input parameters, returns 0 for Quick_print off, and 1 for open.

  3.6 Snmp_set_quick_print Configuration SNMP in the function library Quick_print values.

Syntax: void Snmp_set_quick_print (Boolean quick_print);

return value: None

Function Type: Network system

Description: This function configures the Quick_print value in the UCD SNMP function library and does not function on the Windows NT system. A parameter Quick_print value of 0 means that Quick_print is off, and 1 means open. There is no return value for this function.

Usage examples

<?php

Snmp_set_quick_print (0);

$a = Snmpget ("127.0.0.1", "public", ". 1.3.6.1.2.1.2.2.1.9.1" );

echo $a."n";

Snmp_set_quick_print (1);

$a = Snmpget ("127.0.0.1", "public", ". 1.3.6.1.2.1.2.2.1.9.1" );

echo $a."n";

?>

The return value is

Timeticks: (0) 0:00:00.00

0:00:00.00

three. Calling SNMP functions

1 The sample code for the background call is as follows: (Save as snmp.php)// accessories below, including sample files

<?php

Switch ($_request[' Submit ')

{

Case "GET":

$ret = Snmpget ($_request[' GetIP '), $_request[' getcom '], $_request[' getoid '],5,1);

echo $ret; break;

Case "Walk":

$ret =snmpwalk ($_request[' GetIP '), $_request[' getcom '], $_request[' getoid '],5,1);

for ($i =0; $i <count ($ret); $i + +)

echo $ret [$i]. " <br> ";

Break

Case "Set":

if (Snmpset ($_request[' SetIP '), $_request[' setcom '], $_request[' setoid '],$_request[' type '],$_request[' SetValue '], 5,1))

echo "<br>set <". $_request[' Setoid ']. " = ". $_request[' SetValue ')." > ok! ";

Else

echo "<br>set <". $_request[' Setoid ']. " > Fail,maybe No this OID or it is just read-only! ";

Break

Default

echo "Without this operation:". $_request[' Submit '];

}

?>

Development of SNMP management terminal based on PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.