snmpBulk.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*_############################################################################
  2. _##
  3. _## snmpBulk.cpp
  4. _##
  5. _## SNMP++v3.2.23
  6. _## -----------------------------------------------
  7. _## Copyright (c) 2001-2007 Jochen Katz, Frank Fock
  8. _##
  9. _## This software is based on SNMP++2.6 from Hewlett Packard:
  10. _##
  11. _## Copyright (c) 1996
  12. _## Hewlett-Packard Company
  13. _##
  14. _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
  15. _## Permission to use, copy, modify, distribute and/or sell this software
  16. _## and/or its documentation is hereby granted without fee. User agrees
  17. _## to display the above copyright notice and this license notice in all
  18. _## copies of the software and any documentation of the software. User
  19. _## agrees to assume all liability for the use of the software;
  20. _## Hewlett-Packard and Jochen Katz make no representations about the
  21. _## suitability of this software for any purpose. It is provided
  22. _## "AS-IS" without warranty of any kind, either express or implied. User
  23. _## hereby grants a royalty-free license to any and all derivatives based
  24. _## upon this software code base.
  25. _##
  26. _## Stuttgart, Germany, Sun Nov 11 15:10:59 CET 2007
  27. _##
  28. _##########################################################################*/
  29. /*
  30. snmpBulk.cpp
  31. Copyright (c) 1996
  32. Hewlett-Packard Company
  33. ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
  34. Permission to use, copy, modify, distribute and/or sell this software
  35. and/or its documentation is hereby granted without fee. User agrees
  36. to display the above copyright notice and this license notice in all
  37. copies of the software and any documentation of the software. User
  38. agrees to assume all liability for the use of the software; Hewlett-Packard
  39. makes no representations about the suitability of this software for any
  40. purpose. It is provided "AS-IS" without warranty of any kind,either express
  41. or implied. User hereby grants a royalty-free license to any and all
  42. derivatives based upon this software code base.
  43. Peter E. Mellquist
  44. */
  45. char snmpbulk_cpp_version[]="@(#) SNMP++ $Id: snmpBulk.cpp 289 2007-03-26 19:56:02Z katz $";
  46. #include "snmp_pp/snmp_pp.h"
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49. #ifdef WIN32
  50. #define strcasecmp stricmp
  51. #endif
  52. #ifdef SNMP_PP_NAMESPACE
  53. using namespace Snmp_pp;
  54. #endif
  55. #if (__GNUC__ > 2)
  56. #include <iostream>
  57. using std::cerr;
  58. using std::cout;
  59. using std::endl;
  60. using std::flush;
  61. #else
  62. #include <iostream.h>
  63. #endif
  64. int main(int argc, char **argv)
  65. {
  66. //---------[ check the arg count ]----------------------------------------
  67. if ( argc < 2) {
  68. cout << "Usage:\n";
  69. cout << "snmpBulk IpAddress | DNSName [Oid [Oid...]] [options]\n";
  70. cout << "Oid: sysDescr object is default\n";
  71. cout << "options: -vN , use SNMP version 1, 2 or 3, default is 2\n";
  72. cout << " -PPort , remote port to use\n";
  73. cout << " -CCommunity_name, specify community default is 'public' \n";
  74. cout << " -rN , retries default is N = 1 retry\n";
  75. cout << " -tN , timeout in hundredths of seconds; default is N = 100\n";
  76. cout << " -nN , non-repeaters default is N = 0\n";
  77. cout << " -mN , max-repetitions default is N = 10\n";
  78. #ifdef _SNMPv3
  79. cout << " -snSecurityName, " << endl;
  80. cout << " -slN , securityLevel to use, default N = 3 = authPriv" << endl;
  81. cout << " -smN , securityModel to use, only default N = 3 = USM possible\n";
  82. cout << " -cnContextName, default empty string" << endl;
  83. cout << " -ceContextEngineID, as hex e.g. 800007E580, default empty string" << endl;
  84. cout << " -authPROT, use authentication protocol NONE, SHA or MD5\n";
  85. cout << " -privPROT, use privacy protocol NONE, DES, 3DESEDE, IDEA, AES128, AES192 or AES256\n";
  86. cout << " -uaAuthPassword\n";
  87. cout << " -upPrivPassword\n";
  88. #endif
  89. return 1;
  90. }
  91. Snmp::socket_startup(); // Initialize socket subsystem
  92. //---------[ make a GenAddress and Oid object to retrieve ]---------------
  93. UdpAddress address( argv[1]); // make a SNMP++ Generic address
  94. if ( !address.valid()) { // check validity of address
  95. cout << "Invalid Address or DNS Name, " << argv[1] << "\n";
  96. return 1;
  97. }
  98. Pdu pdu; // construct a Pdu object
  99. Vb vb; // construct a Vb object
  100. if ( argc >= 3) { // if 3 args, then use the callers Oid
  101. int i=2;
  102. while ((i<argc) && (strstr(argv[i],"-")==0))
  103. {
  104. Oid oid(argv[i]);
  105. if ( !oid.valid()) { // check validity of user oid
  106. cout << "Invalid Oid, " << argv[2] << "\n";
  107. return -2;
  108. }
  109. vb.set_oid(oid);
  110. pdu += vb;
  111. i++;
  112. }
  113. }
  114. else {
  115. Oid oid("1.3.6.1.2.1.1.1"); // default is sysDescr
  116. vb.set_oid(oid);
  117. pdu += vb; // add the vb to the Pdu
  118. }
  119. //---------[ determine options to use ]-----------------------------------
  120. snmp_version version=version2c; // default is v2c
  121. int retries=1; // default retries is 1
  122. int timeout=100; // default is 1 second
  123. u_short port=161; // default snmp port is 161
  124. OctetStr community("public"); // community name
  125. int non_reps=0; // non repeaters default is 0
  126. int max_reps=10; // maximum repetitions default is 10
  127. #ifdef _SNMPv3
  128. OctetStr privPassword("");
  129. OctetStr authPassword("");
  130. OctetStr securityName("");
  131. int securityModel = SecurityModel_USM;
  132. int securityLevel = SecurityLevel_authPriv;
  133. OctetStr contextName("");
  134. OctetStr contextEngineID("");
  135. long authProtocol = SNMPv3_usmNoAuthProtocol;
  136. long privProtocol = SNMPv3_usmNoPrivProtocol;
  137. v3MP *v3_MP;
  138. #endif
  139. char *ptr;
  140. for(int x=1;x<argc;x++) { // parse for version
  141. if ( strstr( argv[x],"-v2")!= 0) {
  142. version = version2c;
  143. continue;
  144. }
  145. if ( strstr( argv[x],"-r")!= 0) { // parse for retries
  146. ptr = argv[x]; ptr++; ptr++;
  147. retries = atoi(ptr);
  148. if (( retries<0)|| (retries>5)) retries=1;
  149. continue;
  150. }
  151. if ( strstr( argv[x], "-t")!=0) { // parse for timeout
  152. ptr = argv[x]; ptr++; ptr++;
  153. timeout = atoi( ptr);
  154. if (( timeout < 100)||( timeout>500)) timeout=100;
  155. continue;
  156. }
  157. if ( strstr( argv[x],"-n")!=0) { // parse for non repeaters
  158. ptr = argv[x];ptr++;ptr++;
  159. non_reps=atoi( ptr);
  160. if (( non_reps < 0)||( non_reps>10)) non_reps=0;
  161. }
  162. if ( strstr( argv[x],"-m")!=0) { // parse for max repetitions
  163. ptr = argv[x];ptr++;ptr++;
  164. max_reps=atoi( ptr);
  165. if ( max_reps < 0) max_reps=1;
  166. }
  167. if ( strstr( argv[x],"-C")!=0) {
  168. ptr = argv[x]; ptr++; ptr++;
  169. community = ptr;
  170. continue;
  171. }
  172. if ( strstr( argv[x],"-P")!=0) {
  173. ptr = argv[x]; ptr++; ptr++;
  174. sscanf(ptr, "%hu", &port);
  175. continue;
  176. }
  177. #ifdef _SNMPv3
  178. if ( strstr( argv[x],"-v3")!= 0) {
  179. version = version3;
  180. continue;
  181. }
  182. if ( strstr( argv[x],"-auth") != 0) {
  183. ptr = argv[x]; ptr+=5;
  184. if (strcasecmp(ptr, "SHA") == 0)
  185. authProtocol = SNMP_AUTHPROTOCOL_HMACSHA;
  186. else if (strcasecmp(ptr, "MD5") == 0)
  187. authProtocol = SNMP_AUTHPROTOCOL_HMACMD5;
  188. else
  189. authProtocol = SNMP_AUTHPROTOCOL_NONE;
  190. continue;
  191. }
  192. if ( strstr( argv[x],"-priv") != 0) {
  193. ptr = argv[x]; ptr+=5;
  194. if (strcasecmp(ptr, "DES") == 0)
  195. privProtocol = SNMP_PRIVPROTOCOL_DES;
  196. else if (strcasecmp(ptr, "3DESEDE") == 0)
  197. privProtocol = SNMP_PRIVPROTOCOL_3DESEDE;
  198. else if (strcasecmp(ptr, "IDEA") == 0)
  199. privProtocol = SNMP_PRIVPROTOCOL_IDEA;
  200. else if (strcasecmp(ptr, "AES128") == 0)
  201. privProtocol = SNMP_PRIVPROTOCOL_AES128;
  202. else if (strcasecmp(ptr, "AES192") == 0)
  203. privProtocol = SNMP_PRIVPROTOCOL_AES192;
  204. else if (strcasecmp(ptr, "AES256") == 0)
  205. privProtocol = SNMP_PRIVPROTOCOL_AES256;
  206. else
  207. privProtocol = SNMP_PRIVPROTOCOL_NONE;
  208. printf("\n\nPrivProt : %ld\n", privProtocol);
  209. continue;
  210. }
  211. if ( strstr( argv[x],"-sn")!=0) {
  212. ptr = argv[x]; ptr+=3;
  213. securityName = ptr;
  214. continue;
  215. }
  216. if ( strstr( argv[x], "-sl")!=0) {
  217. ptr = argv[x]; ptr+=3;
  218. securityLevel = atoi( ptr);
  219. if (( securityLevel < SecurityLevel_noAuthNoPriv) ||
  220. ( securityLevel > SecurityLevel_authPriv))
  221. securityLevel = SecurityLevel_authPriv;
  222. continue;
  223. }
  224. if ( strstr( argv[x], "-sm")!=0) {
  225. ptr = argv[x]; ptr+=3;
  226. securityModel = atoi( ptr);
  227. if (( securityModel < SecurityModel_v1) ||
  228. ( securityModel > SecurityModel_USM))
  229. securityModel = SecurityModel_USM;
  230. continue;
  231. }
  232. if ( strstr( argv[x],"-cn")!=0) {
  233. ptr = argv[x]; ptr+=3;
  234. contextName = ptr;
  235. continue;
  236. }
  237. if ( strstr( argv[x],"-ce")!=0) {
  238. ptr = argv[x]; ptr+=3;
  239. contextEngineID = OctetStr::from_hex_string(ptr);
  240. continue;
  241. }
  242. if ( strstr( argv[x],"-ua")!=0) {
  243. ptr = argv[x]; ptr+=3;
  244. authPassword = ptr;
  245. continue;
  246. }
  247. if ( strstr( argv[x],"-up")!=0) {
  248. ptr = argv[x]; ptr+=3;
  249. privPassword = ptr;
  250. continue;
  251. }
  252. #endif
  253. }
  254. //----------[ create a SNMP++ session ]-----------------------------------
  255. int status;
  256. // bind to any port and use IPv6 if needed
  257. Snmp snmp(status, 0, (address.get_ip_version() == Address::version_ipv6));
  258. if ( status != SNMP_CLASS_SUCCESS) {
  259. cout << "SNMP++ Session Create Fail, " << snmp.error_msg(status) << "\n";
  260. return 1;
  261. }
  262. //---------[ init SnmpV3 ]--------------------------------------------
  263. #ifdef _SNMPv3
  264. if (version == version3) {
  265. char *engineId = "snmpBulk";
  266. char *filename = "snmpv3_boot_counter";
  267. unsigned int snmpEngineBoots = 0;
  268. int status;
  269. status = getBootCounter(filename, engineId, snmpEngineBoots);
  270. if ((status != SNMPv3_OK) && (status < SNMPv3_FILEOPEN_ERROR))
  271. {
  272. cout << "Error loading snmpEngineBoots counter: " << status << endl;
  273. return 1;
  274. }
  275. snmpEngineBoots++;
  276. status = saveBootCounter(filename, engineId, snmpEngineBoots);
  277. if (status != SNMPv3_OK)
  278. {
  279. cout << "Error saving snmpEngineBoots counter: " << status << endl;
  280. return 1;
  281. }
  282. int construct_status;
  283. v3_MP = new v3MP(engineId, snmpEngineBoots, construct_status);
  284. USM *usm = v3_MP->get_usm();
  285. usm->add_usm_user(securityName,
  286. authProtocol, privProtocol,
  287. authPassword, privPassword);
  288. }
  289. else
  290. {
  291. // MUST create a dummy v3MP object if _SNMPv3 is enabled!
  292. int construct_status;
  293. v3_MP = new v3MP("dummy", 0, construct_status);
  294. }
  295. #endif
  296. //--------[ build up SNMP++ object needed ]-------------------------------
  297. address.set_port(port);
  298. CTarget ctarget( address); // make a target using the address
  299. #ifdef _SNMPv3
  300. UTarget utarget( address);
  301. if (version == version3) {
  302. utarget.set_version( version); // set the SNMP version SNMPV1 or V2 or V3
  303. utarget.set_retry( retries); // set the number of auto retries
  304. utarget.set_timeout( timeout); // set timeout
  305. utarget.set_security_model( securityModel);
  306. utarget.set_security_name( securityName);
  307. pdu.set_security_level( securityLevel);
  308. pdu.set_context_name (contextName);
  309. pdu.set_context_engine_id(contextEngineID);
  310. }
  311. else {
  312. #endif
  313. ctarget.set_version( version); // set the SNMP version SNMPV1 or V2
  314. ctarget.set_retry( retries); // set the number of auto retries
  315. ctarget.set_timeout( timeout); // set timeout
  316. ctarget.set_readcommunity( community); // set the read community name
  317. #ifdef _SNMPv3
  318. }
  319. #endif
  320. //-------[ issue the request, blocked mode ]-----------------------------
  321. cout << "SNMP++ GetBulk to " << argv[1] << " SNMPV"
  322. #ifdef _SNMPv3
  323. << ((version==version3) ? (version) : (version+1))
  324. #else
  325. << (version+1)
  326. #endif
  327. << " Retries=" << retries
  328. << " Timeout=" << timeout * 10 << "ms"
  329. << " Non Reptrs=" << non_reps
  330. << " Max Reps=" << max_reps << endl;
  331. #ifdef _SNMPv3
  332. if (version == version3)
  333. cout << endl
  334. << "securityName= " << securityName.get_printable()
  335. << ", securityLevel= " << securityLevel
  336. << ", securityModel= " << securityModel << endl
  337. << "contextName= " << contextName.get_printable()
  338. << ", contextEngineID= " << contextEngineID.get_printable()
  339. << endl;
  340. else
  341. #endif
  342. cout << " Community=" << community.get_printable() << endl << flush;
  343. SnmpTarget *target;
  344. #ifdef _SNMPv3
  345. if (version == version3)
  346. target = &utarget;
  347. else
  348. #endif
  349. target = &ctarget;
  350. if (( status = snmp.get_bulk( pdu,*target,non_reps,max_reps))== SNMP_CLASS_SUCCESS) {
  351. for ( int z=0;z<pdu.get_vb_count();z++) {
  352. pdu.get_vb( vb,z);
  353. #ifdef _SNMPv3
  354. if (pdu.get_type() == REPORT_MSG) {
  355. Oid tmp;
  356. vb.get_oid(tmp);
  357. cout << "Received a reportPdu: "
  358. << snmp.error_msg( tmp)
  359. << endl
  360. << vb.get_printable_oid() << " = "
  361. << vb.get_printable_value() << endl;
  362. }
  363. #endif
  364. cout << "Oid = " << vb.get_printable_oid() << "\n";
  365. if ( vb.get_syntax() != sNMP_SYNTAX_ENDOFMIBVIEW) {
  366. cout << "Value = " << vb.get_printable_value() << "\n\n";
  367. }
  368. else {
  369. cout << "End of MIB view.\n\n";
  370. }
  371. }
  372. }
  373. else
  374. cout << "SNMP++ GetBulk Error, " << snmp.error_msg( status) << "\n";
  375. Snmp::socket_cleanup(); // Shut down socket subsystem
  376. }