snmpSet.cpp
资源名称:hp_snmp3.zip [点击查看]
上传用户:czjinwang
上传日期:2007-01-12
资源大小:2484k
文件大小:8k
源码类别:
SNMP编程
开发平台:
Visual C++
- /*
- snmpSet.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>
- // detrmine the smi type and get a value from
- // the user
- int determine_vb( SmiUINT32 val, Vb &vb) {
- char buffer[255];
- cout << "Value Type is ";
- switch (val) {
- // octet string
- case sNMP_SYNTAX_OCTETS:
- {
- cout << "Octet Stringn";
- cout << "Value ?";
- cin >> buffer;
- OctetStr octetstr( buffer);
- if ( octetstr.valid()) {
- vb.set_value( octetstr);
- return TRUE;
- }
- else {
- cout << "Invalid OctetStrn";
- return FALSE;
- }
- }
- break;
- // IP Address
- case sNMP_SYNTAX_IPADDR:
- {
- cout << "IP Addressn";
- cout << "Value ?";
- cin >> buffer;
- IpAddress ipaddress( buffer);
- if ( ipaddress.valid()) {
- vb.set_value( ipaddress);
- return TRUE;
- }
- else {
- cout << "Invalid IP Addressn";
- return FALSE;
- }
- }
- break;
- // Oid
- case sNMP_SYNTAX_OID:
- {
- cout << "Oidn";
- cout << "Value ?";
- cin >> buffer;
- Oid oid( buffer);
- if ( oid.valid()) {
- vb.set_value( oid);
- return TRUE;
- }
- else {
- cout << "Invalid Oidn";
- return FALSE;
- }
- }
- break;
- // TimeTicks
- case sNMP_SYNTAX_TIMETICKS:
- {
- cout << "TimeTicksn";
- cout << "Value ?";
- cin >> buffer;
- unsigned long i;
- i = atol( buffer);
- TimeTicks timeticks( i);
- if ( timeticks.valid()) {
- vb.set_value( timeticks);
- return TRUE;
- }
- else {
- cout << "Invalid TimeTicksn";
- return FALSE;
- }
- }
- break;
- // Gauge32
- case sNMP_SYNTAX_GAUGE32:
- {
- cout << "Gauge32n";
- cout << "Value ?";
- cin >> buffer;
- unsigned long i;
- i = atol( buffer);
- Gauge32 gauge32(i);
- if ( gauge32.valid()) {
- vb.set_value( gauge32);
- return TRUE;
- }
- else {
- cout << "Invalid Gauge32n";
- return FALSE;
- }
- }
- break;
- case sNMP_SYNTAX_CNTR32:
- {
- cout << "Counter32n";
- cout << "Value ?";
- cin >> buffer;
- unsigned long i;
- i = atol( buffer);
- Counter32 counter32(i);
- if ( counter32.valid()) {
- vb.set_value( counter32);
- return TRUE;
- }
- else {
- cout << "Invalid Counter32n";
- return FALSE;
- }
- }
- break;
- case sNMP_SYNTAX_INT:
- {
- cout << "Integern";
- cout << "Value ?";
- cin >> buffer;
- unsigned long i;
- i = atol( buffer);
- long l ;
- l = ( long) i;
- vb.set_value( l);
- return TRUE;
- }
- break;
- case sNMP_SYNTAX_UINT32:
- {
- cout << "Integern";
- cout << "Value ?";
- cin >> buffer;
- unsigned long i;
- i = atol( buffer);
- vb.set_value( i);
- return TRUE;
- }
- break;
- default:
- cout << "Unknown Data Typen";
- return FALSE;
- }
- }
- int main( int argc, char **argv) {
- //---------[ check the arg count ]----------------------------------------
- if ( argc < 2) {
- cout << "Usage:n";
- cout << "snmpSet Address | DNSName [Oid] [options]n";
- cout << "Oid: sysDescr object is defaultn";
- cout << "options: -v1 , use SNMPV1, defaultn";
- cout << " -v2 , use SNMPV2n";
- cout << " -cCommunity_name, specify community default is 'public' n";
- cout << " -rN , retries default is N = 1 retryn";
- cout << " -tN , timeout in hundredths-seconds default is N = 100 = 1 secondn";
- 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("1.3.6.1.2.1.1.4.0"); // defualt is sysName
- 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
- int retries=1; // default retries is 1
- int timeout=100; // default is 1 second
- OctetStr community("public"); // community name
- char *ptr;
- for(int x=1;x<argc;x++) { // parse for version
- if ( strstr( argv[x],"-v2")!= 0)
- version = version2c;
- if ( strstr( argv[x],"-r")!= 0) { // parse for retries
- ptr = argv[x]; ptr++; ptr++;
- retries = atoi(ptr);
- if (( retries<1)|| (retries>5)) retries=1;
- }
- if ( strstr( argv[x], "-t")!=0) { // parse for timeout
- ptr = argv[x]; ptr++; ptr++;
- timeout = atoi( ptr);
- if (( timeout < 100)||( timeout>500)) timeout=100;
- }
- 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; // construct a Vb object
- vb.set_oid( oid); // set the Oid portion of the Vb
- pdu += vb; // add the vb to the Pdu
- CTarget target( address); // make a target using the address
- target.set_version( version); // set the SNMP version SNMPV1 or V2
- target.set_retry( retries); // set the number of auto retries
- target.set_timeout( timeout); // set timeout
- target.set_readcommunity( community); // set read community
- //-------[ issue the request, blocked mode ]-----------------------------
- cout << "SNMP++ Set to " << argv[1] << " SNMPV" << (version+1) << " Retries=" << retries;
- cout << " Timeout=" << timeout <<"msn";
- // first get the variabel to determine its type
- if (( status = snmp.get( pdu,target))== SNMP_CLASS_SUCCESS) {
- pdu.get_vb( vb,0);
- cout << "Oid = " << vb.get_printable_oid() << "n";
- cout << "Current Value = " << vb.get_printable_value() << "n";
- if ( determine_vb(vb.get_syntax(), vb)) {
- // do the Set
- Pdu setpdu;
- vb.set_oid( oid); // use the same oid as the inquire
- setpdu += vb;
- status = snmp.set( setpdu, target);
- cout << "Set Status = ";
- if ( status == SNMP_CLASS_ERR_STATUS_SET)
- status = setpdu.get_error_status();
- cout << snmp.error_msg( status) << "n";
- }
- }
- else {
- if ( status == SNMP_CLASS_ERR_STATUS_SET)
- status = pdu.get_error_status();
- cout << "SNMP++ Set Error, " << snmp.error_msg( status) << "n";
- }
- return 0;
- } // end snmpSet