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:
ActiveSocket SNMP traps features:
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.
Download ActiveSocket from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
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.
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
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.