⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 idsnmp.pas

📁 photo.163.com 相册下载器 多线程下载
💻 PAS
📖 第 1 页 / 共 3 页
字号:
 |   aOwner : TComponent                                                      |
 *----------------------------------------------------------------------------*)
procedure TIdSNMP.InitComponent;
begin
  inherited;
  Port := 161;
  fTrapPort := 162;
  fCommunity := 'public';    {Do not Localize}
  Query:=TSNMPInfo.Create (Self);
  Reply:=TSNMPInfo.Create (Self);
  Trap :=TSNMPInfo.Create (Self);
  Query.Clear;
  Reply.Clear;
  Trap.Clear;
  FReceiveTimeout:=5000;
end;

(*----------------------------------------------------------------------------*
 |  destructor TIdSNMP.Destroy                                                |
 |                                                                            |
 |  Destructor for TIdSNMP component                                          |
 *----------------------------------------------------------------------------*)
destructor TIdSNMP.Destroy;
begin
  Reply.Free;
  Query.Free;
  Trap.Free;
  inherited destroy;
end;

(*----------------------------------------------------------------------------*
 | function TIdSNMP.SendQuery                                                 |
 |                                                                            |
 | Send an SNMP query and receive a reply.                                    |
 |                                                                            |
 | nb.  Before calling this, ensure that the following members are set:       |
 |                                                                            |
 |        Community         The SNMP community being queried - eg. 'public'   |    {Do not Localize}
 |        Host              The IP address being queried.  127.0.0.1 for the  |
 |                          local machine.                                    |
 |                                                                            |
 |      The call Query.Clear, then set:                                       |
 |                                                                            |
 |        Query.PDUType     PDUGetRequest to get a single set of MIBOID       |
 |                          value(s) or PDUGetNextRequest to start walking    |
 |                          the MIB                                           |
 |                                                                            |
 |      Next call Query.Clear, call MIBAdd to add the MIBOID(s) you require.  |
 |                                                                            |
 | The function returns True if a response was received.  IF a response was   |
 | received, it will be decoded into Reply.Value                              |
 *----------------------------------------------------------------------------*)
function TIdSNMP.SendQuery:boolean;
begin
  reply.clear;
  Query.Buffer:=Query.Encodebuf;
  Send(Query.host, Query.port, Query.buffer);
  try
    reply.Buffer := ReceiveString(Query.host, Query.port, FReceiveTimeout);
  except
    on e : EIdSocketError do
      if e.LastError <> 10054 then
        raise
      else
        reply.buffer := '';    {Do not Localize}
  end;

  if reply.Buffer<>'' then reply.DecodeBuf(reply.Buffer);    {Do not Localize}
  Result := (reply.Buffer <> '') and (reply.ErrorStatus = 0)    {Do not Localize}
end;

(*----------------------------------------------------------------------------*
 | TIdSNMP.QuickSend                                                          |
 |                                                                            |
 | Query a single MIBOID value.                                               |
 |                                                                            |
 | Parameters:                                                                |
 |   Mib : string               The MIBOID to query                           |
 |   Community : string         The SNMP comunity                             |
 |   Host : string              The SNMP host                                 |
 |   var value : string         String representation of the returned value.  |
 |                                                                            |
 | The function returns true if a value was returned for the MIB OID          |
 *----------------------------------------------------------------------------*)
function TIdSNMP.QuickSend (const Mib, Community,Host:string; var Value:string):Boolean;
begin
  self.Community := Community;
  self.Host := Host;
  Query.Clear;
  Query.PDUType:=PDUGetRequest;

  Query.MIBAdd(MIB,'');    {Do not Localize}
  Result:=SendQuery;
  if Result then Value:=Reply.MIBGet(MIB);
end;

(*----------------------------------------------------------------------------*
 | TIdSNMP.SendTrap                                                           |
 |                                                                            |
 | Send an SNMP trap.                                                         |
 |                                                                            |
 | The function returns 1                                                     |
 *----------------------------------------------------------------------------*)
function TIdSNMP.SendTrap: integer;
begin
  Trap.PDUType := PDUTrap;
  Trap.EncodeTrap;
  Send(Trap.Host, Trap.Port, Trap.Buffer);
  Result := 1;
end;

