📄 snmpset.cpp
字号:
/*_############################################################################ _## _## snmpSet.cpp _## _## SNMP++v3.2.21a _## ----------------------------------------------- _## Copyright (c) 2001-2006 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, Tue Nov 21 22:12:16 CET 2006 _## _##########################################################################*//* snmpSet.cpp 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*/char snmpset_cpp_version[]="@(#) SNMP++ $Id: snmpSet.cpp,v 1.9 2006/06/16 20:53:15 fock Exp $";#include "snmp_pp/snmp_pp.h"#include <stdlib.h>#include <stdio.h>#ifdef WIN32#define strcasecmp stricmp#endif#ifdef SNMP_PP_NAMESPACEusing namespace Snmp_pp;#endif#if (__GNUC__ > 2)#include <iostream>using std::cerr;using std::cout;using std::cin;using std::endl;using std::flush;#else#include <iostream.h>#endif// determine the smi type and get a value from the userint determine_vb( SmiUINT32 val, Vb &vb){ char buffer[255]; if (val == sNMP_SYNTAX_NOSUCHINSTANCE) { cout << "Instance does not exists but can be created.\n"; cout << "Please enter one of the following types:\n\n"; cout << "Integer: " << sNMP_SYNTAX_INT << "\n"; cout << "Bits: " << sNMP_SYNTAX_BITS << "\n"; cout << "STRING: " << sNMP_SYNTAX_OCTETS << "\n"; cout << "Oid: " << sNMP_SYNTAX_OID << "\n"; cout << "IpAddress:" << sNMP_SYNTAX_IPADDR << "\n\n"; cout << "Please choose value type: "; cin >> val; vb.set_syntax(val); } cout << "Value Type is "; switch (val) { // octet string case sNMP_SYNTAX_OCTETS: { cout << "Octet String\n"; cout << "Please enter new value: "; cin >> buffer; OctetStr octetstr( buffer); if ( octetstr.valid()) { vb.set_value( octetstr); return TRUE; } else { cout << "Invalid OctetStr\n"; return FALSE; } } // IP Address case sNMP_SYNTAX_IPADDR: { cout << "IP Address\n"; cout << "Please enter new value: "; cin >> buffer; IpAddress ipaddress( buffer); if ( ipaddress.valid()) { vb.set_value( ipaddress); return TRUE; } else { cout << "Invalid IP Address\n"; return FALSE; } } // Oid case sNMP_SYNTAX_OID: { cout << "Oid\n"; cout << "Please enter new value: "; cin >> buffer; Oid oid( buffer); if ( oid.valid()) { vb.set_value( oid); return TRUE; } else { cout << "Invalid Oid\n"; return FALSE; } } // TimeTicks case sNMP_SYNTAX_TIMETICKS: { cout << "TimeTicks\n"; cout << "Please enter new value: "; cin >> buffer; unsigned long i; i = atol( buffer); TimeTicks timeticks( i); if ( timeticks.valid()) { vb.set_value( timeticks); return TRUE; } else { cout << "Invalid TimeTicks\n"; return FALSE; } } // Gauge32 case sNMP_SYNTAX_GAUGE32: { cout << "Gauge32\n"; cout << "Please enter new value: "; cin >> buffer; unsigned long i; i = atol( buffer); Gauge32 gauge32(i); if ( gauge32.valid()) { vb.set_value( gauge32); return TRUE; } else { cout << "Invalid Gauge32\n"; return FALSE; } } case sNMP_SYNTAX_CNTR32: { cout << "Counter32\n"; cout << "Please enter new value: "; cin >> buffer; unsigned long i; i = atol( buffer); Counter32 counter32(i); if ( counter32.valid()) { vb.set_value( counter32); return TRUE; } else { cout << "Invalid Counter32\n"; return FALSE; } } case sNMP_SYNTAX_CNTR64: { cout << "Counter64\n"; cout << "Please enter value (low 32 bit): "; cin >> buffer; unsigned long i; i = atol( buffer); Counter64 counter64; counter64.set_low(i); cout << "Please enter value (high 32 bit): "; cin >> buffer; i = atol( buffer); counter64.set_high(i); if ( counter64.valid()) { vb.set_value( counter64); return TRUE; } else { cout << "Invalid Counter64\n"; return FALSE; } } case sNMP_SYNTAX_INT: { cout << "Integer\n"; cout << "Please enter new value: "; cin >> buffer; unsigned long i; i = atol( buffer); long l ; l = ( long) i; vb.set_value( l); return TRUE; } case sNMP_SYNTAX_NOSUCHOBJECT: { cout << "NO SUCH OBJECT\n"; cout << "Object cannot be created.\n"; return FALSE; } default: cout << "Unknown Data Type " << val << "\n"; return FALSE; }}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 SET community default is 'public' \n"; cout << " -GCommunity_name, specify GET community default is set community value \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.4.0"); // default is sysName if ( argc >= 3) { // if 3 args, then use the callers Oid if ( strstr( argv[2],"-")==0) { oid = argv[2];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -