Download ActiveSocket Network Communications Toolkit 4.1  (5094 KB - .exe file)
Download Manual  (505 KB - .htm file)
VBScript SNMP Receive Trap 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: ICMP, HTTP and HTTPs with support for proxy servers and secure web sites, Telnet, NTP time protocol, RSH remote shell script interface, SNMP (Simple Network Management Protcol), SNMP Traps, Sockets, WOL (Wake-On-LAN), and more.
ActiveSocket SNMP traps can be well integrated into VBScript environments.
This document describes how ActiveSocket SNMP traps can be integrated into VBScript code.
ActiveSocket is compliant with SNMP v1 and SNMP v2c.
Several SNMP data types are supported, 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.
ActiveSocket SNMP traps features:
- SNMP v1 and SNMP v2c support
;
- Support for aplphanumeric OID's (object identifier) and numeric OID's;
- Multi-threading architecture: send SNMP traps simultaneously from one process using multiple threads;
- Support for ports other than the default 161 and 162 ports;
- Support for enterprise specific traps;
- Support for SNMP v1 generic traps;
- Send multiple variables in a single SNMP TRAP message.
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 new script
Create a new script using your favorite editor.
You can simply use notepad. However, a VBScript editor is recommended, so you can browse through objects, objects properties and object functions.
You're now able to write a more advanced script to communicate using the ActiveSocket Toolkit.
Step 3: Create the ActiveSocket object in VBScript
Create a new VBScript file called DEMO.VBS. It is recommended to insert the following line on top of your code:
Option Explicit
This statement requires that all variable names be defined (with the Dim statement),
to avoid simple typos that can cause incredible headaches and long debugging sessions for something that should have never happened.
Now, declare the ActiveSocket object(s):
Dim objSnmpTrapManager
Dim objSnmpTrapData
Dim objSnmpTrapVariable
Dim objSnmpConstants
Create the ActiveSocket object(s) like this:
Set objSnmpTrapManager = CreateObject( "ActiveXperts.SnmpTrapManager" )
Set objSnmpTrapData = CreateObject( "ActiveXperts.SnmpTrapData" )
Set objSnmpTrapVariable = CreateObject( "ActiveXperts.SnmpTrapVariables" )
Set objSnmpTrapConstants = CreateObject( "ActiveXperts.ASConstants" )
Now, add the following lines to the file to have your fist ActiveSocket VBScript program:
WScript.Echo "Version: " & objSnmpTrapManager.Version
WScript.Echo "Expiration Date: " & objSnmpTrapManager.Expiration Date
Step 4: Receive SNMP trap messages from any SNMP agent
Receive SNMP trap messages from any SNMP agent.
The following VBScript code shows how to rceive SNMP trap messages:
Option Explicit
' Declare variables
Dim objSnmpTrapManager, objSnmpTrapData
Dim objConstants
Dim strCommunity
Dim i
' Create SnmpTrapManager and ASConstants instances
Set objSnmpTrapManager = CreateObject ( "ActiveXperts.SnmpTrapManager" )
Set objConstants = CreateObject ( "ActiveXperts.ASConstants" )
' Write version information and expiration date
WScript.Echo "ActiveSocket " & objSnmpTrapManager.Version & " demo."
WScript.Echo "Expiration date: " & objSnmpTrapManager.ExpirationDate & vbCrLf
' Get community information
Do
strCommunity = inputbox( "Enter community", "Input", "public" )
Loop until strCommunity <> ""
' Initialize SNMP
objSnmpTrapManager.Initialize
WScript.Echo "Initialize: " & objSnmpTrapManager.LastError & " (" & objSnmpTrapManager.GetErrorDescription( objSnmpTrapManager.LastError ) & ")"
If( objSnmpTrapManager.LastError <> 0 ) Then
WScript.Quit
End If
' Start listening for incoming SNMP traps
objSnmpTrapManager.StartListening strCommunity
WScript.Echo "StartListening, result: " & objSnmpTrapManager.LastError & " (" & objSnmpTrapManager.GetErrorDescription( objSnmpTrapManager.LastError ) & ")"
' Connection established; receive incoming traps
WScript.Echo "Waiting for incoming traps ..."
On Error Resume Next ' Useful because GetFirstTrap may return an empty (null) object
While ( True )
Set objSnmpTrapData = objSnmpTrapManager.GetFirstTrap
While ( objSnmpTrapManager.LastError = 0 )
PrintSnmpTrapData( objSnmpTrapData )
Set objSnmpTrapData = objSnmpTrapManager.GetNextTrap
Wend
WScript.Sleep 1000
Wend
On Error Goto 0 ' Undo previous On Error Resume Next
' Stop listening
objSnmpTrapManager.StopListening
WScript.Echo "StopListening, result: " & objSnmpTrapManager.LastError & " (" & objSnmpTrapManager.GetErrorDescription( objSnmpTrapManager.LastError ) & ")"
' Shutdown SNMP
objSnmpTrapManager.Shutdown
WScript.Echo "Shutdown, result: " & objSnmpTrapManager.LastError & " (" & objSnmpTrapManager.GetErrorDescription( objSnmpTrapManager.LastError ) & ")"
WScript.Echo "Ready."
' ********************************************************************
' Function PrintSnmpTrapData
' ********************************************************************
Function PrintSnmpTrapData( objSnmpTrapData )
Dim objSnmpTrapVariable
WScript.Echo "Trap from : " & objSnmpTrapData.Host
WScript.Echo " TimeStamp : " & objSnmpTrapData.Uptime
WScript.Echo
WScript.Echo " Variables:"
WScript.Echo
Set objSnmpTrapVariable = objSnmpTrapData.GetFirstVariable
While ( objSnmpTrapData.LastError = 0 )
WScript.Echo "OID : " & objSnmpTrapVariable.OID
WScript.Echo " Value:" & objSnmpTrapVariable.Value
WScript.Echo " Type:" & GetTypeString( objSnmpTrapVariable.Type )
Set objSnmpTrapVariable = objSnmpTrapData.GetNextVariable
WEnd
End Function
' ********************************************************************
' Function GetTypeString()
' ********************************************************************
Function GetTypeString( lType )
Select Case lType
Case objConstants.asSNMP_TYPE_INTEGER32:
GetTypeString = "asSNMP_TYPE_INTEGER32"
Case objConstants.asSNMP_TYPE_BITS
GetTypeString = "asSNMP_TYPE_BITS"
Case objConstants.asSNMP_TYPE_OCTETSTRING
GetTypeString = "asSNMP_TYPE_OCTETSTRING"
Case objConstants.asSNMP_TYPE_NULL
GetTypeString = "asSNMP_TYPE_NULL"
Case objConstants.asSNMP_TYPE_OBJECTIDENTIFIER
GetTypeString = "asSNMP_TYPE_OBJECTIDENTIFIER"
Case objConstants.asSNMP_TYPE_SEQUENCE
GetTypeString = "asSNMP_TYPE_SEQUENCE"
Case objConstants.asSNMP_TYPE_IPADDRESS
GetTypeString = "asSNMP_TYPE_IPADDRESS"
Case objConstants.asSNMP_TYPE_COUNTER32
GetTypeString = "asSNMP_TYPE_COUNTER32"
Case objConstants.asSNMP_TYPE_GAUGE32
GetTypeString = "asSNMP_TYPE_GAUGE32"
Case objConstants.asSNMP_TYPE_TIMETICKS
GetTypeString = "asSNMP_TYPE_TIMETICKS"
Case objConstants.asSNMP_TYPE_OPAQUE
GetTypeString = "asSNMP_TYPE_OPAQUE"
Case objConstants.asSNMP_TYPE_COUNTER64
GetTypeString = "asSNMP_TYPE_COUNTER64"
Case objConstants.asSNMP_TYPE_UNSIGNED32
GetTypeString = "asSNMP_TYPE_UNSIGNED32"
Case Else
GetTypeString= "UNKNOWN"
End Select
End 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/asocket.
The ActiveSocket tool is a Network Communications ActiveX software component (SDK).
This control supports SNMP, SMTP, POP3, Telnet, TCP, NTP, RSH, HTTP, HTTPs, FTP, DNS, ICMP and more, and can be used by any Windows development platform,
including Visual Basic .NET, Visual CSharp .NET,
ASP .NET (VB,CS),
ASP,
Visual Basic,
Visual Studio/Visual C++,
Delphi,
PHP,
ColdFusion,
HTML,
VBScript and any other ActiveX/COM compliant platform. The ActiveSocket Toolkit is an ActiveXperts Software B.V. Product.
|