You are here:
ActiveXperts.com > ActiveSocket > How to Use ActiveSocket > SNMP Trap Sender > Powershell
Quicklinks
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:
ActiveSocket supports the following SNMP trap features:
ActiveSocket can be well integrated into VB Script environments.
This document describes how ActiveSocket can be integrated into VB Script projects.
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 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.
To send SNMP traps using Powershell, you need to declare and create the following ActiveSocket objects:
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
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.