snmp api函数演示代码
By Kingron
特别感谢: jinjazz@近身剪
原来不知道有JEDI,所以自己傻傻的翻译了这个SNMP.PAS文件,其实也好,学习API积累了经验,现在的人没这个劲头了。叹!
此次删除了原来的翻译的定义单元,改用网络上的翻译的SNMP.pas
/// 以下均为Jinjazz的Blog内容:
windows sdk文档中对于snmp函数的manager部分描述如下:
Manager Functions
The manager functions define the interface between ISV-developed manager applications and the management function dynamic-link library MGMTAPI.DLL.
SnmpMgrClose
SnmpMgrGetTrap
SnmpMgrOidToStr
SnmpMgrOpen
SnmpMgrRequest
SnmpMgrStrToOid
SnmpMgrTrapListen
最终的目标是调用上边的api函数,达到snmp要求的基本功能:get,getnext,set和trap。写了一个控件来实现上边函数功能,但是存在问题。有时候会跳出cup窗口,应该是函数参数中引用的数据结构还有问题,希望大大们帮忙SeeSee。
把上面的单元install一下,jtworks面板下面会出现TJtSnmp控件。我习惯把自己工作中做出来用的控件和一些frame都扔到这个面板下。
下面贴一些测试代码。因为cpu窗口有时候会跳出来,用了不得已的办法,在formcreate的时候调用了一个PatchInt3过程。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, JtSnmp;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Memo1: TMemo;
Memo2: TMemo;
JtSnmp1: TJtSnmp;
ButtonGet: TButton;
ButtonSet: TButton;
Memo3: TMemo;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
memo4: TMemo;
ButtonTrap: TButton;
procedure FormCreate(Sender: TObject);
procedure ButtonGetClick(Sender: TObject);
procedure ButtonSetClick(Sender: TObject);
procedure Memo2Change(Sender: TObject);
procedure ButtonTrapClick(Sender: TObject);
procedure JtSnmp1Trap(GenericTrap, SpecificTrap: Integer; Host,
MibInfo: String);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure PatchInt3;
var
NOP : Byte;
NTDLL: THandle;
BytesWritten: DWORD;
Address: Pointer;
begin
if Win32Platform <> VER_PLATFORM_WIN32_NT then Exit;
NTDLL := GetModuleHandle('NTDLL.DLL');
if NTDLL = 0 then Exit;
Address := GetProcAddress(NTDLL, 'DbgBreakPoint');
if Address = nil then Exit;
try
if Char(Address^) <> #$CC then Exit;
NOP := $90;
if WriteProcessMemory(GetCurrentProcess, Address, @NOP, 1, BytesWritten) and
(BytesWritten = 1) then
FlushInstructionCache(GetCurrentProcess, Address, 1);
except
// Do not panic if you see an EAccessViolation here, it is perfectly harmless!
on EAccessViolation do ;
else raise;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
PatchInt3;
JtSnmp1.Agent:=Edit1.Text;
JtSnmp1.Community:=Edit2.Text;
JtSnmp1.Connected:=true;
end;
procedure TForm1.ButtonGetClick(Sender: TObject);
begin
memo2.Lines.Clear;
JtSnmp1.MgrGet(memo1.Lines,memo2.Lines);
end;
procedure TForm1.ButtonSetClick(Sender: TObject);
var err,i:integer;
VarLst:Tstringlist;
begin
JtSnmp1.Agent:=Edit1.Text;
JtSnmp1.Community:=Edit2.Text;
JtSnmp1.Connected:=true;
VarLst:=Tstringlist.Create;
for i:=0 to memo3.Lines.Count-1 do
Varlst.AddObject(memo3.Lines[i],TObject(SnmpAsnOctetString));
JtSnmp1.MgrSet(memo1.Lines,Varlst,err);
end;
procedure TForm1.Memo2Change(Sender: TObject);
var i:integer;
begin
memo3.Clear;
for i:=0 to memo2.Lines.Count-1 do
memo3.Lines.Add(memo2.Lines.ValueFromIndex[i])
end;
procedure TForm1.ButtonTrapClick(Sender: TObject);
begin
JtSnmp1.TrapListened:=true;
ButtonTrap.caption:='Listening...';
end;
procedure TForm1.JtSnmp1Trap(GenericTrap, SpecificTrap: Integer; Host,
MibInfo: String);
var s:string;
T:Tstrings;
begin
T:=TstringList.Create;
s:='From Ip Address:'+Host;
T.Text:=Mibinfo;
memo4.Lines.Add(s);
memo4.Lines.AddStrings(T);
memo4.Lines.Add('*****************************************');
memo4.Lines.Add('');
T.Free;
end;
end.
其中trap监听的测试可以用一个第三方的工具或代码触发,然后用本demo接收。
测试程序效果: