Quicklinks
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: 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.
SNMP can be well integrated into Powershell environments.
This document describes how ActiveSocket's SNMP objects can be integrated into Powershell 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:
The following operations are supported:
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.
Create a new Powershell file called DEMO.PS1.
Create the ActiveSocket object(s) like this:
$objSnmpManager = new-object -comobject ActiveXperts.SnmpManager
Now, add the following lines to the file to have your fist ActiveSocket Powershell program:
Write-Host "ActiveSocket Version " $objSnmpManager.Version "; Build " $objSnmpManager.Build "; Module " $objSnmpManager.Module Write-Host "Expiration date: " $objSnmpManager.ExpirationDate
You can now query SNMP OID's.
The following Powershell code shows how to execute an SNMP query on a remote SNMP agent:
################################################################################# # ActiveSocket - Powershell script # © Copyright ActiveXperts Software B.V. # # For more information about ActiveSocket, please # visit the online ActiveSocket page at: # http://www.activexperts.com ################################################################################# cls ################################################################################# # Functions --------------------------------------------------------------------# ################################################################################# ################################################################################# # ReadInput --------------------------------------------------------------------# function ReadInput($strTitle, $strDefault, $bAllowEmpty) { $strReturn = "" do { $strInput = Read-host $strTitle, "(e.g.:" $strDefault, ")" if($strInput -ne "") { $strReturn = $strInput } if($bAllowEmpty -eq 1) { break } } while($strReturn -eq "") return $strReturn } ################################################################################# # PrintSnmpObject --------------------------------------------------------------# function PrintSnmpObject($objSnmpObject) { Write-Host " OID : " $objSnmpObject.OID Write-Host " Value : " $objSnmpObject.Value Write-Host " Type : " $GetTypeString($objSnmpObject.Type) Write-Host } ################################################################################# # GetTypeString ----------------------------------------------------------------# function GetTypeString($lType) { switch ($lType) { $objConstants.asSNMP_TYPE_INTEGER32 { GetTypeString = "asSNMP_TYPE_INTEGER32"} $objConstants.asSNMP_TYPE_BITS { GetTypeString = "asSNMP_TYPE_BITS" } $objConstants.asSNMP_TYPE_OCTETSTRING { GetTypeString = "asSNMP_TYPE_OCTETSTRING" } $objConstants.asSNMP_TYPE_NULL { GetTypeString = "asSNMP_TYPE_NULL" } $objConstants.asSNMP_TYPE_OBJECTIDENTIFIER { GetTypeString = "asSNMP_TYPE_OBJECTIDENTIFIER" } $objConstants.asSNMP_TYPE_SEQUENCE { GetTypeString = "asSNMP_TYPE_SEQUENCE" } $objConstants.asSNMP_TYPE_IPADDRESS { GetTypeString = "asSNMP_TYPE_IPADDRESS" } $objConstants.asSNMP_TYPE_COUNTER32 { GetTypeString = "asSNMP_TYPE_COUNTER32" } $objConstants.asSNMP_TYPE_GAUGE32 { GetTypeString = "asSNMP_TYPE_GAUGE32" } $objConstants.asSNMP_TYPE_TIMETICKS { GetTypeString = "asSNMP_TYPE_TIMETICKS" } $objConstants.asSNMP_TYPE_OPAQUE { GetTypeString = "asSNMP_TYPE_OPAQUE" } $objConstants.asSNMP_TYPE_COUNTER64 { GetTypeString = "asSNMP_TYPE_COUNTER64" } $objConstants.asSNMP_TYPE_UNSIGNED32 { GetTypeString = "asSNMP_TYPE_UNSIGNED32" } else { GetTypeString= "UNKNOWN" } } } ################################################################################# # THE SCRIPT ITSELF ------------------------------------------------------------# ################################################################################# # Create SnmpManager and ASConstants instances $objSnmpManager = new-object -comobject ActiveXperts.SnmpManager $objConstants = new-object -comobject ActiveXperts.ASConstants # Write version information and expiration date Write-Host "ActiveSocket Version " $objSnmpManager.Version "; Build " $objSnmpManager.Build "; Module " $objSnmpManager.Module Write-Host "Expiration date: " $objSnmpManager.ExpirationDate # Get Host, community name and optionally a MIB file $strHostName = ReadInput "Enter the hostname (a remote or local hostname)" "localhost" 0 $strCommunity = ReadInput "Enter community" "public" 0 $strMibFile = ReadInput "Enter location of MIB file (optional, only required when using alpha-numeric OID's)" "" 1 $objSnmpManager.LogFile = "c:\temp\snmp.log" # Set SNMP protocol version (V1 or V2C) $objSnmpManager.ProtocolVersion = $objConstants.asSNMP_VERSION_V2C # Initialize SNMP $objSnmpManager.Initialize() Write-Host "Initialize: " $objSnmpManager.LastError " (" $objSnmpManager.GetErrorDescription($objSnmpManager.LastError) ")" Write-Host if($objSnmpManager.LastError -ne 0) { exit } if($strMibFile -ne "") { $objSnmpManager.LoadMibFile($strMibFile) } # 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) Write-Host "Open( " $strHostName ", " $strCommunity " ): " $objSnmpManager.LastError " (" $objSnmpManager.GetErrorDescription($objSnmpManager.LastError) ")" Write-Host "" if($objSnmpManager.LastError -eq 0) { $strOID = "system.sysDescr.0" $objSnmpObject = $objSnmpManager.Get($strOID) Write-Host "Get( " $strOID ", " $strCommunity " ): " $objSnmpManager.LastError " (" $objSnmpManager.GetErrorDescription($objSnmpManager.LastError) ")" if($objSnmpManager.LastError -eq 0) { PrintSnmpObject($objSnmpObject) } $strOID = "system.sysName.0" $objSnmpObject = $objSnmpManager.Get($strOID) Write-Host "Get( " $strOID ", " $strCommunity " ): " $objSnmpManager.LastError " (" $objSnmpManager.GetErrorDescription($objSnmpManager.LastError) ")" if($objSnmpManager.LastError -eq 0) { PrintSnmpObject($objSnmpObject) } $strOID = "system.sysUpTime.0" $objSnmpObject = $objSnmpManager.Get($strOID) Write-Host "Get( " $strOID ", " $strCommunity " ): " $objSnmpManager.LastError " (" $objSnmpManager.GetErrorDescription($objSnmpManager.LastError) ")" if($objSnmpManager.LastError -eq 0) { PrintSnmpObject($objSnmpObject) } $objSnmpManager.Close() Write-Host "Close(): " $objSnmpManager.LastError " (" $objSnmpManager.GetErrorDescription($objSnmpManager.LastError) ")" Write-Host "" } # Shutdown SNMP session $objSnmpManager.Shutdown() Write-Host "Shutdown(): " $objSnmpManager.LastError " (" $objSnmpManager.GetErrorDescription($objSnmpManager.LastError) ")" Write-Host Write-Host "Ready."
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.