snmptraps.cpp

来自「HP公司的SNMP++的Win32版本源码」· C++ 代码 · 共 103 行

CPP
103
字号
/*
  snmpTrap.cpp 

  version 2.8

  Copyright (c) 1996
  Hewlett-Packard Company

  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
  Permission to use, copy, modify, distribute and/or sell this software
  and/or its documentation is hereby granted without fee. User agrees
  to display the above copyright notice and this license notice in all
  copies of the software and any documentation of the software. User
  agrees to assume all liability for the use of the software; Hewlett-Packard
  makes no representations about the suitability of this software for any
  purpose. It is provided "AS-IS" without warranty of any kind,either express
  or implied. User hereby grants a royalty-free license to any and all
  derivatives based upon this software code base.

  Peter E. Mellquist
*/
#include "snmp_pp.h"
#include <iostream.h>
#include <stdlib.h>
#define COLDSTART "1.3.6.1.6.3.1.1.5.1"
#define PAYLOADID "1.3.6.1.4.1.11.2.16.2"
#define PAYLOAD "SNMP++ Trap Send Test"
#define ENTERPRISE "1.3.6.1.2.1.1.1.2.0.1"
int main( int argc, char **argv)  {

   Oid ent(ENTERPRISE);               // default enterprise

   //---------[ check the arg count ]----------------------------------------
   if ( argc < 2) {
	  cout << "Usage:\n";
	  cout << "snmpTrap Address | DNSName [Id] [options]\n";
	  cout << "Id = default is 1.3.6.1.6.3.1.1.5.1 = ColdStart\n";
	  cout << "options: -v1 , use SNMPV1, default\n";
	  cout << "         -v2 , use SNMPV2\n";
	  cout << "         -cCommunity_name, specify community default is 'public' \n"; 
	  return 0;
   }

   //---------[ make a GenAddress and Oid object to retrieve ]---------------
   GenAddress address( argv[1]);      // make a SNMP++ Generic address
   if ( !address.valid()) {           // check validity of address
	  cout << "Invalid Address or DNS Name, " << argv[1] << "\n";
	  return 0;
   }
   Oid oid( COLDSTART);    // default is ColdStart 
   if ( argc >= 3) {                  // if 3 args, then use the callers Oid
	  if ( strstr( argv[2],"-")==0) {
	     oid = argv[2];
	     if ( !oid.valid()) {         // check validity of user oid
		    cout << "Invalid Oid, " << argv[2] << "\n";
		    return 0;
         }
      }
   }

   //---------[ determine options to use ]-----------------------------------
   snmp_version version=version1;                       // default is v1
   OctetStr community("public");                        // trap community name to use
   char *ptr; 
   for(int x=1;x<argc;x++) {                           // parse for version
      if ( strstr( argv[x],"-v2")!= 0)   
         version = version2c;
      if ( strstr( argv[x],"-c")!=0) {
         ptr = argv[x]; ptr++; ptr++;
	 	 community = ptr;
	   }
   }

   //----------[ create a SNMP++ session ]-----------------------------------
   int status; 
   Snmp snmp( status);                // check construction status
   if ( status != SNMP_CLASS_SUCCESS) {
      cout << "SNMP++ Session Create Fail, " << snmp.error_msg(status) << "\n";
      return 0;
   }

   //--------[ build up SNMP++ object needed ]-------------------------------
   Pdu pdu;                               // construct a Pdu object
   Vb vb;                                 // variable binding object to use
   vb.set_oid(PAYLOADID);                 // example oid for trap payload
   vb.set_value(PAYLOAD);                 // example string for payload
   pdu += vb;                             // append the vb to the pdu
   pdu.set_notify_id( oid);               // set the id of the trap
   pdu.set_notify_enterprise( ent);       // set up the enterprise of the trap
   CTarget target( address);              // make a target using the address
   target.set_version( version);          // set the SNMP version SNMPV1 or V2
   target.set_readcommunity( community);  // set the community name to use 
 
   //-------[ Send the trap  ]------------------------------------------------
   cout << "SNMP++ Trap to " << argv[1] << " SNMPV" << (version+1);
   cout << " Community=" << community.get_printable() << "\n"; 
   status = snmp.trap( pdu,target);
   cout << "SNMP++ Trap Send Status = " << snmp.error_msg( status) << "\n";

   return 0;
}  // end snmpTrap 

⌨️ 快捷键说明

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