Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » SNMP Trap Receiver » Classic ASP

SNMP Trap Receiver Sample Source Code in ASP

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.


Network Component is compliant with SNMP v1 and SNMP v2c. Several SNMP data types are supported, including:

Network Component SNMP traps features:


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 new Web Site

First, create a new directory on the IIS Server's file system. This directory will hold the ASP later on.

From the 'Start menu', click on 'Administrative Tools' and click on 'Internet Information Services (IIS) Manager'. Right-click on the 'Web Sites' container and choose 'New->Web Site':

IIS

(Click on the picture to enlarge)

The 'Web Site Creation Wizard' is shown, guiding you thorugh the process of creating a new web site. Provide all necessary information:

You're now able to write an ASP script to use IP protocols with Network Component.

Step 3: Create the from

First of all we need to create a form in HTML to get the login information for the ftp server. All we actually need is:

In this sample all we're going to do is create a ASP file that is able to list files and directories in a directory. Using Network Component it is easy to create a page that makes you able to upload and to download files.

You can create a form that looks like this:

(Click on the picture to enlarge)

Step 4:Create ActiveXperts Network Component objects in ASP

Create a new ASP script called DEFAULT.ASP in the directory that was created in Step2, using your favorite editor. On top of the ASP code, insert the following lines to declare and create the Dns object:

<object runat="server" progid="AxNetwork.SnmpTrapManager" id="objSftp"></object>
<object runat="server" progid="AxNetwork.ASConstants" id="objConstants"></object>

Appendix: Full source code

<object runat="server" progid="AxNetwork.SnmpTrapManager" id="objSnmpTrapManager"> </object>
<object runat="server" progid="AxNetwork.NwConstants" id="objConstants"> </object>

<%
' HTML-CSS layout includes below, no code there!
%>
<!-- #include file="css/Header.html" -->
<!-- #include file="css/Menu.html" -->

<%
  ' A license key is required to unlock this component after the trial period has expired.
  ' Call 'Activate' with a valid license key as its first parameter. Second parameter determines whether to save the license key permanently 
  ' to the registry (True, so you need to call Activate only once), or not to store the key permanently (False, so you need to call Activate
  ' every time the component is created). For details, see manual, chapter "Product Activation".
  '
  ' objSnmpTrapManager.Activate "XXXXX-XXXXX-XXXXX", False    
%>

<%
  Dim numLastError, strLastError

  Set fso = CreateObject("Scripting.FileSystemObject")
  strLogfile = fso.GetSpecialFolder(2) & "\SnmpTrapManager.log"
    
  strLastError = ""

  If( Request( "txtSendTrap" ) <> "" ) Then

    objSnmpTrapManager.LogFile = Request( "txtLogFile" )    ' Optional: set LogFile property for troubleshooting purposes

    ' Set Protocol (v1 or v2c)
    If( Request( "txtProtocol" ) = "SNMP v1" ) Then
       objSnmpTrapManager.ProtocolVersion = objConstants.nwSNMP_VERSION_V1
    Else
       objSnmpTrapManager.ProtocolVersion = objConstants.nwSNMP_VERSION_V2C
    End If

    objSnmpTrapManager.Initialize
    numLastError                     =  objSnmpTrapManager.LastError  
    If( numLastError = 0 ) Then
      Set objSnmpTrap                = CreateObject( "ActiveXperts.SnmpTrap" )
      Set objSnmpObject              = CreateObject( "ActiveXperts.SnmpObject" )

      objSnmpTrap.Clear()
      objSnmpTrap.Host               = Request( "txtAgent" )
      objSnmpTrap.Community          = Request( "txtCommunity" )
      objSnmpTrap.Port               = Request( "txtPort" )

      ' Add  variable to trap
      objSnmpObject.Clear()
      objSnmpObject.OID              = Request( "txtOID" )
      objSnmpObject.Type             = GetTypeCode( Request( "txtType" ) )
      objSnmpObject.Value            = Request( "txtValue" )

      objSnmpTrap.AddObject( objSnmpObject )
      numLastError                   =  objSnmpTrap.LastError  
    End If

    If( numLastError = 0 ) Then
      objSnmpTrapManager.Send objSnmpTrap
      numLastError                   =  objSnmpTrapManager.LastError  
    End If

    strLastError                     =  numLastError & ": " & objSnmpTrapManager.GetErrorDescription( numLastError )  

    objSnmpTrapManager.Shutdown
  End If
%>

<div>

  <h1>ActiveXperts Network Component ASP Sample - SNMP Trap Sender</h1>

  <form action="snmptrap.asp" method="post">
    <h2>Component:</h2>
    <h3>Module [<% = objSnmpTrapManager.Module %>]; Build [<% = objSnmpTrapManager.Build %>]</h3>
    
    <!-- Remote Agent -->
    <label for="Remote Agent">Remote Agent:</label>
    <p>
      <input type="text" name="txtAgent" value="<% If Request( "txtAgent" ) = "" Then%>localhost<% Else %><% = Request( "txtAgent" ) %><% End If %>">
      :<input style="width: 50px" type="text" name="txtPort" value="<% If Request( "txtPort" ) = "" Then%>162<% Else %><% = Request( "txtPort" ) %><% End If %>">
    </p>
    
    <!-- Community -->
    <label for="Community">Community:</label>
    <p>
      <input type="text" name="txtCommunity" value="<% If Request( "txtCommunity" ) = "" Then %>public<% Else %><% = Request( "txtCommunity" ) %><% End If %>">
    </p>
    
    <!-- Protocol -->
    <label for="Protocol">Protocol version:</label>    
    <p>
      <select name="txtProtocol">
        <% If( Request( "txtProtocol" ) = "SNMP v1" ) Then %>
       <option value="SNMP v1" selected>SNMP v1</option>
       <option value="SNMP v2c">SNMP v2c</option>
        <% Else %>
       <option value="SNMP v1">SNMP v1</option>
       <option value="SNMP v2c" selected>SNMP v2c</option>
        <% End If %>
      </select>
    </p>
    
    <!-- Send OID (Object ID) -->
    <label for="Protocol">Send OID (Object ID):</label>    
    <p>
      <input type="text" name="txtOID" value="<% If Request( "txtOID" ) = "" Then %>system.sysDescr.0<% Else %><% = Request( "txtOID" ) %><% End If %>">
    </p>
    
      <!-- Send Data Type -->
    <label for="Send Data Type">Send Data Type:</label>    
    <p>
      <select name="txtType">
        <% Dim arrType( 15 )
           Dim i
           arrType( 0 )  = "nwSNMP_TYPE_UNDEFINED"
           arrType( 1 )  = "nwSNMP_TYPE_INTEGER32"
           arrType( 2 )  = "nwSNMP_TYPE_BITS"
           arrType( 3 )  = "nwSNMP_TYPE_OCTETSTRING"
           arrType( 4 )  = "nwSNMP_TYPE_NULL"
           arrType( 5 )  = "nwSNMP_TYPE_OBJECTIDENTIFIER"
           arrType( 6 )  = "nwSNMP_TYPE_SEQUENCE"
           arrType( 7 )  = "nwSNMP_TYPE_IPADDRESS"
           arrType( 8 )  = "nwSNMP_TYPE_COUNTER32"
           arrType( 9 )  = "nwSNMP_TYPE_GAUGE32"
           arrType( 10 ) = "nwSNMP_TYPE_TIMETICKS"
           arrType( 11 ) = "nwSNMP_TYPE_OPAQUE"
           arrType( 12 ) = "nwSNMP_TYPE_COUNTER64"
           arrType( 13 ) = "nwSNMP_TYPE_UNSIGNED32"
           arrType( 14 ) = "nwSNMP_TYPE_COUNTER64"
        %>

        <% For i = 0 To UBound( arrType ) - 1 %> 
              <% If( Request( "txtType" ) = arrType( i ) Or ( Request( "txtType" ) = "" And i = 3 ) ) Then %>
                    <option value="<% = arrType( i ) %>" selected><% = arrType( i ) %></option>
              <% Else %>
                    <option value="<% = arrType( i ) %>"><% = arrType( i ) %></option>
              <% End If %>
        <% Next %>  
      </select>
    </p>
    
    <!-- Send Data Value: -->
    <label for="Send Data Value">Send Data Value:</label>    
    <p>
      <input type="text" name="txtValue" value="<% If Request( "txtValue" ) = "" Then %>A system description<% Else %><% = Request( "txtValue" ) %><% End If %>">
    </p>
    
    <!-- Send Trap Button -->
    <div></div>
    <p>
      <input type="submit" value="Send Trap" name="txtSendTrap">
    </p>
    
    <!-- Empty Row -->
    <div></div>
     
    <!-- Logfile -->
    <label for="Logfile">Logfile:</label>
    <p>
      <input type="text" id="LogFile" name="txtLogFile" value="<% = strLogfile %>" />
    </p> 
    
    <!-- Result -->
    <label for="Result">Result:</label>
    <p>
      <input type="text" id="Result" name="txtResult" style="font-weight: bold;" value="<% = strLastError %>" />
    </p>
  </form>
  <p>
    This sample is based on ActiveXperts Network Component, an <a href="https://www.activexperts.com/" target="_blank">ActiveXperts Software</a> product.<br />
    <a href="Default.asp">Back to main page</a>
  </p>
