示例#1
0
MainWindow::~MainWindow()
{
  if (snmp)
  {
    delete snmp;
    snmp = 0;
  }
  if (v3mp)
  {
    USM *usm = v3mp->get_usm();

    // Save USM users with their passwords into a file
    // The passwords are not encrypted!
    if (SNMPv3_USM_OK != usm->save_users(FILE_USERS))
    {
      QString err = "\nCould not save users to file file.\n";
      text_edit_output->append(err);
    }
    
    delete v3mp;
    v3mp = 0;
  }

  Snmp::socket_cleanup();  // Shut down socket subsystem
}
示例#2
0
void MainWindow::update_combobox_sec_name()
{
  USM *usm = v3mp->get_usm();

  combo_box_sec_name->clear();

  // get all security names
  usm->lock_user_name_table(); // lock table for peek_XXX()

  const struct UsmUserNameTableEntry *user = usm->peek_first_user();
  QStringList names;
  QString initial("initial");
  QString to_add;
  while (user)
  {
    to_add.setAscii((const char*)(user->usmUserSecurityName.data()),
		     user->usmUserSecurityName.len());
    if (!names.contains(to_add) && (to_add != initial))
      names += to_add;
    
    user = usm->peek_next_user(user);
  }
  usm->unlock_user_name_table(); // unlock table

  combo_box_sec_name->insertStringList(names);
}
示例#3
0
int CSVBaseSNMP::InitSNMPV3()
{
    static const char chEngineId[] = "snmpWalk";                    //Engine ID
    static const char chFilename[] = "snmpv3_boot_counter";         //Local File Name

    unsigned int snmpEngineBoots = 0;
    int nStatus = 0;
    //Get Current Boot Counter
    nStatus = getBootCounter(chFilename, chEngineId, snmpEngineBoots);
    if ( nStatus != SNMPv3_OK && nStatus < SNMPv3_FILEOPEN_ERROR )
    {
        return 1;
    }

    //Add Boot Counter
    snmpEngineBoots ++;
    //Save Boot Counter In File
    nStatus = saveBootCounter(chFilename, chEngineId, snmpEngineBoots);
    if (nStatus != SNMPv3_OK)
    {//Save File Failed
        return 1;
    }

    //Construct Status
    int construct_status;
    //Create v3MP Class By Engine ID and Boot Counter, return Result into
    //Construct status
    v3MP *v3_MP = new v3MP(chEngineId, snmpEngineBoots, construct_status);
    if(v3_MP)
    {
        //Get v3MP Property (Point to USM Class)
        USM *usm = v3_MP->get_usm();
        //Set USM Discovery Mode
        usm->set_discovery_mode();
        //Add Auth User , Auth Password and Private Password
        usm->add_usm_user(m_szSecurityName, m_lAuthProtocol, m_lPrivProtocol, m_szAuthPassword, m_szPrivPassword);
        //Return Status
        delete v3_MP;
    }

    return 0;
}
示例#4
0
/////////////////////////////////////////////////////////////////////////////
// 函数:  InitSNMPV3                                                                 //
// 说明: 当SNMP版本是3,初始化环境变量                                     //
// 参数:                                                                   //
//		无															       //
// 返回值:                                                                 //
//		成功为0 ,否则为1                                                  //
/////////////////////////////////////////////////////////////////////////////
int BasicSNMP::InitSNMPV3()
{
	char *engineId = "snmpWalk";//Engine ID
	char *filename = "snmpv3_boot_counter"; //Local File Name
	unsigned int snmpEngineBoots = 0;
	int status = 0;
	//Get Current Boot Counter
	status = getBootCounter(filename, engineId, snmpEngineBoots);
	if ((status != SNMPv3_OK) && (status < SNMPv3_FILEOPEN_ERROR))
	{
		return 1;
	}
	//Add Boot Counter
	snmpEngineBoots++;
	//Save Boot Counter In File
	status = saveBootCounter(filename, engineId, snmpEngineBoots);
	if (status != SNMPv3_OK)
	{//Save File Failed
		return 1;
	}
	//Construct Status
	int construct_status;
	//Create v3MP Class By Engine ID and Boot Counter, return Result into
	//Construct status
	v3MP *v3_MP = new v3MP(engineId, snmpEngineBoots, construct_status);
	//Get v3MP Property (Point to USM Class)
	USM *usm = v3_MP->get_usm();
	//Set USM Discovery Mode
	usm->set_discovery_mode();
	//Add Auth User , Auth Password and Private Password
	usm->add_usm_user(securityName, authProtocol, privProtocol,
		   authPassword, privPassword);
	//Return Status
	delete v3_MP;
	return 0;
}
示例#5
0
MainWindow::MainWindow(QWidget* parent, const char* name, WFlags fl)
  : MainWindowPrivate(parent, name, fl)
{
  int status;

#ifndef _NO_LOGGING
  DefaultLog::log()->set_filter(ERROR_LOG, 5);
  DefaultLog::log()->set_filter(WARNING_LOG, 5);
  DefaultLog::log()->set_filter(EVENT_LOG, 5);
  DefaultLog::log()->set_filter(INFO_LOG, 5);
  DefaultLog::log()->set_filter(DEBUG_LOG, 8);

  // Write debug info to a file
  DefaultLog::init(new AgentLogImpl("QtExample.log"));
#endif

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

  connect(&timer, SIGNAL(timeout()), this, SLOT(timer_expired()));

  // get the Boot counter (you may use any own method for this)
  char *engineId = "not_needed";
  char *filename = "snmpv3_boot_counter";
  unsigned int snmpEngineBoots = 0;

  status = getBootCounter(filename, engineId, snmpEngineBoots);
  if ((status != SNMPv3_OK) && (status < SNMPv3_FILEOPEN_ERROR))
  {
    QString err = QString("Error loading snmpEngineBoots counter: %1\n")
                         .arg(status);
    text_edit_output->append(err);
  }

  // increase the boot counter
  snmpEngineBoots++;

  // save the boot counter
  status = saveBootCounter(filename, engineId, snmpEngineBoots);
  if (status != SNMPv3_OK)
  {
    QString err = QString("Error saving snmpEngineBoots counter: %1\n")
                         .arg(status);
    text_edit_output->append(err);
  }

  // Create our SNMP session object
  snmp = new Snmp(status);
  if (status != SNMP_CLASS_SUCCESS)
  {
    QString err = "\nCould not create SNMP++ session:\n";
    err += Snmp::error_msg(status);
    text_edit_output->append(err);
  }

  // If _SNMPv3 is enabled we MUST create ONE v3MP object!
  v3mp = new v3MP(engineId, snmpEngineBoots, status);
  if (status != SNMPv3_MP_OK)
  {
    QString err = "\nCould not create v3MP object:\n";
    err += Snmp::error_msg(status);
    text_edit_output->append(err);
  }

  // The v3MP creates a USM object, get the pointer to it
  USM *usm = v3mp->get_usm();

  // Load the USM users from a file
  if (SNMPv3_USM_OK != usm->load_users(FILE_USERS))
  {
    QString err = "\nCould not load users from file.\n";
    text_edit_output->append(err);
  }    

#if 0 // Localized users will be created automatically!
  if (SNMPv3_USM_OK != usm->load_localized_users(FILE_LOCALIZED_USERS))
  {
    QString err = "\nCould not load localized users from file.\n";
    text_edit_output->append(err);
  }    
#endif
  update_combobox_sec_name();
}
示例#6
0
int main(int argc, char **argv)
{
    //---------[ check the arg count ]----------------------------------------
    if ( argc < 2) {
        cout << "Usage:\n";
        cout << argv[0] << " IpAddress | DNSName [Oid] [options]\n";
        cout << "Oid: sysDescr object is default\n";
        cout << "options: -vN , use SNMP version 1, 2 or 3, default is 1\n";
        cout << "         -PPort , remote port to use\n";
        cout << "         -CCommunity_name, specify community default is 'public' \n";
        cout << "         -rN , retries default is N = 1 retry\n";
        cout << "         -tN , timeout in hundredths of seconds; default is N = 100\n";
#ifdef _SNMPv3
        cout << "         -snSecurityName, " << endl;
        cout << "         -slN , securityLevel to use, default N = 3 = authPriv" << endl;
        cout << "         -smN , securityModel to use, only default N = 3 = USM possible\n";
        cout << "         -cnContextName, default empty string" << endl;
        cout << "         -ceContextEngineID, as hex e.g. 800007E580, default empty string" << endl;
        cout << "         -authPROT, use authentication protocol NONE, SHA or MD5\n";
        cout << "         -privPROT, use privacy protocol NONE, DES, 3DESEDE, IDEA, AES128, AES192 or AES256\n";
        cout << "         -uaAuthPassword\n";
        cout << "         -upPrivPassword\n";
#endif
        return 1;
    }

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

    //---------[ make a GenAddress and Oid object to retrieve ]---------------
    UdpAddress 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 1;
    }
    Oid oid("1.3.6.1.2.1.1.1");      // default is sysDescr
    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 1;
            }
        }
    }

    //---------[ determine options to use ]-----------------------------------
    snmp_version version=version1;                  // default is v1
    int retries=1;                                  // default retries is 1
    int timeout=100;                                // default is 1 second
    u_short port=161;                               // default snmp port is 161
    OctetStr community("public");                   // community name

#ifdef _SNMPv3
    OctetStr privPassword("");
    OctetStr authPassword("");
    OctetStr securityName("");
    int securityModel = SecurityModel_USM;
    int securityLevel = SecurityLevel_authPriv;
    OctetStr contextName("");
    OctetStr contextEngineID("");
    long authProtocol = SNMPv3_usmNoAuthProtocol;
    long privProtocol = SNMPv3_usmNoPrivProtocol;
    v3MP *v3_MP;
#endif

    char *ptr;

    for(int x=1; x<argc; x++) {                         // parse for version
        if ( strstr( argv[x],"-v2")!= 0) {
            version = version2c;
            continue;
        }
        if ( strstr( argv[x],"-r")!= 0) {                 // parse for retries
            ptr = argv[x];
            ptr++;
            ptr++;
            retries = atoi(ptr);
            if (( retries<0)|| (retries>5)) retries=1;
            continue;
        }
        if ( strstr( argv[x], "-t")!=0) {                 // parse for timeout
            ptr = argv[x];
            ptr++;
            ptr++;
            timeout = atoi( ptr);
            if (( timeout < 100)||( timeout>500)) timeout=100;
            continue;
        }
        if ( strstr( argv[x],"-C")!=0) {
            ptr = argv[x];
            ptr++;
            ptr++;
            community = ptr;
            continue;
        }
        if ( strstr( argv[x],"-P")!=0) {
            ptr = argv[x];
            ptr++;
            ptr++;
            sscanf(ptr, "%hu", &port);
            continue;
        }

#ifdef _SNMPv3
        if ( strstr( argv[x],"-v3")!= 0) {
            version = version3;
            continue;
        }
        if ( strstr( argv[x],"-auth") != 0) {
            ptr = argv[x];
            ptr+=5;
            if (strcasecmp(ptr, "SHA") == 0)
                authProtocol = SNMP_AUTHPROTOCOL_HMACSHA;
            else if (strcasecmp(ptr, "MD5") == 0)
                authProtocol = SNMP_AUTHPROTOCOL_HMACMD5;
            else
                authProtocol = SNMP_AUTHPROTOCOL_NONE;
            continue;
        }
        if ( strstr( argv[x],"-priv") != 0) {
            ptr = argv[x];
            ptr+=5;
            if (strcasecmp(ptr, "DES") == 0)
                privProtocol = SNMP_PRIVPROTOCOL_DES;
            else if (strcasecmp(ptr, "3DESEDE") == 0)
                privProtocol = SNMP_PRIVPROTOCOL_3DESEDE;
            else if (strcasecmp(ptr, "IDEA") == 0)
                privProtocol = SNMP_PRIVPROTOCOL_IDEA;
            else if (strcasecmp(ptr, "AES128") == 0)
                privProtocol = SNMP_PRIVPROTOCOL_AES128;
            else if (strcasecmp(ptr, "AES192") == 0)
                privProtocol = SNMP_PRIVPROTOCOL_AES192;
            else if (strcasecmp(ptr, "AES256") == 0)
                privProtocol = SNMP_PRIVPROTOCOL_AES256;
            else
                privProtocol = SNMP_PRIVPROTOCOL_NONE;
            printf("\n\nPrivProt : %ld\n", privProtocol);
            continue;
        }
        if ( strstr( argv[x],"-sn")!=0) {
            ptr = argv[x];
            ptr+=3;
            securityName = ptr;
            continue;
        }
        if ( strstr( argv[x], "-sl")!=0) {
            ptr = argv[x];
            ptr+=3;
            securityLevel = atoi( ptr);
            if (( securityLevel < SecurityLevel_noAuthNoPriv) ||
                    ( securityLevel > SecurityLevel_authPriv))
                securityLevel = SecurityLevel_authPriv;
            continue;
        }
        if ( strstr( argv[x], "-sm")!=0) {
            ptr = argv[x];
            ptr+=3;
            securityModel = atoi( ptr);
            if (( securityModel < SecurityModel_v1) ||
                    ( securityModel > SecurityModel_USM))
                securityModel = SecurityModel_USM;
            continue;
        }
        if ( strstr( argv[x],"-cn")!=0) {
            ptr = argv[x];
            ptr+=3;
            contextName = ptr;
            continue;
        }
        if ( strstr( argv[x],"-ce")!=0) {
            ptr = argv[x];
            ptr+=3;
            contextEngineID = OctetStr::from_hex_string(ptr);
            continue;
        }
        if ( strstr( argv[x],"-ua")!=0) {
            ptr = argv[x];
            ptr+=3;
            authPassword = ptr;
            continue;
        }
        if ( strstr( argv[x],"-up")!=0) {
            ptr = argv[x];
            ptr+=3;
            privPassword = ptr;
            continue;
        }
#endif
    }

    //----------[ create a SNMP++ session ]-----------------------------------
    int status;
    // bind to any port and use IPv6 if needed
    Snmp snmp(status, 0, (address.get_ip_version() == Address::version_ipv6));

    if ( status != SNMP_CLASS_SUCCESS) {
        cout << "SNMP++ Session Create Fail, " << snmp.error_msg(status) << "\n";
        return 1;
    }

    //---------[ init SnmpV3 ]--------------------------------------------
#ifdef _SNMPv3
    if (version == version3) {
        char *engineId = "snmpNextAsync";
        char *filename = "snmpv3_boot_counter";
        unsigned int snmpEngineBoots = 0;
        int status;

        status = getBootCounter(filename, engineId, snmpEngineBoots);
        if ((status != SNMPv3_OK) && (status < SNMPv3_FILEOPEN_ERROR))
        {
            cout << "Error loading snmpEngineBoots counter: " << status << endl;
            return 1;
        }
        snmpEngineBoots++;
        status = saveBootCounter(filename, engineId, snmpEngineBoots);
        if (status != SNMPv3_OK)
        {
            cout << "Error saving snmpEngineBoots counter: " << status << endl;
            return 1;
        }

        int construct_status;
        v3_MP = new v3MP(engineId, snmpEngineBoots, construct_status);

        USM *usm = v3_MP->get_usm();
        usm->add_usm_user(securityName,
                          authProtocol, privProtocol,
                          authPassword, privPassword);
    }
    else
    {
        // MUST create a dummy v3MP object if _SNMPv3 is enabled!
        int construct_status;
        v3_MP = new v3MP("dummy", 0, construct_status);
    }
