ActiveSocket Toolkit Add network capabilities to any Windows or .NET application

Quicklinks


Sending SNMP traps using Powershell

ActiveSocket provides an easy-to-use development interface (SDK) to a variety of IP protocols. By using ActiveSocket, you can very easily create or enhance applications with network features.

ActiveSocket 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.

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

  • ASN_INTEGER; Numeric values;
  • ASN_INTEGER32; Numeric values;
  • ASN_BITS; Bit values, for instance: one bit per port to see if a port is connected;
  • ASN_OCTETSTRING; Byte string;
  • ASN_NULL; Null value;
  • ASN_OBJECTIDENTIFIER; Object identifier (OID);
  • ASN_SEQUENCE; Text string;
  • ASN_IPADDRESS; IP address;
  • ASN_COUNTER32; 32 bit counter;
  • ASN_GAUGE32; 32 bit gauge;
  • ASN_TIMETICKS; Timestamps or uptime;
  • ASN_OPAQUE; Byte string;
  • ASN_COUNTER64; 64 bit counter;
  • ASN_UNSIGNED32; unsigned 32 bit numeric value.

ActiveSocket supports the following SNMP trap features:

  • SNMP v1 and SNMP v2c support;
  • Support for alphanumeric OID's (object identifier) and numeric OID's;
  • Multithreading architecture: send and/or receive 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 such as coldstart, warmstart, linkup, linkdown, authfailure and neighbourloss;
  • Send multiple data objects (variable bindings) using a single SNMP trap message.

ActiveSocket can be well integrated into VB Script environments.

This document describes how ActiveSocket can be integrated into VB Script projects.

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 Powershell 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 objects in Powershell

To send SNMP traps using Powershell, you need to declare and create the following ActiveSocket objects:

  • SnmpTrapManager; The SNMP manager which allows you to send and/or receive SNMP traps in the form of SnmpTrap objects;
  • SnmpTrap; The actual SNMP trap, this object holds some SNMP trap properties like: destination host, port and community, specific and generic trap and a timestamp;
  • SnmpObject; This object is used to hold the data to be send with the SNMP trap. These data objects are also called variable bindings.

Create a new Powershell file called DEMO.PS1.

Create the ActiveSocket object(s) like this:

$objSnmpTrapManager  = new-object -comobject ActiveXperts.SnmpTrapManager
$objSnmpTrap	     = new-object -comobject ActiveXperts.SnmpTrap
$objSnmpObject   	 = new-object -comobject ActiveXperts.SnmpObject
$objConstants        = new-object -comobject ActiveXperts.ASConstants

Now, add the following lines to the file to have your fist ActiveSocket Powershell program:

Write-Host "ActiveSocket Version " $objSnmpTrapManager.Version "; Build " $objSnmpTrapManager.Build "; Module " $objSnmpTrapManager.Module
Write-Host "Expiration date: " $objSnmpTrapManager.ExpirationDate

Step 4: Send an SNMP trap message to a remote SNMP management station

You can now send SNMP trap messages to any SNMP management station or device. The following Powershell code shows how to send an SNMP trap to a remote SNMP manager:

#################################################################################
# ActiveSocket - Powershell script
# © Copyright ActiveXperts Software B.V.
#
# For more information about ActiveSocket, please
# visit the online ActiveSocket page at:
# http://www.activexperts.com
#################################################################################
# ActiveSocket sample - Send SNMP Traps
#################################################################################

cls

#################################################################################
# Functions --------------------------------------------------------------------#
#################################################################################


#################################################################################
# ReadInput --------------------------------------------------------------------#

function ReadInput($strTitle, $strDefault, $bAllowEmpty)
{
	$strReturn = ""
	do
		{
		     $strInput = Read-host $strTitle, "(", $strDefault , ")"
		     if($strInput -ne "") 
				{
		          $strReturn = $strInput
				}
			
			if($bAllowEmpty -eq 1)
				{
					break
				}
		}		
	while($strReturn -eq "")

	return $strReturn
}


#################################################################################
# THE SCRIPT ITSELF ------------------------------------------------------------#
#################################################################################
 
	
# Create a SnmpTrapOut instance
$objSnmpTrapManager  = new-object -comobject ActiveXperts.SnmpTrapManager
$objSnmpTrap	     = new-object -comobject ActiveXperts.SnmpTrap
$objSnmpObject   	 = new-object -comobject ActiveXperts.SnmpObject
$objConstants        = new-object -comobject ActiveXperts.ASConstants

# Write some information to console
Write-Host "ActiveSocket Version " $objSnmpTrapManager.Version "; Build " $objSnmpTrapManager.Build "; Module " $objSnmpTrapManager.Module
Write-Host "Expiration date: " $objSnmpTrapManager.ExpirationDate

# Get Host, community name and optionally a MIB file
$strHostName    = ReadInput "Enter the hostname (a remote or local hostname)" "localhost"
$strCommunity   = ReadInput "Enter community" "public"
$strMibFile     = ReadInput "Enter location of MIB file (optional, only required when using alpha-numeric OID's)" ""

# Initialize SNMP
$objSnmpTrapManager.Initialize()
Write-Host "Initialize, result: " $objSnmpTrapManager.LastError " (" $objSnmpTrapManager.GetErrorDescription( $objSnmpTrapManager.LastError ) ")"
if( $objSnmpTrapManager.LastError -ne 0 )
	{
		exit
	}
	
# Set SNMP protocol version. if not assigned, then default: asSNMP_VERSION_V2C will be used.
$objSnmpTrapManager.ProtocolVersion = $objConstants.asSNMP_VERSION_V2C

if( $strMibFile -ne "" )
	{
		$objSnmpTrapManager.LoadMibFile( $strMibFile )
	}	

# Set trap properties
$objSnmpTrap.Clear()
$objSnmpTrap.Host	   = $strHostName
$objSnmpTrap.Community = $strCommunity
$objSnmpTrap.Port	   = 162

# Add first variable to trap
$objSnmpObject.Clear()
$objSnmpObject.OID   = ".1.3.6.1.2.1.1.5.0"
$objSnmpObject.Type  = $objConstants.asSNMP_TYPE_IPADDRESS
$objSnmpObject.Value = "10.0.0.1"

$objSnmpTrap.AddObject( $objSnmpObject )

# Add second variable to trap
$objSnmpObject.Clear()
$objSnmpObject.OID   = ".1.3.6.1.2.1.1.5.1"
$objSnmpObject.Type  = $objConstants.asSNMP_TYPE_OCTETSTRING
$objSnmpObject.Value = "One two three"

$objSnmpTrap.AddObject( $objSnmpObject )

# Send the trap. If you want to use another port than default SNMP trap port 162, set it as optional 2nd parameter
$objSnmpTrapManager.Send($objSnmpTrap)

Write-Host "Send, result: " $objSnmpTrapManager.LastError " (" $objSnmpTrapManager.GetErrorDescription( $objSnmpTrapManager.LastError ) ")"

$objSnmpTrapManager.Shutdown()
Write-Host "Shutdown, result: " $objSnmpTrapManager.LastError " (" $objSnmpTrapManager.GetErrorDescription( $objSnmpTrapManager.LastError ) ")"

To run the code, start Powershell and browse to the location of the file you just created. Enter .\Demo.ps1 to run the code. Notice that if the script is not working, you have to change the execution policy; you can do that with the following command:

Set-ExecutionPolicy -unrestricted

There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/network-component.