www.pudn.com > javasnmpv3.rar > test_app.cpp


/*_############################################################################
  _## 
  _##  test_app.cpp  
  _##
  _##  SNMP++v3.2.22
  _##  -----------------------------------------------
  _##  Copyright (c) 2001-2007 Jochen Katz, Frank Fock
  _##
  _##  This software is based on SNMP++2.6 from Hewlett Packard:
  _##  
  _##    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 and Jochen Katz make 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. 
  _##  
  _##  Stuttgart, Germany, Wed May  2 23:22:30 CEST 2007 
  _##  
  _##########################################################################*/
char test_app_cpp_version[]="@(#) SNMP++ $Id: test_app.cpp 80 2004-05-28 18:47:24Z katz $";

#include      // For exit() function prototype
#include "snmp_pp/snmp_pp.h"

#ifdef SNMP_PP_NAMESPACE
using namespace Snmp_pp;
#endif

#if (__GNUC__ > 2)
#include 
using std::cerr;
using std::cout;
using std::endl;
using std::flush;
#else
#include 
#endif

// default request oids
#define NUM_SYS_VBS	6
#define sysDescr	"1.3.6.1.2.1.1.1.0"
#define sysObjectID	"1.3.6.1.2.1.1.2.0"
#define sysUpTime	"1.3.6.1.2.1.1.3.0"
#define sysContact	"1.3.6.1.2.1.1.4.0"
#define sysName		"1.3.6.1.2.1.1.5.0"
#define sysLocation	"1.3.6.1.2.1.1.6.0"
//#define sysServices	"1.3.6.1.2.1.1.7.0" // not all agents support this...

// default notification oid
#define coldStart	"1.3.6.1.6.3.1.1.4.3.0.1"

int main(int argc, char **argv)
{
  int status;
  char *req_str      = (char*) "get";
  //  char *dflt_req_oid = (char*) sysDescr;
  char *dflt_trp_oid = (char*) coldStart;
  char *genAddrStr   = (char*) "127.0.0.1" ;		  // localhost
  char *oid_str      = (char*) NULL;

  if (argc > 1) genAddrStr = argv[1];
  if (argc > 2) req_str    = argv[2];
  if (argc > 3) oid_str    = argv[3];

  Snmp::socket_startup();  // Initialize socket subsystem

  IpAddress ipAddr(genAddrStr);
  if (!ipAddr.valid()) {
    cout << "Invalid destination: " << genAddrStr << endl;
    return(1);
  }

  // bind to any port and use IPv6 if needed
  Snmp snmp(status, 0, (ipAddr.get_ip_version() == Address::version_ipv6));
  if (status){
    cout << "Failed to create SNMP Session: " << status << endl;
    return(1);
  }
  cout << "Created session successfully" << endl;


  CTarget target(ipAddr);
  if (! target.valid()) {
    cout << "Invalid target" << endl;
    return(1);
  }

  Pdu pdu;
  Vb vb;
  if ( strcmp(req_str, "get") == 0 ) {

    Vb vbl[NUM_SYS_VBS];
    vbl[0].set_oid(sysDescr);
    vbl[1].set_oid(sysObjectID);
    vbl[2].set_oid(sysUpTime);
    vbl[3].set_oid(sysContact);
    vbl[4].set_oid(sysName);
    vbl[5].set_oid(sysLocation);
//    vbl[6].set_oid(sysServices);

    cout << "Send a GET-REQUEST to: " << ipAddr.get_printable() << endl;
    if ( ! oid_str ) {
      if ( strcmp(genAddrStr,"localhost" ) == 0 ||
	   strcmp(genAddrStr, "127.0.0.1") == 0 ){
	pdu.set_vblist(vbl, NUM_SYS_VBS);
      } else {
	for (int i=0; i