#endif

    //--------[ build up SNMP++ object needed ]-------------------------------
    Pdu pdu;                               // construct a Pdu object
    Vb vb;                                 // construct a Vb object
    vb.set_oid( oid);                      // set the Oid portion of the Vb
    pdu += vb;                             // add the vb to the Pdu

    address.set_port(port);
    CTarget ctarget( address);             // make a target using the address
#ifdef _SNMPv3
    UTarget utarget( address);

    if (version == version3) {
        utarget.set_version( version);          // set the SNMP version SNMPV1 or V2 or V3
        utarget.set_retry( retries);            // set the number of auto retries
        utarget.set_timeout( timeout);          // set timeout
        utarget.set_security_model( securityModel);
        utarget.set_security_name( securityName);
        pdu.set_security_level( securityLevel);
        pdu.set_context_name (contextName);
        pdu.set_context_engine_id(contextEngineID);
    }
    else {
#endif
        ctarget.set_version( version);         // set the SNMP version SNMPV1 or V2
        ctarget.set_retry( retries);           // set the number of auto retries
        ctarget.set_timeout( timeout);         // set timeout
        ctarget.set_readcommunity( community); // set the read community name
#ifdef _SNMPv3
    }
#endif

    //-------[ issue the request, blocked mode ]-----------------------------
    cout << "SNMP++ GetNext to " << argv[1] << " SNMPV"
#ifdef _SNMPv3
         << ((version==version3) ? (version) : (version+1))
#else
         << (version+1)
#endif
         << " Retries=" << retries
         << " Timeout=" << timeout * 10 <<"ms";
#ifdef _SNMPv3
    if (version == version3)
        cout << endl
             << "securityName= " << securityName.get_printable()
             << ", securityLevel= " << securityLevel
             << ", securityModel= " << securityModel << endl
             << "contextName= " << contextName.get_printable()
             << ", contextEngineID= " << contextEngineID.get_printable()
             << endl;
    else
#endif
        cout << " Community=" << community.get_printable() << endl << flush;

    SnmpTarget *target;
#ifdef _SNMPv3
    if (version == version3)
        target = &utarget;
    else
#endif
        target = &ctarget;

    status = snmp.get_next( pdu, *target, callback,NULL);

    if (status == SNMP_CLASS_SUCCESS)
    {
        cout << "Async GetNext Request sent." << endl;
    }
    else
        cout << "SNMP++ GetNext Error, " << snmp.error_msg( status)
             << " (" << status <<")" << endl ;

    for (int t=1; t<=10; t++)
    {
        snmp.eventListHolder->SNMPProcessPendingEvents();
#ifdef WIN32
        Sleep(1000);
#else
        sleep(1);
#endif
    }

    Snmp::socket_cleanup();  // Shut down socket subsystem
}
示例#7
0
int main(int argc, char **argv)
{
   //---------[ check the arg count ]----------------------------------------
   if ( argc < 2 )
     usage();
   if ( strstr( argv[1],"-h") != 0 )
     help();
   if ( strstr( argv[1],"-?") != 0 )
     usage();

#if !defined(_NO_LOGGING) && !defined(WITH_LOG_PROFILES)
   // Set filter for logging
   DefaultLog::log()->set_filter(ERROR_LOG, 7);
   DefaultLog::log()->set_filter(WARNING_LOG, 7);
   DefaultLog::log()->set_filter(EVENT_LOG, 7);
   DefaultLog::log()->set_filter(INFO_LOG, 7);
   DefaultLog::log()->set_filter(DEBUG_LOG, 7);
#endif

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

   //---------[ make a GenAddress and Oid object to retrieve ]---------------
   UdpAddress address( argv[1]);      // make a SNMP++ Generic address
   if ( !address.valid()) {           // check validity of address
	  cout << "Invalid Address or DNS Name, " << argv[1] << "\n";
	  usage();
   }
   Oid oid("1.3.6.1.2.1.1.1");      // default is sysDescr
   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";
		    usage();
         }
      }
   }
   // cout << "GETNEXT for OID " << oid.get_printable() << "\n";

   //---------[ determine options to use ]-----------------------------------
   snmp_version version=version1;                  // default is v1
   int retries=1;                                  // default retries is 1
   int timeout=100;                                // default is 1 second
   u_short port=161;                               // default snmp port is 161
   OctetStr community("public");                   // community name

#ifdef _SNMPv3
   OctetStr privPassword("");
   OctetStr authPassword("");
   OctetStr securityName("");
   int securityModel = SNMP_SECURITY_MODEL_USM;
   int securityLevel = SNMP_SECURITY_LEVEL_AUTH_PRIV;
   OctetStr contextName("");
   OctetStr contextEngineID("");
   long authProtocol = SNMP_AUTHPROTOCOL_NONE;
   long privProtocol = SNMP_PRIVPROTOCOL_NONE;
   v3MP *v3_MP;
#endif

   char *ptr;

   for(int x=1;x<argc;x++) {                           // parse for version
     if ( strstr( argv[x],"-v2")!= 0) {
       version = version2c;
       continue;
     }
     if ( strstr( argv[x],"-r")!= 0) {                 // parse for retries
       ptr = argv[x]; ptr++; ptr++;
       retries = atoi(ptr);
       if (( retries<0)|| (retries>5)) retries=1; 
       continue;
     }
     if ( strstr( argv[x], "-t")!=0) {                 // parse for timeout
       ptr = argv[x]; ptr++; ptr++;
       timeout = atoi( ptr);
       if (( timeout < 100)||( timeout>500)) timeout=100;
       continue;
     }
     if ( strstr( argv[x],"-C")!=0) {
       ptr = argv[x]; ptr++; ptr++;
       community = ptr;
       continue;
     }
     if ( strstr( argv[x],"-P")!=0) {
       ptr = argv[x]; ptr++; ptr++;
       sscanf(ptr, "%hu", &port);
       continue;
     }

#ifdef WITH_LOG_PROFILES
     if ( strstr( argv[x], "-L" ) != 0 ) {
       ptr = argv[x]; ptr++; ptr++;
       DefaultLog::log()->set_profile(ptr);
     }
#endif

#ifdef _SNMPv3
     if ( strstr( argv[x],"-v3")!= 0) {
       version = version3;
       continue;
     }
     if ( strstr( argv[x],"-auth") != 0) {
       ptr = argv[x]; ptr+=5;
       if (strcasecmp(ptr, "SHA") == 0)
	 authProtocol = SNMP_AUTHPROTOCOL_HMACSHA;
       else if (strcasecmp(ptr, "MD5") == 0)
	 authProtocol = SNMP_AUTHPROTOCOL_HMACMD5;
       else if (strcasecmp(ptr, "NONE") == 0)
	 authProtocol = SNMP_AUTHPROTOCOL_NONE;
       else
	 cout << "Warning: ignoring unknown auth protocol: " << ptr << endl;
       continue;
     }
     if ( strstr( argv[x],"-priv") != 0) {
       ptr = argv[x]; ptr+=5;
       if (strcasecmp(ptr, "DES") == 0)
	   privProtocol = SNMP_PRIVPROTOCOL_DES;
       else if (strcasecmp(ptr, "3DESEDE") == 0)
	   privProtocol = SNMP_PRIVPROTOCOL_3DESEDE;
       else if (strcasecmp(ptr, "IDEA") == 0)
	   privProtocol = SNMP_PRIVPROTOCOL_IDEA;
       else if (strcasecmp(ptr, "AES128") == 0)
	   privProtocol = SNMP_PRIVPROTOCOL_AES128;
       else if (strcasecmp(ptr, "AES192") == 0)
	   privProtocol = SNMP_PRIVPROTOCOL_AES192;
       else if (strcasecmp(ptr, "AES256") == 0)
	   privProtocol = SNMP_PRIVPROTOCOL_AES256;
       else if (strcasecmp(ptr, "NONE") == 0)
	   privProtocol = SNMP_PRIVPROTOCOL_NONE;
       else
	 cout << "Warning: ignoring unknown priv protocol: " << ptr << endl;
       continue;
     }
     if ( strstr( argv[x],"-sn")!=0) {
       ptr = argv[x]; ptr+=3;
       securityName = ptr;
       continue;
      }
     if ( strstr( argv[x], "-sl")!=0) {
       ptr = argv[x]; ptr+=3;
       securityLevel = atoi( ptr);
       if (( securityLevel < SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV) ||
           ( securityLevel > SNMP_SECURITY_LEVEL_AUTH_PRIV))
         securityLevel = SNMP_SECURITY_LEVEL_AUTH_PRIV;
       continue;
     }
     if ( strstr( argv[x], "-sm")!=0) {
       ptr = argv[x]; ptr+=3;
       securityModel = atoi( ptr);
       if (( securityModel < SNMP_SECURITY_MODEL_V1) ||
           ( securityModel > SNMP_SECURITY_MODEL_USM))
         securityModel = SNMP_SECURITY_MODEL_USM;
       continue;
     }
     if ( strstr( argv[x],"-cn")!=0) {
       ptr = argv[x]; ptr+=3;
       contextName = ptr;
       continue;
     }
     if ( strstr( argv[x],"-ce")!=0) {
       ptr = argv[x]; ptr+=3;
       contextEngineID = OctetStr::from_hex_string(ptr);
       continue;
     }
     if ( strstr( argv[x],"-ua")!=0) {
       ptr = argv[x]; ptr+=3;
       authPassword = ptr;
       continue;
     }
     if ( strstr( argv[x],"-up")!=0) {
       ptr = argv[x]; ptr+=3;
       privPassword = ptr;
       continue;
     }
#endif
  }

   //----------[ create a SNMP++ session ]-----------------------------------
   int status;
   // bind to any port and use IPv6 if needed
   Snmp snmp(status, 0, (address.get_ip_version() == Address::version_ipv6));

   if ( status != SNMP_CLASS_SUCCESS) {
      cout << "SNMP++ Session Create Fail, " << snmp.error_msg(status) << "\n";
      return 1;
   }

   //---------[ init SnmpV3 ]--------------------------------------------
#ifdef _SNMPv3
   if (version == version3) {
     const char *engineId = "snmpNext";
     const char *filename = "snmpv3_boot_counter";
     unsigned int snmpEngineBoots = 0;
     int status;

     status = getBootCounter(filename, engineId, snmpEngineBoots);
     if ((status != SNMPv3_OK) && (status < SNMPv3_FILEOPEN_ERROR))
     {
       cout << "Error loading snmpEngineBoots counter: " << status << endl;
       return 1;
     }
     snmpEngineBoots++;
     status = saveBootCounter(filename, engineId, snmpEngineBoots);
     if (status != SNMPv3_OK)
     {
       cout << "Error saving snmpEngineBoots counter: " << status << endl;
       return 1;
     }

     int construct_status;
     v3_MP = new v3MP(engineId, snmpEngineBoots, construct_status);
     if (construct_status != SNMPv3_MP_OK)
     {
       cout << "Error initializing v3MP: " << construct_status << endl;
       return 1;
     }

     USM *usm = v3_MP->get_usm();
     usm->add_usm_user(securityName,
		       authProtocol, privProtocol,
		       authPassword, privPassword);
   }
   else
   {
     // MUST create a dummy v3MP object if _SNMPv3 is enabled!
     int construct_status;
     v3_MP = new v3MP("dummy", 0, construct_status);
   }
#endif

   //--------[ build up SNMP++ object needed ]-------------------------------
   Pdu pdu;                               // construct a Pdu object
   Vb vb;                                 // construct a Vb object
   vb.set_oid( oid);                      // set the Oid portion of the Vb
   pdu += vb;                             // add the vb to the Pdu

   address.set_port(port);
   CTarget ctarget( address);             // make a target using the address
#ifdef _SNMPv3
   UTarget utarget( address);

   if (version == version3) {
     utarget.set_version( version);          // set the SNMP version SNMPV1 or V2 or V3
     utarget.set_retry( retries);            // set the number of auto retries
     utarget.set_timeout( timeout);          // set timeout
     utarget.set_security_model( securityModel);
     utarget.set_security_name( securityName);
     pdu.set_security_level( securityLevel);
     pdu.set_context_name (contextName);
     pdu.set_context_engine_id(contextEngineID);
   }
   else {
#endif
     ctarget.set_version( version);         // set the SNMP version SNMPV1 or V2
     ctarget.set_retry( retries);           // set the number of auto retries
     ctarget.set_timeout( timeout);         // set timeout
     ctarget.set_readcommunity( community); // set the read community name
#ifdef _SNMPv3
   }
#endif

   //-------[ issue the request, blocked mode ]-----------------------------
   cout << "SNMP++ GetNext to " << argv[1] << " SNMPV"
#ifdef _SNMPv3
        << ((version==version3) ? (version) : (version+1))
#else
        << (version+1)
#endif
        << " Retries=" << retries
        << " Timeout=" << timeout * 10 <<"ms";
#ifdef _SNMPv3
   if (version == version3)
     cout << endl
          << "securityName= " << securityName.get_printable()
          << ", securityLevel= " << securityLevel
          << ", securityModel= " << securityModel << endl
          << "contextName= " << contextName.get_printable()
          << ", contextEngineID= " << contextEngineID.get_printable()
          << endl;
   else
#endif
     cout << " Community=" << community.get_printable() << endl << flush;

   SnmpTarget *target;
#ifdef _SNMPv3
   if (version == version3)
     target = &utarget;
   else
#endif
     target = &ctarget;

   status = snmp.get_next( pdu, *target);

   if (status == SNMP_CLASS_SUCCESS)
   {
     pdu.get_vb( vb,0);
#ifdef _SNMPv3
     if (pdu.get_type() == REPORT_MSG) {
       cout << "Received a report pdu: "
            << snmp.error_msg(vb.get_printable_oid()) << endl;
     }
#endif
     cout << "Oid = " << vb.get_printable_oid() << endl
	  << "Value = " << vb.get_printable_value() << endl;
   }
   else
   {
     cout << "SNMP++ GetNext Error, " << snmp.error_msg( status)
	  << " (" << status <<")" << endl;
   }

   Snmp::socket_cleanup();  // Shut down socket subsystem
}
示例#8
0
int main_bulk_jun( int argc, char **argv)
{
	{
	  cout << "Usage:\n";
	  cout << "snmpBulk IpAddress | DNSName [Oid [Oid...]] [options]\n";
	  cout << "Oid: sysDescr object is default\n";
	  cout << "options: -v1 , use SNMPV1, default\n";
	  cout << "         -v2 , use SNMPV2\n";
#ifdef _SNMPv3
          cout << "         -v3 , use SNMPV3\n";
#endif
	  cout << "         -pPort , remote port to use\n";
	  cout << "         -CCommunity_name, specify community default is 'public' \n";
	  cout << "         -rN , retries default is N = 1 retry\n";
	  cout << "         -tN , timeout in hundredths of seconds; default is N = 100\n";
	  cout << "         -nN , non-repeaters default is N = 0\n";
	  cout << "         -mN , max-repetitions default is  N = 1\n";
#ifdef _SNMPv3
          cout << "         -snSecurityName, " << endl;
          cout << "         -slN , securityLevel to use, default N = 3 = authPriv" << endl;
          cout << "         -smN , securityModel to use, only default N = 3 = USM possible\n";
          cout << "         -cnContextName, default """"" << endl;
          cout << "         -ceContextEngineID, default """"" << endl;
          cout << "         -md5 , use MD5 authentication protocol\n";
          cout << "         -sha , use SHA authentication protocol\n";
          cout << "         -des , use DES privacy protocol\n";
          cout << "         -idea, use IDEA privacy protocol\n";
          cout << "         -aes128, use AES128 privacy protocol\n";
          cout << "         -aes192, use AES192 privacy protocol\n";
          cout << "         -aes256, use AES256 privacy protocol\n";
          cout << "         -uaAuthPassword\n";
          cout << "         -upPrivPassword\n";
#endif
	}
   //---------[ check the arg count ]----------------------------------------
   if ( argc < 2) {
	  
	  return 1;
   }

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

   //---------[ make a GenAddress and Oid object to retrieve ]---------------
   UdpAddress 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 1;
   }
   Pdu pdu;                              // construct a Pdu object
   Vb vb;                                // construct a Vb object
   if ( argc >= 3) {                  // if 3 args, then use the callers Oid
	int i=2;
	while ((strstr(argv[i],"-")==0) && (i<argc)) {
		Oid oid(argv[i]);
		if ( !oid.valid()) {            // check validity of user oid
			cout << "Invalid Oid, " << argv[2] << "\n";
			return -2;
		}
		vb.set_oid(oid);
		pdu += vb;
		i++;
	}
   }
   else {
     Oid oid("1.3.6.1.2.1.1.1");      // default is sysDescr
     vb.set_oid(oid);
     pdu += vb;                            // add the vb to the Pdu
   }

   //jun add
   {
		Oid oid ("1.3.6.1.2.1.1.6.0");
		vb.set_oid(oid);
		pdu += vb;
		Oid oid2("1.3.6.1.2.1.1.3.0");
		vb.set_oid(oid2);
		pdu += vb;
		Oid oid3("1.3.6.1.2.1.1.5.0");
		vb.set_oid(oid3);
		pdu += vb;
		Oid oidx("1.3.6.1.2.1.1.5");
		vb.set_oid(oidx);
		pdu += vb;
		Oid oid4("1.3.6.1.2.1.25.5.1.1");
		vb.set_oid(oid4);
		pdu += vb;
   }
   //---------[ determine options to use ]-----------------------------------
   snmp_version version=version1;                  // default is v1
   int retries=1;                                  // default retries is 1
   int timeout=100;                                // default is 1 second
   u_short port=161;                               // default snmp port is 161
   OctetStr community("public");                   // community name
   int non_reps=2;                                 // non repeaters default is 0
   int max_reps=20;                                 // maximum repetitions default is 1

#ifdef _SNMPv3
   OctetStr privPassword("");
   OctetStr authPassword("");
   OctetStr securityName("");
   int securityModel = SecurityModel_USM;
   int securityLevel = SecurityLevel_authPriv;
   OctetStr contextName("");
   OctetStr contextEngineID("");
   long authProtocol = SNMPv3_usmNoAuthProtocol;
   long privProtocol = SNMPv3_usmNoPrivProtocol;
   v3MP *v3_MP;
#endif

   char *ptr;

   for(int x=1;x<argc;x++) {                           // parse for version
     if ( strstr( argv[x],"-v2")!= 0) {
       version = version2c;
       continue;
     }
     if ( strstr( argv[x],"-r")!= 0) {                 // parse for retries
       ptr = argv[x]; ptr++; ptr++;
       retries = atoi(ptr);
       if (( retries<0)|| (retries>5)) retries=1; 
       continue;
     }
     if ( strstr( argv[x], "-t")!=0) {                 // parse for timeout
       ptr = argv[x]; ptr++; ptr++;
       timeout = atoi( ptr);
       if (( timeout < 100)||( timeout>500)) timeout=100;
       continue;
     }
     if ( strstr( argv[x],"-n")!=0) {                 // parse for non repeaters
       ptr = argv[x];ptr++;ptr++;
       non_reps=atoi( ptr);
       if (( non_reps < 0)||( non_reps>10)) non_reps=0;
     }
     if ( strstr( argv[x],"-m")!=0) {                 // parse for max repetitions 
       ptr = argv[x];ptr++;ptr++;
       max_reps=atoi( ptr);
       if ( max_reps < 0) max_reps=1;
     }
     if ( strstr( argv[x],"-C")!=0) {
       ptr = argv[x]; ptr++; ptr++;
       community = ptr;
       continue;
     }
     if ( strstr( argv[x],"-p")!=0) {
       ptr = argv[x]; ptr++; ptr++;
       sscanf(ptr, "%hu", &port);
       continue;
     }

#ifdef _SNMPv3
     if ( strstr( argv[x],"-v3")!= 0) {
       version = version3;
       continue;
     }
     if ( strstr( argv[x],"-idea") != 0) {
       ptr = argv[x]; ptr++; ptr++;
       privProtocol = SNMPv3_usmIDEAPrivProtocol;
       continue;
     }
     if ( strstr( argv[x],"-aes128") != 0) {
       ptr = argv[x]; ptr++; ptr++;
       privProtocol = SNMPv3_usmAES128PrivProtocol;
       continue;
     }
     if ( strstr( argv[x],"-aes192") != 0) {
       ptr = argv[x]; ptr++; ptr++;
       privProtocol = SNMPv3_usmAES192PrivProtocol;
       continue;
     }
     if ( strstr( argv[x],"-aes256") != 0) {
       ptr = argv[x]; ptr++; ptr++;
       privProtocol = SNMPv3_usmAES256PrivProtocol;
       continue;
     }
     if ( strstr( argv[x],"-sha") != 0) {
       ptr = argv[x]; ptr++; ptr++;
       authProtocol = SNMPv3_usmHMACSHAAuthProtocol;
       continue;
     }
     if ( strstr( argv[x],"-des") != 0) {
       ptr = argv[x]; ptr++; ptr++;
       privProtocol = SNMPv3_usmDESPrivProtocol;
       continue;
     }
     if ( strstr( argv[x],"-md5") != 0) {
       ptr = argv[x]; ptr++; ptr++;
       authProtocol = SNMPv3_usmHMACMD5AuthProtocol;
       continue;
     }
     if ( strstr( argv[x],"-sn")!=0) {
       ptr = argv[x]; ptr+=3;
       securityName = ptr;
       continue;
      }
     if ( strstr( argv[x], "-sl")!=0) {
       ptr = argv[x]; ptr+=3;
       securityLevel = atoi( ptr);
       if (( securityLevel < SecurityLevel_noAuthNoPriv) ||
           ( securityLevel > SecurityLevel_authPriv))
         securityLevel = SecurityLevel_authPriv;
       continue;
     }
     if ( strstr( argv[x], "-sm")!=0) {
       ptr = argv[x]; ptr+=3;
       securityModel = atoi( ptr);
       if (( securityModel < SecurityModel_v1) ||
           ( securityModel > SecurityModel_USM))
         securityModel = SecurityModel_USM;
       continue;
     }
     if ( strstr( argv[x],"-cn")!=0) {
       ptr = argv[x]; ptr+=3;
       contextName = ptr;
       continue;
     }
     if ( strstr( argv[x],"-ce")!=0) {
       ptr = argv[x]; ptr+=3;
       contextEngineID = ptr;
       continue;
     }
     if ( strstr( argv[x],"-ua")!=0) {
       ptr = argv[x]; ptr+=3;
       authPassword = ptr;
       continue;
     }
     if ( strstr( argv[x],"-up")!=0) {
       ptr = argv[x]; ptr+=3;
       privPassword = ptr;
       continue;
     }
#endif
  }

   //----------[ create a SNMP++ session ]-----------------------------------
   int status;
   // bind to any port and use IPv6 if needed
   Snmp snmp(status, 0, (address.get_ip_version() == Address::version_ipv6));

   if ( status != SNMP_CLASS_SUCCESS) {
      cout << "SNMP++ Session Create Fail, " << snmp.error_msg(status) << "\n";
      return 1;
   }

   //---------[ init SnmpV3 ]--------------------------------------------