function TIdSNMP.ReceiveTrap: integer;
begin
  Trap.PDUType := PDUTrap;
  Result := 0;
  Trap.Buffer := ReceiveString(trap.host, trap.port, FReceiveTimeout);
  if Trap.Buffer <> '' then begin    {Do not Localize}
     Trap.DecodeTrap;
     Result := 1;
  end;
end;

function TIdSNMP.QuickSendTrap(const Dest, Enterprise, Community: string;
  Port, Generic, Specific: integer; MIBName, MIBValue: TIdStringList): integer;
var
  i: integer;
begin
    Trap.Host := Dest;
    Trap.Enterprise := Enterprise;
    Trap.GenTrap := Generic;
    Trap.SpecTrap := Specific;
    for i:=0 to (MIBName.Count - 1) do
      Trap.MIBAdd(MIBName[i], MIBValue[i], Integer (MibValue.Objects [i]));
    Result := SendTrap;
end;

function TIdSNMP.QuickReceiveTrap(var Source, Enterprise, Community: string;
  var Port, Generic, Specific, Seconds: integer; var MIBName, MIBValue: TIdStringList): integer;
var
  i: integer;
begin
    Result := ReceiveTrap;
    if (Result <> 0) then
    begin
      Source := Trap.Host;
      Port := Trap.Port;
      Enterprise := Trap.Enterprise;
      Community := Trap.Community;
      Generic := Trap.GenTrap;
      Specific := Trap.SpecTrap;
      Seconds := Trap.TimeTicks;
      MIBName.Clear;
      MIBValue.Clear;
      for i:=0 to (Trap.MIBOID.Count - 1) do
        begin
          MIBName.Add(Trap.MIBOID[i]);
          MIBValue.Add(Trap.MIBValue[i]);
        end;
    end;
end;

(*----------------------------------------------------------------------------*
 | TSNMPInfo.GetValue                                                         |
 |                                                                            |
 | Return string representation of value 'idx'                                |    {Do not Localize}
 |                                                                            |
 | Parameters:                                                                |
 |   idx : Integer              The value to get                              |
 |                                                                            |
 | The function returns the string representation of the value.               |
 *----------------------------------------------------------------------------*)
function TSNMPInfo.GetValue (idx : Integer) : string;
begin
  Result := MIBValue [idx]
end;

(*----------------------------------------------------------------------------*
 | TSNMPInfo.GetValueCount                                                    |
 |                                                                            |
 | Get the number of values.                                                  |
 |                                                                            |
 | The function returns the number of values.                                 |
 *----------------------------------------------------------------------------*)
function TSNMPInfo.GetValueCount: Integer;
begin
  Result := MIBValue.Count
end;

(*----------------------------------------------------------------------------*
 | TSNMPInfo.GetValueType                                                     |
 |                                                                            |
 | Return the 'type' of value 'idx'                                           |    {Do not Localize}
 |                                                                            |
 | Parameters:                                                                |
 |   idx : Integer              The value type to get                         |
 |                                                                            |
 | The function returns the value type.                                       |
 *----------------------------------------------------------------------------*)
function TSNMPInfo.GetValueType (idx : Integer): Integer;
begin
  Result := Integer (MIBValue.Objects [idx]);
  if Result = 0 then
    Result := ASN1_OCTSTR
end;

(*----------------------------------------------------------------------------*
 | TSNMPInfo.GetValueOID                                                      |
 |                                                                            |
 | Get the MIB OID for value 'idx'                                            |    {Do not Localize}
 |                                                                            |
 | Parameters:                                                                |
 |   idx: Integer               The MIB OID to gey                            |
 |                                                                            |
 | The function returns the specified MIB OID                                 |
 *----------------------------------------------------------------------------*)
function TSNMPInfo.GetValueOID(idx: Integer): string;
begin
  Result := MIBOID [idx];
end;

(*----------------------------------------------------------------------------*
 | TIdSNMP.SetCommunity                                                       |
 |                                                                            |
 | Setter for the Community property.                                         |
 |                                                                            |
 | Parameters:                                                                |
 |   const Value: string        The new community value                       |
 *----------------------------------------------------------------------------*)
procedure TIdSNMP.SetCommunity(const Value: string);
begin
  if fCommunity <> Value then
  begin
    fCommunity := Value;
    Query.Community := Value;
    Reply.Community := Value;
    Trap.Community := Value
  end
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -