ActiveSocket Toolkit Add network capabilities to any Windows or .NET application

Quicklinks


PHP SNMP Sample Source Code

ActiveSocket provides an easy-to-use development interface to a variety of IP protocols. By using ActiveSocket, you can very easily create or enhance applications with network features.

ActiveSocket features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.

SNMP can be well integrated into PHP environments. This document describes how ActiveSocket's SNMP objects can be integrated into PHP code. ActiveSocket is compliant with SNMP v1 and SNMP v2c. ActiveSocket automatically detects which SNMP version is running on the remote agent. Different SNMP data types, including:

  • String types (also called "octet strings");
  • Integer types (16bit, 32bit, 64bit and unsigned integers);
  • IP Address types;
  • Timetick types;
  • Counter types (32bit and 64bit counters);
  • OID types (also called "Object ID's");
  • Other, less frequently used datatypes.

The following operations are supported:

  • Get - retrieve an object variable from the (remote) agent;
  • GetNext - retrieve the next object variable from a table or list within an agent;
  • Set - set values for object variables within an agent.

Prerequisites

IMPORTANT: Make sure that the SNMP Service is installed and running on the machine where ActiveSocket is installed. For more details, please read FAQ items Q1200010 and Q1200015.

Step 1: Download and install the ActiveSocket Toolkit

Download ActiveSocket from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a html form

First of all we need to collect information from the user. To connect properly to another computer/device, we need to know the hostname, the SNMP protocol it is using and the community of the computer/device. We also need to know what information must be sent to the computer/device. To collect this information in a webinterface, you need to create a form.

In the form we're using in this sample, we're working with 7 fields.

  • The first field is reserved for the hostname (name : host).
  • The second one is reserved for the community (name : community).
  • The third one is reserved for the protocolversion (name : version).
  • The fourth, fifth, sixth and seventh are reserved for the commands (names : box1, box2, box3 and box4).

Our form louks like the image below

PHP

(Click on the picture to enlarge)

To verify if the form is submitted, we use the property "action" in the <form> tag. If the form is submitted, we navigate to index.php?method=submit. In php we fetch the variable "method" using $_GET['method'].

Step 3: Execute the commands

First of all we're going to fetch some variables. Using a loop we're executing all the filled in commands. We're executing the command using the function "execute_command()". We're doing that using this code:

//fetch the hostname
$host = $_POST['host'];
$community = $_POST['community'];
$version = $_POST['version'];
            
//walk through the 4 inputboxes, you can add more boxes if you like
for($i = 1; $i < 5; $i++){

    //again fetch some variables
    $boxname = "box" . $i;
    $boxvalue = $_POST[$boxname];
							 
    //execute the command, if there is a command
    if($boxvalue != ""){
       execute_command($boxvalue, $host, $community, $version);
    }
    
//close the loop
}

The function looks like this:

function execute_command($command, $host, $community, $version){
    
  //create the objects
  $objSocket = new COM ("ActiveXperts.SnmpManager");
        
  //initialise SNMP
  $objSocket->Initialize;
            		
  //set version
  $objSocket->ProtocolVersion = $version;
            		
  //open the object
  $objSocket->open($host,$community);
		
  //echo the results:
  echo "<b>Result " . $command . ":</b><br>";		
  if($objSocket->LastError == 0){
    //execute the command
    $objGet = $objSocket->Get($command);
    echo $objGet->Value . "<br>";
    echo "<br>";
  }
  else{
    //say out an error if the connection failed
    echo "Sorry.... Connection failed!<br><br>";		
  }

  //echo the error code
  echo "<b>Errorcode</b><br>";
  echo $objSocket->LastError . ":" . $objSocket->GetErrorDescription($objSocket->LastError) . "<br>";
  echo "<br>";
  echo "__________________________________________________";
  echo "<br>";
  echo "<br>";
		
  //close the object
  $objSocket->close;

//closing the function		
}

There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/network-component.