#ifdef _SNMPv3
   if (version == version3) {
     char *engineId = "snmpBulk";
     char *filename = "snmpv3_boot_counter";
     unsigned int snmpEngineBoots = 0;
     int status;

     status = getBootCounter(filename, engineId, snmpEngineBoots);
     if ((status != SNMPv3_OK) && (status < SNMPv3_FILEOPEN_ERROR))
     {
       cout << "Error loading snmpEngineBoots counter: " << status << endl;
       return 1;
     }
     snmpEngineBoots++;
     status = saveBootCounter(filename, engineId, snmpEngineBoots);
     if (status != SNMPv3_OK)
     {
       cout << "Error saving snmpEngineBoots counter: " << status << endl;
       return 1;
     }

     int construct_status;
     v3_MP = new v3MP(engineId, snmpEngineBoots, construct_status);

     USM *usm = v3_MP->get_usm();
     usm->add_usm_user(securityName,
		       authProtocol, privProtocol,
		       authPassword, privPassword);
   }
   else
   {
     // MUST create a dummy v3MP object if _SNMPv3 is enabled!
     int construct_status;
     v3_MP = new v3MP("dummy", 0, construct_status);
   }
#endif

   //--------[ build up SNMP++ object needed ]-------------------------------
   address.set_port(port);
   CTarget ctarget( address);             // make a target using the address
#ifdef _SNMPv3
   UTarget utarget( address);

   if (version == version3) {
     utarget.set_version( version);          // set the SNMP version SNMPV1 or V2 or V3
     utarget.set_retry( retries);            // set the number of auto retries
     utarget.set_timeout( timeout);          // set timeout
     utarget.set_security_model( securityModel);
     utarget.set_security_name( securityName);
     pdu.set_security_level( securityLevel);
     pdu.set_context_name (contextName);
     pdu.set_context_engine_id(contextEngineID);
   }
   else {
#endif
     ctarget.set_version( version);         // set the SNMP version SNMPV1 or V2
     ctarget.set_retry( retries);           // set the number of auto retries
     ctarget.set_timeout( timeout);         // set timeout
     ctarget.set_readcommunity( community); // set the read community name
#ifdef _SNMPv3
   }
#endif

   //-------[ issue the request, blocked mode ]-----------------------------
   cout << "SNMP++ GetBulk to " << argv[1] << " SNMPV" 
#ifdef _SNMPv3
        << ((version==version3) ? (version) : (version+1))
#else
        << (version+1)
#endif
        << " Retries=" << retries
	<< " Timeout=" << timeout << "ms"
	<< " Non Reptrs=" << non_reps
	<< " Max Reps=" << max_reps << endl;
#ifdef _SNMPv3
   if (version == version3)
     cout << endl
          << "securityName= " << securityName.get_printable()
          << ", securityLevel= " << securityLevel
          << ", securityModel= " << securityModel << endl
          << "contextName= " << contextName.get_printable()
          << ", contextEngineID= " << contextEngineID.get_printable()
          << endl;
   else
#endif
     cout << " Community=" << community.get_printable() << endl << flush;

   SnmpTarget *target;
#ifdef _SNMPv3
   if (version == version3)
     target = &utarget;
   else
#endif
     target = &ctarget;
   Pdu pdu2 = pdu;

   if (( status = snmp.get_bulk( pdu,*target,non_reps,max_reps))== SNMP_CLASS_SUCCESS) {
     for ( int z=0;z<pdu.get_vb_count();z++) {
       pdu.get_vb( vb,z);
#ifdef _SNMPv3
       if (pdu.get_type() == REPORT_MSG) {
         Oid tmp;
         vb.get_oid(tmp);
         cout << "Received a reportPdu: "
              << snmp.error_msg( tmp) 
              << endl
              << vb.get_printable_oid() << " = "
              << vb.get_printable_value() << endl;
       }
#endif
       cout << "Oid = " << vb.get_printable_oid() << "\n";
       if ( vb.get_syntax() != sNMP_SYNTAX_ENDOFMIBVIEW) {
	 cout << "Value = " << vb.get_printable_value() << "\n\n";
       }
       else {
	 cout << "End of MIB view.\n\n";
       }
     }
   }
   else
     cout << "SNMP++ GetBulk Error, " << snmp.error_msg( status) << "\n";

   pdu = pdu2;
//--------------------
   if (( status = snmp.get_bulk( pdu,*target,non_reps,max_reps))== SNMP_CLASS_SUCCESS) {
     for ( int z=0;z<pdu.get_vb_count();z++) {
       pdu.get_vb( vb,z);
#ifdef _SNMPv3
       if (pdu.get_type() == REPORT_MSG) {
         Oid tmp;
         vb.get_oid(tmp);
         cout << "Received a reportPdu: "
              << snmp.error_msg( tmp) 
              << endl
              << vb.get_printable_oid() << " = "
              << vb.get_printable_value() << endl;
       }
#endif
       cout << "Oid = " << vb.get_printable_oid() << "\n";
       if ( vb.get_syntax() != sNMP_SYNTAX_ENDOFMIBVIEW) {
	 cout << "Value = " << vb.get_printable_value() << "\n\n";
	 
       }
       else {
	 cout << "End of MIB view.\n\n";
       }
     }
   }
   else
     cout << "SNMP++ GetBulk Error, " << snmp.error_msg( status) << "\n";
   //-------------


   Snmp::socket_cleanup();  // Shut down socket subsystem
}
示例#9
0
int main(int argc, char **argv)
{
    //---------[ check the arg count ]----------------------------------------
    if ( argc < 2) {
        cout << "Usage:\n";
        cout << "snmpTraps IpAddress | DNSName [Id] [options]\n";
        cout << "Id = default is 1.3.6.1.6.3.1.1.5.1 = ColdStart";
        cout << "options: -vN , use SNMP version 1, 2 or 3, default is 1\n";
        cout << "         -PPort , remote port to use\n";
        cout << "         -CCommunity_name, specify community default is 'public' \n";
#ifdef _SNMPv3
        cout << "         -snSecurityName, " << endl;
        cout << "         -slN , securityLevel to use, default N = 3 = authPriv" << endl;
        cout << "         -smN , securityModel to use, only default N = 3 = USM possible\n";
        cout << "         -cnContextName, default empty string" << endl;
        cout << "         -ceContextEngineID, as hex e.g. 800007E580, default empty string" << endl;
        cout << "         -authPROT, use authentication protocol NONE, SHA or MD5\n";
        cout << "         -privPROT, use privacy protocol NONE, DES, 3DESEDE, IDEA, AES128, AES192 or AES256\n";
        cout << "         -uaAuthPassword\n";
        cout << "         -upPrivPassword\n";
#endif
        return 1;
    }

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

    //---------[ make a GenAddress and Oid object to retrieve ]---------------
    UdpAddress 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 1;
    }
    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 1;
            }
        }
    }

    //---------[ determine options to use ]-----------------------------------
    snmp_version version=version1;                  // default is v1
    u_short port=161;                               // default snmp port is 161
    OctetStr community("public");                   // community name
    Oid ent(ENTERPRISE);                            // default enterprise

#ifdef _SNMPv3
    OctetStr privPassword("");
    OctetStr authPassword("");
    OctetStr securityName("");
    int securityModel = SecurityModel_USM;
    int securityLevel = SecurityLevel_authPriv;
    OctetStr contextName("");
    OctetStr contextEngineID("");
    long authProtocol = SNMPv3_usmNoAuthProtocol;
    long privProtocol = SNMPv3_usmNoPrivProtocol;
    v3MP *v3_MP;
