Delphi SNMP MIB Sample Source Code
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 MIB Browsing can be well integrated into ASP .NET environments. This document describes how Network Component's SNMP MIB Browser can be integrated into ASP .NET projects.
A management information base (MIB) is a database used to manage the devices in a communications network. The database is hierarchical (tree-structured) and entries are addressed through object identifiers (OID's). A MIB should contain information on these commands and on the target objects (controllable entities or potential sources of status information) with a view to tuning the network transport to the current needs. Each type of object in a MIB database has a name, a syntax, and an encoding. The name is represented uniquely as an OID. An OID is an administratively assigned name. The administrative policies used for assigning names are discussed later in this memo.
Use Network Component's 'SnmpMibBrowser' object to load a MIB database into memory and iterate over all objects and view all properties.
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 Delphi Project
Launch Borland Delphi from the Start menu. Choose 'New' from the 'File' menu and select your preferred kind of application, for instance: 'VCL Forms Application - Delphi for Win32'. A new Form is displayed in the workspace.
Step 3: Refer to the Network Component Library and create the objects
Now that a new project has been created, you must add a reference to Network Component in the project to be able to use the Network Component object. To do so, choose 'Import Component...' from the 'Component' menu. The Import Components' dialog appears. Select 'Import a Type Library':
In the 'Registered Type Libraries' page, select 'Network Component 4.4 Type Library' and click 'Next':
In the 'Components' page, leave all fields default and click 'Next':
In the 'Install' page, select 'Create Unit' and click 'Next':
The interface code is generated now and is shown in the AxNetwork_TLB tab of the project.
Step 4: Declare and create the object
From the Project Manager, open Unit1.bas and add the AxNetwork_TLB to the 'Uses' statement to refer to the Network Component library:
In the 'private' or 'public' section, declare the following objects:
Appendix: Full source code
{******************************************************************************} { } { ActiveXperts } { } { Network Component: Delphi_Console_SNMPmibBrowser } { } { } { } { } { } { } {******************************************************************************} program SNMPmibBrowserDemo; {$APPTYPE CONSOLE} uses SysUtils, ActiveX, AxNetwork_TLB in '..\..\Imports\AxNetwork_TLB.pas'; var vt:OleVariant; {==============================================================================} {= R e a d I n p u t ========================================================} {==============================================================================} function ReadInput(strTitle:string;strDefault:string):String; var strInput:string; begin strInput := ''; WriteLn(strTitle); Write(' > '); ReadLn(strInput); if( strInput = '' )then strInput :=strDefault else strInput:=strTitle; ReadInput:=strInput; end; {==============================================================================} {= S h o w M i b t r e e ==================================================} {==============================================================================} procedure ShowMIBtree(objSnmpMIB:SnmpMibBrowser; objConstants: NwConstants); var objSNMP:SnmpObject; strDescription, strAccess, strStatus:string; nDescription:integer; begin // Start with "iso" vt:=objSnmpMIB.Get('iso'); objSNMP := IDispatch ( vt ) as ISnmpObject; objSnmpMIB.Get('iso'); while( objSnmpMIB.LastError = 0 )do begin objSNMP := IDispatch ( vt ) as ISnmpObject; strDescription := StringReplaceEx(objSNMP.Description, #13+#10, ' [nl] ', [rfReplaceAll, rfIgnoreCase]); strDescription:= StringReplaceEx(objSNMP.Description, #10, ' [nl] ', [rfReplaceAll, rfIgnoreCase]); nDescription := Length(strDescription); if(nDescription > 40)then begin strDescription := copy(strDescription,0,40); strDescription := strDescription+'...'; end; if( objSnmp.Access = objConstants.nwMIB_ACCESS_NOACCESS ) then strAccess := 'NOACCESS' else if( objSnmp.Access = objConstants.nwMIB_ACCESS_NOTIFY ) then strAccess := 'NOTIFY' else if( objSnmp.Access = objConstants.nwMIB_ACCESS_READONLY ) then strAccess := 'READONLY' else if( objSnmp.Access = objConstants.nwMIB_ACCESS_WRITEONLY ) then strAccess := 'WRITEONLY"' else if( objSnmp.Access = objConstants.nwMIB_ACCESS_READWRITE ) then strAccess := 'READWRITE' else if( objSnmp.Access = objConstants.nwMIB_ACCESS_READCREATE )then strAccess := 'READCREATE' else strAccess := 'n/a'; if( objSnmp.Status = objConstants.nwMIB_STATUS_CURRENT ) then strStatus := 'CURRENT' else if( objSnmp.Status = objConstants.nwMIB_STATUS_DEPRECATED )then strStatus := 'DEPRECATED' else if( objSnmp.Status = objConstants.nwMIB_STATUS_OBSOLETE ) then strStatus := 'OBSOLETE' else if( objSnmp.Status = objConstants.nwMIB_STATUS_MANDATORY ) then strStatus := 'MANDATORY' else strStatus := 'n/a'; WriteLn(' OID: ' + objSnmp.OID); WriteLn(' Description: ' + strDescription ); WriteLn(' Syntax: ' + objSnmp.Syntax); WriteLn(' Access: ' + strAccess ); WriteLn(' Status: ' + strStatus ); vt:=objSnmpMIB.GetNext; end; end; {==============================================================================} {==============================================================================} {= T H E P R O G R A M I T S E L F ========================================} {==============================================================================} {==============================================================================} var objSNMPmib:SnmpMibBrowser; objConstants:NwConstants; strMibFile:string; bVerbose:boolean; begin CoInitialize(nil); strMibFile:= ''; bVerbose :=false; objSNMPmib := COSnmpMibBrowser.Create(); objConstants := CONwConstants.Create(); // A license key is required to unlock this component after the trial period // has expired. Assign the LicenseKey property every time a new instance of // this component is created (see code below). Alternatively, the LicenseKey // property can be set automatically. This requires the license key to be // stored in the registry. For details, see manual, chapter // "Product Activation". { objSnmpTrapManager.LicenseKey = 'XXXXX-XXXXX-XXXXX'; } Writeln('ActiveXperts Network Component ' + objSNMPmib.Version + sLineBreak + 'Build: ' + objSNMPmib.Build + sLineBreak + 'Module: ' + objSNMPmib.Module + sLineBreak + 'License Status: ' + objSNMPmib.LicenseStatus + sLineBreak + 'License Key: ' + objSNMPmib.LicenseKey + sLineBreak); strMibFile := ReadInput('Enter MIB file (leave blank for a Windows System MIB file)', 'c:\windows\system32\mib_ii.mib' ); WriteLn('MIB File: ' + strMibFile ); objSnmpMIB.LoadMibFile( strMibFile ); WriteLn('LoadMibFile, result: ' + IntToStr(objSnmpMIB.LastError) + ' (' + objSnmpMIB.GetErrorDescription(objSnmpMIB.LastError) + ')' ); if( objSnmpMIB.LastError = 0 )then begin ShowMIBTree(objSnmpMIB, objConstants ); end; WriteLn('Ready.'); sleep(3000); writeln('Done. Pressto close this window');readln; end.
You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.