</div><!-- /container -->
<%
' HTML-CSS layout include below, no code there!
%>
<!-- #include file="css/Footer.html" -->

<% Function GetTypeString( numType )
    Select Case numType
     Case objConstants.nwSNMP_TYPE_INTEGER32: 
        GetTypeString = "nwSNMP_TYPE_INTEGER32"
     Case objConstants.nwSNMP_TYPE_BITS 
        GetTypeString = "nwSNMP_TYPE_BITS"
     Case objConstants.nwSNMP_TYPE_OCTETSTRING 
        GetTypeString = "nwSNMP_TYPE_OCTETSTRING"
     Case objConstants.nwSNMP_TYPE_NULL 
        GetTypeString = "nwSNMP_TYPE_NULL"
     Case objConstants.nwSNMP_TYPE_OBJECTIDENTIFIER
        GetTypeString = "nwSNMP_TYPE_OBJECTIDENTIFIER"
     Case objConstants.nwSNMP_TYPE_SEQUENCE 
        GetTypeString = "nwSNMP_TYPE_SEQUENCE"
     Case objConstants.nwSNMP_TYPE_IPADDRESS 
        GetTypeString = "nwSNMP_TYPE_IPADDRESS"
     Case objConstants.nwSNMP_TYPE_COUNTER32 
        GetTypeString = "nwSNMP_TYPE_COUNTER32"
     Case objConstants.nwSNMP_TYPE_GAUGE32
        GetTypeString = "nwSNMP_TYPE_GAUGE32"
     Case objConstants.nwSNMP_TYPE_TIMETICKS 
        GetTypeString = "nwSNMP_TYPE_TIMETICKS"
     Case objConstants.nwSNMP_TYPE_OPAQUE 
        GetTypeString = "nwSNMP_TYPE_OPAQUE"
     Case objConstants.nwSNMP_TYPE_COUNTER64
        GetTypeString = "nwSNMP_TYPE_COUNTER64"
     Case objConstants.nwSNMP_TYPE_UNSIGNED32 
        GetTypeString = "nwSNMP_TYPE_UNSIGNED32"
     Case Else 
        GetTypeString = "nwSNMP_TYPE_UNDEFINED" 
    End Select 
   End Function
%>

<% Function GetTypeCode( strType )
    Select Case strType
     Case "nwSNMP_TYPE_INTEGER32": 
        GetTypeCode = objConstants.nwSNMP_TYPE_INTEGER32
     Case "nwSNMP_TYPE_BITS" 
        GetTypeCode = objConstants.nwSNMP_TYPE_BITS
     Case "nwSNMP_TYPE_OCTETSTRING"
        GetTypeCode = objConstants.nwSNMP_TYPE_OCTETSTRING
     Case "nwSNMP_TYPE_NULL" 
        GetTypeCode = objConstants.nwSNMP_TYPE_NULL
     Case "nwSNMP_TYPE_OBJECTIDENTIFIER"
        GetTypeCode = objConstants.nwSNMP_TYPE_OBJECTIDENTIFIER
     Case "nwSNMP_TYPE_SEQUENCE" 
        GetTypeCode = objConstants.nwSNMP_TYPE_SEQUENCE
     Case "nwSNMP_TYPE_IPADDRESS"
        GetTypeCode = objConstants.nwSNMP_TYPE_IPADDRESS
     Case "nwSNMP_TYPE_COUNTER32"
        GetTypeCode = objConstants.nwSNMP_TYPE_COUNTER32
     Case "nwSNMP_TYPE_GAUGE32"
        GetTypeCode = objConstants.nwSNMP_TYPE_GAUGE32
     Case "nwSNMP_TYPE_TIMETICKS" 
        GetTypeCode = objConstants.nwSNMP_TYPE_TIMETICKS
     Case "nwSNMP_TYPE_OPAQUE" 
        GetTypeCode = objConstants.nwSNMP_TYPE_OPAQUE
     Case "nwSNMP_TYPE_COUNTER64"
        GetTypeCode = objConstants.nwSNMP_TYPE_COUNTER64
     Case "nwSNMP_TYPE_UNSIGNED32" 
        GetTypeCode = objConstants.nwSNMP_TYPE_UNSIGNED32
     Case Else 
        GetTypeCode = objConstants.nwSNMP_TYPE_UNDEFINED
    End Select 
   End Function
%>

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