#endif

    char *ptr;

    for(int x=1; x<argc; x++) {                         // parse for version
        if ( strstr( argv[x],"-v2")!= 0) {
            version = version2c;
            continue;
        }
        if ( strstr( argv[x],"-C")!=0) {
            ptr = argv[x];
            ptr++;
            ptr++;
            community = ptr;
            continue;
        }
        if ( strstr( argv[x],"-P")!=0) {
            ptr = argv[x];
            ptr++;
            ptr++;
            sscanf(ptr, "%hu", &port);
            continue;
        }

#ifdef _SNMPv3
        if ( strstr( argv[x],"-v3")!= 0) {
            version = version3;
            continue;
        }
        if ( strstr( argv[x],"-auth") != 0) {
            ptr = argv[x];
            ptr+=5;
            if (strcasecmp(ptr, "SHA") == 0)
                authProtocol = SNMP_AUTHPROTOCOL_HMACSHA;
            else if (strcasecmp(ptr, "MD5") == 0)
                authProtocol = SNMP_AUTHPROTOCOL_HMACMD5;
            else
                authProtocol = SNMP_AUTHPROTOCOL_NONE;
            continue;
        }
        if ( strstr( argv[x],"-priv") != 0) {
            ptr = argv[x];
            ptr+=5;
            if (strcasecmp(ptr, "DES") == 0)
                privProtocol = SNMP_PRIVPROTOCOL_DES;
            else if (strcasecmp(ptr, "3DESEDE") == 0)
                privProtocol = SNMP_PRIVPROTOCOL_3DESEDE;
            else if (strcasecmp(ptr, "IDEA") == 0)
                privProtocol = SNMP_PRIVPROTOCOL_IDEA;
            else if (strcasecmp(ptr, "AES128") == 0)
                privProtocol = SNMP_PRIVPROTOCOL_AES128;
            else if (strcasecmp(ptr, "AES192") == 0)
                privProtocol = SNMP_PRIVPROTOCOL_AES192;
            else if (strcasecmp(ptr, "AES256") == 0)
                privProtocol = SNMP_PRIVPROTOCOL_AES256;
            else
                privProtocol = SNMP_PRIVPROTOCOL_NONE;
            printf("\n\nPrivProt : %ld\n", privProtocol);
            continue;
        }
        if ( strstr( argv[x],"-sn")!=0) {
            ptr = argv[x];
            ptr+=3;
            securityName = ptr;
            continue;
        }
        if ( strstr( argv[x], "-sl")!=0) {
            ptr = argv[x];
            ptr+=3;
            securityLevel = atoi( ptr);
            if (( securityLevel < SecurityLevel_noAuthNoPriv) ||
                    ( securityLevel > SecurityLevel_authPriv))
                securityLevel = SecurityLevel_authPriv;
            continue;
        }
        if ( strstr( argv[x], "-sm")!=0) {
            ptr = argv[x];
            ptr+=3;
            securityModel = atoi( ptr);
            if (( securityModel < SecurityModel_v1) ||
                    ( securityModel > SecurityModel_USM))
                securityModel = SecurityModel_USM;
            continue;
        }
        if ( strstr( argv[x],"-cn")!=0) {
            ptr = argv[x];
            ptr+=3;
            contextName = ptr;
            continue;
        }
        if ( strstr( argv[x],"-ce")!=0) {
            ptr = argv[x];
            ptr+=3;
            contextEngineID = OctetStr::from_hex_string(ptr);
            continue;
        }
        if ( strstr( argv[x],"-ua")!=0) {
            ptr = argv[x];
            ptr+=3;
            authPassword = ptr;
            continue;
        }
        if ( strstr( argv[x],"-up")!=0) {
            ptr = argv[x];
            ptr+=3;
            privPassword = ptr;
            continue;
        }
#endif
    }

    //----------[ create a SNMP++ session ]-----------------------------------
    int status;
    Snmp *snmp;

    if (address.get_ip_version() == Address::version_ipv4)
        snmp = new Snmp(status, "0.0.0.0");
    else
        snmp = new Snmp(status, "::");

    if ( status != SNMP_CLASS_SUCCESS) {
        cout << "SNMP++ Session Create Fail, " << snmp->error_msg(status) << "\n";
        return 1;
    }

    //---------[ init SnmpV3 ]--------------------------------------------
#ifdef _SNMPv3
    if (version == version3) {
        char *engineId = "TrapSender";
        char *filename = "snmpv3_boot_counter";
        unsigned int snmpEngineBoots = 0;
        int status;

        status = getBootCounter(filename, engineId, snmpEngineBoots);
        if ((status != SNMPv3_OK) && (status < SNMPv3_FILEOPEN_ERROR))
        {
            cout << "Error loading snmpEngineBoots counter: " << status << endl;
            return 1;
        }
        snmpEngineBoots++;
        status = saveBootCounter(filename, engineId, snmpEngineBoots);
        if (status != SNMPv3_OK)
        {
            cout << "Error saving snmpEngineBoots counter: " << status << endl;
            return 1;
        }

        int construct_status;
        v3_MP = new v3MP(engineId, snmpEngineBoots, construct_status);

        USM *usm = v3_MP->get_usm();
        usm->add_usm_user(securityName,
                          authProtocol, privProtocol,
                          authPassword, privPassword);
    }
    else
    {
        // MUST create a dummy v3MP object if _SNMPv3 is enabled!
        int construct_status;
        v3_MP = new v3MP("dummy", 0, construct_status);
    }
#endif

    //--------[ 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
    address.set_port(port);
    CTarget ctarget( address);             // make a target using the address
#ifdef _SNMPv3
    UTarget utarget( address);

    if (version == version3) {
        utarget.set_version( version);          // set the SNMP version SNMPV1 or V2 or V3
        utarget.set_security_model( securityModel);
        utarget.set_security_name( securityName);
        pdu.set_security_level( securityLevel);
        pdu.set_context_name (contextName);
        pdu.set_context_engine_id(contextEngineID);
    }
    else {
#endif
        ctarget.set_version( version);         // set the SNMP version SNMPV1 or V2
        ctarget.set_readcommunity( community); // set the read community name
#ifdef _SNMPv3
    }
#endif

    //-------[ Send the trap  ]------------------------------------------------
    cout << "SNMP++ Trap to " << argv[1] << " SNMPV"
#ifdef _SNMPv3
         << ((version==version3) ? (version) : (version+1));
#else
         << (version+1);
