2 //=============================================================================
6 * Sample application demonstrating synchronous Snmp::set API
7 * to update an oid in an SNMP Version 1 agent.
9 //=============================================================================
11 /*===================================================================
13 Hewlett-Packard Company
15 ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
16 Permission to use, copy, modify, distribute and/or sell this software
17 and/or its documentation is hereby granted without fee. User agrees
18 to display the above copyright notice and this license notice in all
19 copies of the software and any documentation of the software. User
20 agrees to assume all liability for the use of the software; Hewlett-Packard
21 makes no representations about the suitability of this software for any
22 purpose. It is provided "AS-IS without warranty of any kind,either express
23 or implied. User hereby grants a royalty-free license to any and all
24 derivatives based upon this software code base.
25 =====================================================================*/
27 #include "asnmp/snmp.h"
28 #include "ace/Argv_Type_Converter.h"
29 #include "ace/Get_Opt.h"
31 // FUZZ: disable check_for_streams_include
32 #include "ace/streams.h"
35 // SNMPv1 Set Application
39 set(int argc
, char **argv
); // process command line args
40 int valid() const; // verify transaction can proceed
41 int run(); // issue transaction
42 static void usage(); // operator help message
48 Pdu pdu_
; // construct a request Pdu
58 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
60 ACE_Argv_Type_Converter
atc (argc
, argv
);
61 set
get (atc
.get_argc (), atc
.get_ASCII_argv ());
75 set::set(int argc
, char *argv
[]): valid_(0)
77 Vb vb
; // construct a Vb object
81 target_
.get_write_community(community_
);
82 address_
= argv
[argc
- 1];
83 if ( !address_
.valid()) {
84 cout
<< "ERROR: Invalid IPv4 address or DNS hostname: " \
85 << argv
[argc
] << "\n";
89 ACE_Argv_Type_Converter
to_tchar (argc
, argv
);
90 ACE_Get_Opt
get_opt (argc
,
91 to_tchar
.get_TCHAR_argv (),
92 ACE_TEXT ("o:c:r:t:I:U:C:G:T:O:S:P:"));
93 for (int c
; (c
= get_opt ()) != -1; )
97 req
= ACE_TEXT_ALWAYS_CHAR (get_opt
.opt_arg());
99 cout
<< "ERROR: oid value: "
100 << ACE_TEXT_ALWAYS_CHAR (get_opt
.opt_arg())
101 << "is not valid. using default.\n";
105 community_
= ACE_TEXT_ALWAYS_CHAR (get_opt
.opt_arg());
106 target_
.set_write_community(community_
);
110 target_
.set_retry(ACE_OS::atoi (get_opt
.opt_arg()));
114 target_
.set_timeout(ACE_OS::atoi (get_opt
.opt_arg()));
117 case 'I': // Integer32
119 SnmpInt32
o(ACE_OS::atoi(get_opt
.opt_arg()));
125 case 'U': // Unsigned32
127 SnmpUInt32
o(ACE_OS::atoi(get_opt
.opt_arg()));
133 case 'C': // Counter32
135 Counter32
o(ACE_OS::atoi(get_opt
.opt_arg()));
143 Gauge32
o(ACE_OS::atoi(get_opt
.opt_arg()));
149 case 'T': // TimeTicks
151 TimeTicks
o(ACE_OS::atoi(get_opt
.opt_arg()));
157 case 'O': // Oid as a variable identifier
159 oid_
= ACE_TEXT_ALWAYS_CHAR (get_opt
.opt_arg());
160 vb
.set_oid(oid_
); // when value is set, pdu updated
164 case 'S': // Octet String
166 OctetStr
o(ACE_TEXT_ALWAYS_CHAR (get_opt
.opt_arg()));
167 vb
.set_value(o
); // set the Oid portion of the Vb
172 case 'P': // Oid String as a value
174 Oid
o(ACE_TEXT_ALWAYS_CHAR (get_opt
.opt_arg()));
175 vb
.set_value(o
); // set the Oid portion of the Vb
184 // if user didn't set anything use defaults
185 if (pdu_
.get_vb_count() == 0) {
186 Oid
def_oid("1.3.6.1.2.1.1.4.0"); // defualt is sysName
187 OctetStr
def_value("sysName.0 updated by ASNMP set command");
189 vb
.set_value(def_value
);
191 cout
<< "INFO: using defaults, setting sysName to : " << \
192 def_value
.to_string() << endl
;
201 cout
<< "next [options] dotted-quad | DNSName[:port]\n";
202 cout
<< " -o OID starts with oid after 1.3.6.1.2.1.1.1.0 (mibII sysDescr.0)\n";
203 cout
<< " -c Community_name, default is 'private'\n";
204 cout
<< " -r N retries default is N = 1 retry\n";
205 cout
<< " -t N timeout in seconds default is 1 second\n";
206 cout
<< " -O oid_to_set -{I,U,G,S,P} value\n";
207 cout
<< " where I=int32, U=uint32, G=gauge32, S=octet, P=oid" << endl
;
213 //----------[ create a ASNMP session ]-----------------------------------
214 if ( snmp_
.valid() != SNMP_CLASS_SUCCESS
) {
215 cout
<< "\nASNMP:ERROR:Create session failed: "<<
216 snmp_
.error_string()<< "\n";
220 //--------[ build up ASNMP object needed ]-------------------------------
221 if (address_
.get_port() == 0)
222 address_
.set_port(DEF_AGENT_PORT
);
223 target_
.set_address( address_
); // make a target using the address
225 //-------[ issue the request, blocked mode ]-----------------------------
226 cout
<< "\nASNMP:INFO:SNMP Version " << (target_
.get_version()+ 1) << \
227 " SET SAMPLE PROGRAM \nOID: " << oid_
.to_string() << "\n";
228 target_
.get_address(address_
); // target updates port used
230 const char *name
= address_
.resolve_hostname(rc
);
232 cout
<< "Device: " << address_
<< " ";
234 //FUZZ: disable check_for_lack_ACE_OS
235 cout
<< (rc
? "<< did not resolve via gethostbyname() >>" : name
) << "\n";
236 //FUZZ: enable check_for_lack_ACE_OS
238 cout
<< "[ Retries=" << target_
.get_retry() << " \
239 Timeout=" << target_
.get_timeout() <<" ms " << "Community=" << \
240 community_
.to_string() << " ]"<< endl
;
242 if (snmp_
.set( pdu_
, target_
) == SNMP_CLASS_SUCCESS
) {
244 // check to see if there are any errors
245 if (pdu_
.get_error_status()) {
246 cout
<< "ERROR: agent replied as follows\n";
247 cout
<< pdu_
.agent_error_reason() << endl
;
251 while (iter
.next(vb
)) {
252 cout
<< "\tOid = " << vb
.to_string_oid() << "\n";
253 cout
<< "\tValue = " << vb
.to_string_value() << "\n";
258 const char *ptr
= snmp_
.error_string();
259 cout
<< "ASNMP:ERROR: set command failed reason: " << ptr
<< endl
;
261 cout
<< "ASNMP:INFO:command completed normally.\n"<< endl
;