How to use SNMP Trap Receiver in Powershell
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.
Network Component is compliant with SNMP v1 and SNMP v2c. Several 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.
Network Component SNMP traps features:
- SNMP v1 and SNMP v2c support;
- Support for aplphanumeric OID's (object identifier) and numeric OID's;
- Multi-threading architecture: send 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;
- Send multiple variables in a single SNMP TRAP message.
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 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 Network Component.
Step 3: Create the Network Component object in Powershell
Create a new Powershell file called DEMO.PS1.
Create the Network Component object(s) like this:
$objSnmpObject = new-object -comobject AxNetwork.SnmpObject
Now, add the following lines to the file to have your first Network Component Powershell program:
Write-Host "Network Component Version " $objSnmpObject.Version Write-Host " Build " $objSnmpObject.Build Write-Host " Module " $objSnmpObject.Module Write-Host "License Status: " $objSnmpObject.LicenseStatus
Appendix: Full source code
# ******************************************************************* # ActiveXperts Network Component Sample - SNMP Walk # Written by ActiveXperts Software - https://www.activexperts.com # ******************************************************************** # ******************************************************************** # Function PrintSnmpTrapData # ******************************************************************** Function PrintSnmpTrap ($objSnmpTrap) { Write-Host " " $res = "Trap from : " + $objSnmpTrap.Host Write-Host $res Write-Host " " Write-Host "Variables :" Write-Host " " $objSnmpObject = $objSnmpTrap.GetFirstObject() While($objSnmpTrap.LastError -eq 0) { $result = GetTypeString $objSnmpObject.Type Write-Host "OID : " $objSnmpObject.OID Write-Host "Value : " $objSnmpObject.Value Write-Host "Type : " $result Write-Host "Request ID : " $objSnmpObject.RequestID Write-Host " " $objSnmpObject = $objSnmpTrap.GetNextObject() } } # ******************************************************************** # Function GetTypeString() # ******************************************************************** Function GetTypeString ($lType) { $objConstants = new-object -comobject AxNetwork.NwConstants switch($lType) { $objConstants.nwSNMP_TYPE_INTEGER32{"nwSNMP_TYPE_INTEGER32"} $objConstants.nwSNMP_TYPE_BITS{"nwSNMP_TYPE_BITS"} $objConstants.nwSNMP_TYPE_OCTETSTRING{"nwSNMP_TYPE_OCTETSTRING"} $objConstants.nwSNMP_TYPE_NULL{"nwSNMP_TYPE_NULL"} $objConstants.nwSNMP_TYPE_OBJECTIDENTIFIER{"nwSNMP_TYPE_OBJECTIDENTIFIER"} $objConstants.nwSNMP_TYPE_SEQUENCE{"nwSNMP_TYPE_SEQUENCE"} $objConstants.nwSNMP_TYPE_IPADDRESS{"nwSNMP_TYPE_IPADDRESS"} $objConstants.nwSNMP_TYPE_COUNTER32 {"nwSNMP_TYPE_COUNTER32"} $objConstants.nwSNMP_TYPE_GAUGE32{"nwSNMP_TYPE_GAUGE32"} $objConstants.nwSNMP_TYPE_TIMETICKS{"nwSNMP_TYPE_TIMETICKS"} $objConstants.nwSNMP_TYPE_OPAQUE{"nwSNMP_TYPE_OPAQUE"} $objConstants.nwSNMP_TYPE_COUNTER64{"nwSNMP_TYPE_COUNTER64"} $objConstants.nwSNMP_TYPE_UNSIGNED32{"nwSNMP_TYPE_UNSIGNED32"} default{"UNKNOWN"} } } # *************************************************************************** # Function ReadInput # *************************************************************************** Function ReadInput($strPrompt, $strDefaultValue, $bAllowEmpty) { $strReturn = "" If ($strDefaultValue -ne "") { $strPrompt += " (leave empty for " + $strDefaultValue + "): " } Do { Write-Host $strPrompt $strReturn = read-host If ($strReturn -eq "" -and $strDefaultValue -ne "") { $strReturn = $strDefaultValue Write-Host $strReturn } elseif ($strReturn -eq "" -and $bAllowEmpty -eq $True) { break } } While ($strReturn -eq "") Write-Host "" return $strReturn } # *************************************************************************** # MAIN SCRIPT # *************************************************************************** cls # Create a SnmpTrapOut instance $objSnmpTrapManager = new-object -comobject AxNetwork.SnmpTrapManager $objConstants = new-object -comobject AxNetwork.NwConstants # Display ActiveXperts Network Component Version Write-Host "ActiveXperts Network Component " $objSnmpTrapManager.Version "`nBuild: " $objSnmpTrapManager.Build "`nModule: " $objSnmpTrapManager.Module "`nLicense Status: " $objSnmpTrapManager.LicenseStatus "`nLicense Key: " $objSnmpTrapManager.LicenseKey "`n`n"; # Logfile $objSnmpTrapManager.Logfile = $env:temp + "\objSnmpTrapManagerRecv.log" Write-Host "Log file used: " $objSnmpTrapManager.Logfile "`n" # Get Host, community name and optionally a MIB file $strCommunity = ReadInput "Enter community" "public" $True $strMibFile = ReadInput "Enter location of MIB file (optional, only required when using alpha-numeric OID's)" "" $True # Initialize SNMP $objSnmpTrapManager.Initialize() $res = "Initialize, result: " + $objSnmpTrapManager.LastError + " (" + $objSnmpTrapManager.GetErrorDescription( $objSnmpTrapManager.LastError ) + ")" Write-Host $res If($objSnmpTrapManager.LastError -ne 0 ) { exit } if($strMibFile -ne "") { $objSnmpTrapManager.LoadMibFile($strMibFile) } # Start listening for incoming SNMP traps $objSnmpTrapManager.StartListening($strCommunity) $res = "StartListening, result: " + $objSnmpTrapManager.LastError + " (" + $objSnmpTrapManager.GetErrorDescription( $objSnmpTrapManager.LastError ) + ")" Write-Host $res # Connection established; receive incoming traps Write-Host "Waiting for incoming traps ..." While($true) { $objSnmpTrap = $objSnmpTrapManager.GetFirstTrap() While ($objSnmpTrapManager.LastError -eq 0) { PrintSnmpTrap ($objSnmpTrap) trap [Exception] { $objSnmpTrap = $objSnmpTrapManager.GetNextTrap() } break } start-Sleep -m 1000 if($objSnmpTrapManager.LastError -eq 0) { $strEnter = ReadInput "type y to stop" "" $true if($strEnter -eq "y") { break } } } # Stop listening $objSnmpTrapManager.StopListening() $res = "StopListening, result: " + $objSnmpTrapManager.LastError + " (" + $objSnmpTrapManager.GetErrorDescription( $objSnmpTrapManager.LastError ) + ")" Write-Host $res # Shutdown SNMP $objSnmpTrapManager.Shutdown() $res = "Shutdown, result: " + $objSnmpTrapManager.LastError + " (" + $objSnmpTrapManager.GetErrorDescription( $objSnmpTrapManager.LastError ) + ")" Write-Host $res # Finished Write-Host "Finished."
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
You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.