示例#10
0
int main_walkThreads_jun( int argc, char **argv)  {

  //---------[ check the arg count ]----------------------------------------
  if ( argc < 2) {
	  cout << "Usage:\n";
	  cout << "snmpWalkThreads host/port [host/port]... [options]\n";
	  cout << "StartOid: 1\n";
	  cout << "options: -v1 , use SNMPV1, default\n";
	  cout << "         -v2 , use SNMPV2\n";
#ifdef _SNMPv3
          cout << "         -v3 , use SNMPV3\n";
#endif
	  cout << "         -pPort , remote port to use\n";
	  cout << "         -CCommunity_name, specify community default is 'public' \n";
	  cout << "         -rN , retries default is N = 1 retry\n";
	  cout << "         -tN , timeout in hundredths of seconds; default is N = 100\n";
#ifdef _SNMPv3
          cout << "         -snSecurityName, " << endl;
          cout << "         -slN , securityLevel to use, default N = 3 = authPriv" << endl;
          cout << "         -smN , securityModel to use, only default N = 3 = USM possible\n";
          cout << "         -cnContextName, default """"" << endl;
          cout << "         -ceContextEngineID, default """"" << endl;
          cout << "         -md5 , use MD5 authentication protocol\n";
          cout << "         -sha , use SHA authentication protocol\n";
          cout << "         -des , use DES privacy protocol\n";
          cout << "         -idea, use IDEA privacy protocol\n";
          cout << "         -aes128, use AES128 privacy protocol\n";
          cout << "         -aes192, use AES192 privacy protocol\n";
          cout << "         -aes256, use AES256 privacy protocol\n";
          cout << "         -uaAuthPassword\n";
          cout << "         -upPrivPassword\n";
#endif
          return 0;
  }

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

  //---------[ make a GenAddress and Oid object to retrieve ]---------------
  address[0] = UdpAddress(argv[1]);
  if ( !address[0].valid()) {           // check validity of address
    cout << "Invalid Address or DNS Name, " << argv[1] << "\n";
    return -1;
  }
  int x=2;
  while ((x<argc) && (x<100) && (strstr(argv[x],"-")==0)) {
    address[x-1] = UdpAddress(argv[x]);
    if ( !address[x-1].valid()) {           // check validity of address
      cout << "Invalid Address or DNS Name, " << argv[x] << "\n";
      return -1;
    }     
    x++;
  }
  int threads = x-1;

  cout << community.get_printable() << endl;

   //---------[ determine options to use ]-----------------------------------
   char *ptr;
   for(;x<argc;x++) {                           // parse for version
     if ( strstr( argv[x],"-v2")!= 0) {
       version = version2c;
       continue;
     }
     if ( strstr( argv[x],"-r")!= 0) {                 // parse for retries
       ptr = argv[x]; ptr++; ptr++;
       retries = atoi(ptr);
       if (( retries<0)|| (retries>5)) retries=1; 
       continue;
     }
     if ( strstr( argv[x], "-t")!=0) {                 // parse for timeout
       ptr = argv[x]; ptr++; ptr++;
       timeout = atoi( ptr);
       if (( timeout < 100)||( timeout>500)) timeout=100;
       continue;
     }
     if ( strstr( argv[x],"-C")!=0) {
       ptr = argv[x]; ptr++; ptr++;
       community = ptr;
       continue;
     }
     if ( strstr( argv[x],"-p")!=0) {
       ptr = argv[x]; ptr++; ptr++;
       sscanf(ptr, "%hu", &port);
       continue;
     }

#ifdef _SNMPv3
     if ( strstr( argv[x],"-v3")!= 0) {
       version = version3;
       continue;
     }
     if ( strstr( argv[x],"-idea") != 0) {
       ptr = argv[x]; ptr++; ptr++;
       privProtocol = SNMPv3_usmIDEAPrivProtocol;
       continue;
     }
     if ( strstr( argv[x],"-aes128") != 0) {
       ptr = argv[x]; ptr++; ptr++;
       privProtocol = SNMPv3_usmAES128PrivProtocol;
       continue;
     }
     if ( strstr( argv[x],"-aes192") != 0) {
       ptr = argv[x]; ptr++; ptr++;
       privProtocol = SNMPv3_usmAES192PrivProtocol;
       continue;
     }
     if ( strstr( argv[x],"-aes256") != 0) {
       ptr = argv[x]; ptr++; ptr++;
       privProtocol = SNMPv3_usmAES256PrivProtocol;
       continue;
     }
     if ( strstr( argv[x],"-sha") != 0) {
       ptr = argv[x]; ptr++; ptr++;
       authProtocol = SNMPv3_usmHMACSHAAuthProtocol;
       continue;
     }
     if ( strstr( argv[x],"-des") != 0) {
       ptr = argv[x]; ptr++; ptr++;
       privProtocol = SNMPv3_usmDESPrivProtocol;
       continue;
     }
     if ( strstr( argv[x],"-md5") != 0) {
       ptr = argv[x]; ptr++; ptr++;
       authProtocol = SNMPv3_usmHMACMD5AuthProtocol;
       continue;
     }
     if ( strstr( argv[x],"-sn")!=0) {
       ptr = argv[x]; ptr+=3;
       securityName = ptr;
       continue;
      }
     if ( strstr( argv[x], "-sl")!=0) {
       ptr = argv[x]; ptr+=3;
       securityLevel = atoi( ptr);
       if (( securityLevel < SecurityLevel_noAuthNoPriv) ||
           ( securityLevel > SecurityLevel_authPriv))
         securityLevel = SecurityLevel_authPriv;
       continue;
     }
     if ( strstr( argv[x], "-sm")!=0) {
       ptr = argv[x]; ptr+=3;
       securityModel = atoi( ptr);
       if (( securityModel < SecurityModel_v1) ||
           ( securityModel > SecurityModel_USM))
         securityModel = SecurityModel_USM;
       continue;
     }
     if ( strstr( argv[x],"-cn")!=0) {
       ptr = argv[x]; ptr+=3;
       contextName = ptr;
       continue;
     }
     if ( strstr( argv[x],"-ce")!=0) {
       ptr = argv[x]; ptr+=3;
       contextEngineID = ptr;
       continue;
     }
     if ( strstr( argv[x],"-ua")!=0) {
       ptr = argv[x]; ptr+=3;
       authPassword = ptr;
       continue;
     }
     if ( strstr( argv[x],"-up")!=0) {
       ptr = argv[x]; ptr+=3;
       privPassword = ptr;
       continue;
     }
#endif
   }

   //----------[ create a SNMP++ session ]-----------------------------------
   int status;
   // bind to any port and use IPv6 if enabled
#ifdef SNMP_PP_IPv6
   snmp = new Snmp(status, 0, true);
#else
   snmp = new Snmp(status);
#endif

   if ( status != SNMP_CLASS_SUCCESS) {
     cout << "SNMP++ Session Create Fail, " 
	  << snmp->error_msg(status) << "\n";
     return -3;
   }

#ifdef _SNMPv3
   //---------[ init SnmpV3 ]--------------------------------------------
   v3MP *v3_MP;
   if (version == version3) {
     char *engineId = "snmpWalk";
     char *filename = "snmpv3_boot_counter";
     unsigned int snmpEngineBoots = 0;
     int status;

     status = getBootCounter(filename, engineId, snmpEngineBoots);
     if ((status != SNMPv3_OK) && (status < SNMPv3_FILEOPEN_ERROR))
     {
       cout << "Error loading snmpEngineBoots counter: " << status << endl;
       return 1;
     }
     snmpEngineBoots++;
     status = saveBootCounter(filename, engineId, snmpEngineBoots);
     if (status != SNMPv3_OK)
     {
       cout << "Error saving snmpEngineBoots counter: " << status << endl;
       return 1;
     }

     int construct_status;
     v3_MP = new v3MP(engineId, snmpEngineBoots, construct_status);

     USM *usm = v3_MP->get_usm();
     usm->add_usm_user(securityName,
		       authProtocol, privProtocol,
		       authPassword, privPassword);
   }
   else
   {
     // MUST create a dummy v3MP object if _SNMPv3 is enabled!
     int construct_status;
     v3_MP = new v3MP("dummy", 0, construct_status);
   }
#endif

#ifdef _THREADS
  pthread_t thread[100];
  int started = threads;
#endif

  while (threads) {
#ifdef _THREADS
    if (!attr) {
      attr = new pthread_attr_t;
      pthread_attr_init(attr);
      pthread_attr_setdetachstate(attr, PTHREAD_CREATE_JOINABLE);
    }
    pthread_create(&thread[threads-1], 0, 
		   &runable,
		   (void*)new int(threads-1));
#else
    int n = threads - 1;
    runable(&n);
#endif
    threads--;
  }
#ifdef _THREADS
  // wait for threads to terminate
  for (int i=0; i<started; i++) {
    cout << "JOINING THREAD " << i << endl;
    pthread_join(thread[i], 0);
  }
#endif
  cout << "END" << endl;
  Snmp::socket_cleanup();  // Shut down socket subsystem
}