How to use SNMP in VBScript
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 can be well integrated into ASP .NET (Visual Basic) environments.
This document describes how Network Component's SNMP objects can be integrated into ASP .NET (Visual Basic) projects.
Network Component is compliant with SNMP v1 and SNMP v2c. Different 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.
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.
Step 1: Download and install the 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 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 Network Component.
Step 3: Create the Network Component 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 Network Component object(s):
Dim objSnmp
Create the Network Component object(s) like this:
Set objSnmp = CreateObject( "AxNetwork.Snmp" )
Now, add the following lines to the file to have your first Network Component VBScript program:
WScript.Echo "Version: " & objSnmp.Version WScript.Echo "License Status: " & objSnmp.LicenseStatus
Appendix: Full source code
' ******************************************************************** ' ActiveXperts Network Component sample - SNMP Walk (Get/GetNext) ' ' Written by ActiveXperts Software ' https://www.activexperts.com ' ******************************************************************** Option Explicit Dim objSnmpManager, objSnmpObject, objConstants Dim strCommunity,strOID, strNextOID, fso Dim strMibFile, strHostName, strValue ' Create instances of the objects Set objSnmpManager = CreateObject( "AxNetwork.SnmpManager" ) Set objConstants = CreateObject( "AxNetwork.NwConstants" ) ' 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". ' ' objSnmpManager.Activate "XXXXX-XXXXX-XXXXX", False ' Component info Wscript.Echo "ActiveXpert Network Component " & objSnmpManager.Version Wscript.Echo "Build: " & objSnmpManager.Build & vbCrLf & "Module: " & objSnmpManager.Module Wscript.Echo "License Status: " & objSnmpManager.LicenseStatus & vbCrLf & "License Key: " & objSnmpManager.LicenseKey & vbCrLf ' Set Logfile Set fso = CreateObject( "Scripting.FileSystemObject" ) objSnmpManager.LogFile = fso.GetSpecialFolder(2) & "\SnmpManager.log" WScript.Echo "Log file: " & objSnmpManager.LogFile & vbCrLf ' Get Host, community name and optionally a MIB file strHostName = ReadInput( "Enter the hostname (a remote or local hostname)", "192.168.31.12", False ) strMibFile = ReadInput( "Enter location of MIB file (optional, only required when using alpha-numeric OID's)", "c:\windows\system32\hostmib.mib", True ) ' Set SNMP protocol version (V1, V2C or V3) objSnmpManager.ProtocolVersion = objConstants.nwSNMP_VERSION_V3 ' Set SNMPv3 authentication credentials (SHA1 or MD5) objSnmpManager.AuthProtocol = objConstants.nwSNMP_AUTH_SHA1 objSnmpManager.AuthUsername = "user" objSnmpManager.AuthPassword = "password1" 'Set SNMPv3 encryption credentials (DES or AES) objSnmpManager.PrivProtocol = objConstants.nwSNMP_PRIV_DES objSnmpManager.PrivPassword = "password2" ' Initialize SNMP objSnmpManager.Initialize WScript.Echo "Initialize, result: " & objSnmpManager.LastError & " (" & objSnmpManager.GetErrorDescription( objSnmpManager.LastError ) & ")" If( objSnmpManager.LastError <> 0 ) Then WScript.Quit End If If( strMibFile <> "" ) Then objSnmpManager.LoadMibFile( strMibFile ) End If ' Open SNMP session. Pass hostname and community. ' Note: Port 161 is used by default. 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 ) & ")" If( objSnmpManager.LastError = 0 ) Then strOID = "snmp.snmpInPkts.0" If( objSnmpManager.LastError = 0 ) Then Set objSnmpObject = objSnmpManager.Get( strOID ) While( objSnmpManager.LastError = 0 ) PrintSnmpObject( objSnmpObject ) On Error Resume Next Set objSnmpObject = objSnmpManager.GetNext() WEnd objSnmpManager.Close() WScript.Echo "Close, result: " & objSnmpManager.LastError & " (" & objSnmpManager.GetErrorDescription( objSnmpManager.LastError ) & ")" End If End If ' Shutdown SNMP objSnmpManager.Shutdown WScript.Echo "Shutdown(): " & objSnmpManager.LastError & " (" & objSnmpManager.GetErrorDescription( objSnmpManager.LastError ) & ")" WScript.Echo "Ready." ' *************************************************************************** ' Function ReadInput ' *************************************************************************** Function ReadInput( ByVal strTitle, ByVal strDefault, ByVal bAllowEmpty ) Dim strInput, strReturn Do strInput = inputbox( strTitle, "Enter value", strDefault ) If ( strInput <> "" ) Then strReturn = strInput End If Loop until strReturn <> "" Or bAllowEmpty ReadInput = strReturn End Function ' ******************************************************************** ' Function PrintSnmpData ' ******************************************************************** Function PrintSnmpObject( objSnmpObject ) WScript.Echo "OID : " & objSnmpObject.OID WScript.Echo " Name :" & objSnmpObject.OIDName WScript.Echo " Type :" & GetTypeString( objSnmpObject.Type ) WScript.Echo " Value :" & objSnmpObject.Value End Function ' ******************************************************************** ' Function GetTypeString() ' ******************************************************************** Function GetTypeString( lType ) Select Case lType 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= "UNKNOWN" 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.