Download ActiveSocket Network Communications Toolkit 4.1  (5094 KB - .exe file)
Download Manual  (505 KB - .htm file)
VBScript 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: 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.
SNMP can be well integrated into VBScript environments.
This document describes how ActiveSocket's SNMP objects can be integrated into VBScript 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 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 objSnmpManager
Create the ActiveSocket object(s) like this:
Set objSnmpManager = CreateObject( "ActiveXperts.SnmpManager" )
Now, add the following lines to the file to have your fist ActiveSocket VBScript program:
WScript.Echo "Version: " & objSocket.Version
WScript.Echo "Expiration Date: " & objSocket.Expiration Date
Step 4: Execute an SNMP query on a remote SNMP agent
You can now query SNMP OID's.
The following VBScript code shows how to execute an SNMP query on a remote SNMP agent:
Option Explicit
Dim objSnmpManager
Dim objSnmpData
Dim objConstants
Dim strHostName, strCommunity, strValue, strOID
' Create SnmpManager and ASConstants instances
Set objSnmpManager = CreateObject ( "ActiveXperts.SnmpManager" )
Set objConstants = CreateObject ( "ActiveXperts.ASConstants" )
' Write version information and expiration date
WScript.Echo "ActiveSocket " & objSnmpManager.Version & " demo."
WScript.Echo "Expiration date: " & objSnmpManager.ExpirationDate & vbCrLf
' Get Host and communicaty name
Do
strHostName = inputbox( "Enter the hostname (a remote or local hostname)", "Input", "localhost" )
Loop until strHostName <> ""
Do
strCommunity = inputbox( "Enter community", "Input", "public" )
Loop until strCommunity <> ""
' Initialize SNMP
objSnmpManager.Initialize
WScript.Echo "Initialize: " & objSnmpManager.LastError & " (" & objSnmpManager.GetErrorDescription( objSnmpManager.LastError ) & ")"
WScript.Echo
If( objSnmpManager.LastError <> 0 ) Then
WScript.Quit
End If
' Open SNMP session. Pass hostname and community.
' Note: Port 161 is used. To specify a different port, pass the port number as 3rd parameter (optional)
objSnmpManager.Open strHostName, strCommunity
WScript.Echo "Open( " & strHostName & ", " & Chr(34) & strCommunity & Chr(34) & " ): " & objSnmpManager.LastError & " (" & objSnmpManager.GetErrorDescription( objSnmpManager.LastError ) & ")"
WScript.Echo
If( objSnmpManager.LastError = 0 ) Then
strOID = "system.sysDescr.0"
Set objSnmpData = objSnmpManager.Get( strOID )
WScript.Echo "Get( " & strOID & ", " & Chr(34) & strCommunity & Chr(34) & " ): " & objSnmpManager.LastError & " (" & objSnmpManager.GetErrorDescription( objSnmpManager.LastError ) & ")" & vbCrLf
If( objSnmpManager.LastError = 0 ) Then
PrintSnmpData( objSnmpData )
End If
strOID = "system.sysName.0"
Set objSnmpData = objSnmpManager.Get( strOID )
WScript.Echo "Get( " & strOID & ", " & Chr(34) & strCommunity & Chr(34) & " ): " & objSnmpManager.LastError & " (" & objSnmpManager.GetErrorDescription( objSnmpManager.LastError ) & ")" & vbCrLf
If( objSnmpManager.LastError = 0 ) Then
PrintSnmpData( objSnmpData )
End If
strOID = "system.sysUpTime.0"
Set objSnmpData = objSnmpManager.Get( strOID )
WScript.Echo "Get( " & strOID & ", " & Chr(34) & strCommunity & Chr(34) & " ): " & objSnmpManager.LastError & " (" & objSnmpManager.GetErrorDescription( objSnmpManager.LastError ) & ")" & vbCrLf
If( objSnmpManager.LastError = 0 ) Then
PrintSnmpData( objSnmpData )
End If
objSnmpManager.Close()
WScript.Echo "Close(): " & objSnmpManager.LastError & " (" & objSnmpManager.GetErrorDescription( objSnmpManager.LastError ) & ")"
WScript.Echo
End If
' Shutdown SNMP session
objSnmpManager.Shutdown
WScript.Echo "Shutdown(): " & objSnmpManager.LastError & " (" & objSnmpManager.GetErrorDescription( objSnmpManager.LastError ) & ")"
WScript.Echo
WScript.Echo "Ready."
' ********************************************************************
' Function PrintSnmpData
' ********************************************************************
Function PrintSnmpData( objSnmpData )
WScript.Echo " OID : " & objSnmpData.OID
WScript.Echo " Value : " & objSnmpData.Value
WScript.Echo " Type : " & GetTypeString( objSnmpData.Type )
WScript.Echo
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.
|