Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » SMTP Trap Sender » PHP

PHP SNMP Trap Sender Sample Source Code

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

Network Component 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.

Network Component can be well integrated into any development platform that supports ActiveX objects.


SNMP MIB Browsing can be well integrated into ASP .NET environments. This document describes how Network Component's SNMP MIB Browser can be integrated into ASP .NET projects.

A management information base (MIB) is a database used to manage the devices in a communications network. The database is hierarchical (tree-structured) and entries are addressed through object identifiers (OID's). A MIB should contain information on these commands and on the target objects (controllable entities or potential sources of status information) with a view to tuning the network transport to the current needs. Each type of object in a MIB database has a name, a syntax, and an encoding. The name is represented uniquely as an OID. An OID is an administratively assigned name. The administrative policies used for assigning names are discussed later in this memo.

Use Network Component's 'SnmpMibBrowser' object to load a MIB database into memory and iterate over all objects and view all properties.


Step 1: Download and install Network Component

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

Step 2: Create a form

To connect to an ftp server using PHP we need to collect the login information from the user, then connect to the server and then view the content

First of all we need to collect some information from the user. Like the username, the password and of course the server we are going to connect to. Perhaps you'd like to change the directory or/and create a logfile at logon. You can make the form look like this.

(Click on the picture to enlarge)

Step 3: Create the Network Component object in PHP

To create the Network Component object in php use the following code:

<? //create the Network Component SNMP Trap Sender object
$objSnmpTrapManager = new COM (AxNetwork.SnmpTrapManager);
?>

Appendix: Full source code

<html>
  <head>
    <title>ActiveXperts Network component PHP Sample</title>
			
    <style>
        body{
          font-family: verdana;
          font-size: xx-acsmall;
          color: navy;
          text-align: center;
        }
        td{
          font-family: verdana;
          font-size: x-small;
          color: navy;
        }	 
        .dotted{
          font-family: verdana;
          font-size: x-small;
          color: navy;
          background-color: white;
          border: 0px solid navy;
        }
        .solid{
          font-family: verdana;
          font-size: x-small;
          color: navy;
          background-color: white;
          border: 1px solid navy;
        }
        a{
          font-family: verdana;
          font-size: xx-small;
          color: red;
			  }
    </style>
		
  </head>
	
	<body>

    <h2>ActiveXperts Network component<br>Sample with PHP</h2>

    <table>
      <tr>
         <td>
           <?php
    
           //******************************************************************************
           //   script for displaying the form
           //******************************************************************************

           //only show the form, if it isn't allready submitted
           if($_GET['method'] == ""){	
							
             //explain what this sample does
             echo "With this sample you are able to send an SNMP-trap to a machine. Please fill in the form and hit the 'Send trap!' button.";
		  					
             //creating the form
             echo '<form method=post action=' . $_SERVER['PHP_SELF'] . '?method=submit>';

               //textfield for the computername
               echo '<b>Please fill in the computer/device you want to send a trap to:</b><br>';
               echo '<input type=text name=host class=solid value="localhost" style="width: 400px;"><br>';
               echo '<br>';
    	
               //textfield for the community
               echo '<b>Please fill in the community:</b><br>';
               echo '<input type=text name=community value="public" class=solid style="width: 400px;"><br>';
               echo '<br>';	

               //textfield for the community
               echo '<b>Please fill in the SNMP version:</b><br>';
               echo '<select class=solid style="width: 400px;" name="version">';
               echo '   <option value="2">SNMP V2C (default)</option>';
               echo '   <option value="1">SNMP V1</option>';			
               echo '</select>';
               echo '<br>';		
               echo '<br>';
	
               //textfields for commands
               echo "<b>Please enter the OID:</b><br>";
               echo '<input name=oid value="system.sysName.0" class=solid style="width: 400px;" class=notdotted type=text><br>';
               echo "<br>";
							
               //textfield for the community
               echo '<b>OID data type:</b><br>';
               echo '<select class=solid style="width: 400px;" name="type">';
               echo '   <option value="string">String</option>';							
               echo '   <option value="number">Number</option>';
               echo '   <option value="ipaddress">IP Address</option>';										
               echo '</select>';
               echo '<br>';		
               echo '<br>';							
							
               //textfields for commands
               echo "<b>Please enter the value:</b><br>";
               echo '<input name=value value="value" class=solid style="width: 400px;" class=notdotted type=text><br>';
               echo "<br>";

               //textfields for logfile
               echo "<b>Logfile (optional):</b><br>";
               echo '<input name=logfile value="" class=solid style="width: 400px;" class=notdotted type=text><br>';
               echo "<br>";							
    
               //create the submitbutton
               echo '<center><input class=solid type=submit value="Send trap!" style="width: 150px;"></center>';									

             //closing the form
             echo '</form>';
	
           //closing the if statement
           }

           $host = $_POST['host'];
           $community = $_POST['community'];
           $version = $_POST['version'];
   
    

           //******************************************************************************
           // function for executing the command
           //******************************************************************************

           function execute_command($host, $community, $version, $type, $message, $oid, $logfile){
    
             //create the objects
             $objSnmpTrap        = new com("AxNetwork.SnmpTrap");
             $objSnmpObject      = new com("AxNetwork.SnmpObject");
             $objConstants       = new com("AxNetwork.NwConstants");
             $objSnmpTrapManager = new com("AxNetwork.SnmpTrapManager");
  				
             //set logfile
             $objSnmpTrapManager->Logfile = "C:\Snmplogfile.txt";
				
             //initialise SNMP
             $objSnmpTrapManager->Initialize;
		
             //set version
             $objSnmpTrapManager->ProtocolVersion = $version;
		
             //set variable data
             $objSnmpObject->Clear();
             $objSnmpObject->OID = "$oid";
								
             //fetch the type and fill in the right type
             switch ($type){

               //if someone fills in "ipaddress":
               case "ipaddress":
               $objSnmpObject->Type = $objConstants->nwSNMP_TYPE_IPADDRESS;
               break;
											 
               //if someone fills in "number": 
               case "number":
               $objSnmpObject->Type = $objConstants->nwSNMP_TYPE_INTEGER;
               break;
											 
               //if someone fills in "string":
               case "string":
               $objSnmpObject->Type = $objConstants->nwSNMP_TYPE_OCTETSTRING;
               break;
								
             //close the switch
             }
								
             $objSnmpObject->Value = "$message";
								
             //create the trap
             $objSnmpTrap->Host = $host;
             $objSnmpTrap->Community = $community;
             $objSnmpTrap->AddObject ( $objSnmpObject ); 
            
								
             //send it
             $objSnmpTrapManager->Send( $objSnmpTrap );
								
             //and get the error message
             echo "<b>Errorcode:</b> ";
             echo $objSnmpTrapManager->LastError; 
             echo " : "; 
             echo $objSnmpTrapManager->GetErrorDescription($objSnmpTrapManager->LastError); 
		
		
           }

           //check if the form is submitted
           if($_GET['method'] != ""){
             //execute the command					
             execute_command($_POST['host'], $_POST['community'], $_POST['version'], $_POST['type'], $_POST["value"], $_POST['oid'], $_POST['logfile']);
           }

         ?>

        </td>
      </tr>
    </table>
		This sample uses <a href="https://www.activexperts.com">ActiveXperts Network component</a>
  </body>
</html>

You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.