snmpNext.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*_############################################################################
  2. _##
  3. _## snmpNext.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. snmpNext.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 snmpnext_cpp_version[]="@(#) SNMP++ $Id: snmpNext.cpp 264 2006-06-16 20:53:15Z fock $";
  46. #include "snmp_pp/snmp_pp.h"
  47. #include "snmp_pp/log.h"
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #ifdef WIN32
  51. #define strcasecmp stricmp
  52. #endif
  53. #ifdef SNMP_PP_NAMESPACE
  54. using namespace Snmp_pp;
  55. #endif
  56. #if (__GNUC__ > 2)
  57. #include <iostream>
  58. using std::cerr;
  59. using std::cout;
  60. using std::endl;
  61. using std::flush;
  62. #else
  63. #include <iostream.h>
  64. #endif
  65. int main(int argc, char **argv)
  66. {
  67. //---------[ check the arg count ]----------------------------------------
  68. if ( argc < 2) {
  69. cout << "Usage:\n";
  70. cout << argv[0] << " IpAddress | DNSName [Oid] [options]\n";
  71. cout << "Oid: sysDescr object is default\n";
  72. cout << "options: -vN , use SNMP version 1, 2 or 3, default is 1\n";
  73. cout << " -PPort , remote port to use\n";
  74. cout << " -CCommunity_name, specify community default is 'public' \n";
  75. cout << " -rN , retries default is N = 1 retry\n";
  76. cout << " -tN , timeout in hundredths of seconds; default is N = 100\n";
  77. #ifdef _SNMPv3
  78. cout << " -snSecurityName, " << endl;
  79. cout << " -slN , securityLevel to use, default N = 3 = authPriv" << endl;
  80. cout << " -smN , securityModel to use, only default N = 3 = USM possible\n";
  81. cout << " -cnContextName, default empty string" << endl;
  82. cout << " -ceContextEngineID, as hex e.g. 800007E580, default empty string" << endl;
  83. cout << " -authPROT, use authentication protocol NONE, SHA or MD5\n";
  84. cout << " -privPROT, use privacy protocol NONE, DES, 3DESEDE, IDEA, AES128, AES192 or AES256\n";
  85. cout << " -uaAuthPassword\n";
  86. cout << " -upPrivPassword\n";
  87. #endif
  88. return 1;
  89. }
  90. // Set filter for logging
  91. DefaultLog::log()->set_filter(ERROR_LOG, 7);
  92. DefaultLog::log()->set_filter(WARNING_LOG, 7);
  93. DefaultLog::log()->set_filter(EVENT_LOG, 7);
  94. DefaultLog::log()->set_filter(INFO_LOG, 7);
  95. DefaultLog::log()->set_filter(DEBUG_LOG, 7);
  96. Snmp::socket_startup(); // Initialize socket subsystem
  97. //---------[ make a GenAddress and Oid object to retrieve ]---------------
  98. UdpAddress address( argv[1]); // make a SNMP++ Generic address
  99. if ( !address.valid()) { // check validity of address
  100. cout << "Invalid Address or DNS Name, " << argv[1] << "\n";
  101. return 1;
  102. }
  103. Oid oid("1.3.6.1.2.1.1.1"); // default is sysDescr
  104. if ( argc >= 3) { // if 3 args, then use the callers Oid
  105. if ( strstr( argv[2],"-")==0) {
  106. oid = argv[2];
  107. if ( !oid.valid()) { // check validity of user oid
  108. cout << "Invalid Oid, " << argv[2] << "\n";
  109. return 1;
  110. }
  111. }
  112. }
  113. //---------[ determine options to use ]-----------------------------------
  114. snmp_version version=version1; // default is v1
  115. int retries=1; // default retries is 1
  116. int timeout=100; // default is 1 second
  117. u_short port=161; // default snmp port is 161
  118. OctetStr community("public"); // community name
  119. #ifdef _SNMPv3
  120. OctetStr privPassword("");
  121. OctetStr authPassword("");
  122. OctetStr securityName("");
  123. int securityModel = SecurityModel_USM;
  124. int securityLevel = SecurityLevel_authPriv;
  125. OctetStr contextName("");
  126. OctetStr contextEngineID("");
  127. long authProtocol = SNMPv3_usmNoAuthProtocol;
  128. long privProtocol = SNMPv3_usmNoPrivProtocol;
  129. v3MP *v3_MP;
  130. #endif
  131. char *ptr;
  132. for(int x=1;x<argc;x++) { // parse for version
  133. if ( strstr( argv[x],"-v2")!= 0) {
  134. version = version2c;
  135. continue;
  136. }
  137. if ( strstr( argv[x],"-r")!= 0) { // parse for retries
  138. ptr = argv[x]; ptr++; ptr++;
  139. retries = atoi(ptr);
  140. if (( retries<0)|| (retries>5)) retries=1;
  141. continue;
  142. }
  143. if ( strstr( argv[x], "-t")!=0) { // parse for timeout
  144. ptr = argv[x]; ptr++; ptr++;
  145. timeout = atoi( ptr);
  146. if (( timeout < 100)||( timeout>500)) timeout=100;
  147. continue;
  148. }
  149. if ( strstr( argv[x],"-C")!=0) {
  150. ptr = argv[x]; ptr++; ptr++;
  151. community = ptr;
  152. continue;
  153. }
  154. if ( strstr( argv[x],"-P")!=0) {
  155. ptr = argv[x]; ptr++; ptr++;
  156. sscanf(ptr, "%hu", &port);
  157. continue;
  158. }
  159. #ifdef _SNMPv3
  160. if ( strstr( argv[x],"-v3")!= 0) {
  161. version = version3;
  162. continue;
  163. }
  164. if ( strstr( argv[x],"-auth") != 0) {
  165. ptr = argv[x]; ptr+=5;
  166. if (strcasecmp(ptr, "SHA") == 0)
  167. authProtocol = SNMP_AUTHPROTOCOL_HMACSHA;
  168. else if (strcasecmp(ptr, "MD5") == 0)
  169. authProtocol = SNMP_AUTHPROTOCOL_HMACMD5;
  170. else
  171. authProtocol = SNMP_AUTHPROTOCOL_NONE;
  172. continue;
  173. }
  174. if ( strstr( argv[x],"-priv") != 0) {
  175. ptr = argv[x]; ptr+=5;
  176. if (strcasecmp(ptr, "DES") == 0)
  177. privProtocol = SNMP_PRIVPROTOCOL_DES;
  178. else if (strcasecmp(ptr, "3DESEDE") == 0)
  179. privProtocol = SNMP_PRIVPROTOCOL_3DESEDE;
  180. else if (strcasecmp(ptr, "IDEA") == 0)
  181. privProtocol = SNMP_PRIVPROTOCOL_IDEA;
  182. else if (strcasecmp(ptr, "AES128") == 0)
  183. privProtocol = SNMP_PRIVPROTOCOL_AES128;
  184. else if (strcasecmp(ptr, "AES192") == 0)
  185. privProtocol = SNMP_PRIVPROTOCOL_AES192;
  186. else if (strcasecmp(ptr, "AES256") == 0)
  187. privProtocol = SNMP_PRIVPROTOCOL_AES256;
  188. else
  189. privProtocol = SNMP_PRIVPROTOCOL_NONE;
  190. printf("\n\nPrivProt : %ld\n", privProtocol);
  191. continue;
  192. }
  193. if ( strstr( argv[x],"-sn")!=0) {
  194. ptr = argv[x]; ptr+=3;
  195. securityName = ptr;
  196. continue;
  197. }
  198. if ( strstr( argv[x], "-sl")!=0) {
  199. ptr = argv[x]; ptr+=3;
  200. securityLevel = atoi( ptr);
  201. if (( securityLevel < SecurityLevel_noAuthNoPriv) ||
  202. ( securityLevel > SecurityLevel_authPriv))
  203. securityLevel = SecurityLevel_authPriv;
  204. continue;
  205. }
  206. if ( strstr( argv[x], "-sm")!=0) {
  207. ptr = argv[x]; ptr+=3;
  208. securityModel = atoi( ptr);
  209. if (( securityModel < SecurityModel_v1) ||
  210. ( securityModel > SecurityModel_USM))
  211. securityModel = SecurityModel_USM;
  212. continue;
  213. }
  214. if ( strstr( argv[x],"-cn")!=0) {
  215. ptr = argv[x]; ptr+=3;
  216. contextName = ptr;
  217. continue;
  218. }
  219. if ( strstr( argv[x],"-ce")!=0) {
  220. ptr = argv[x]; ptr+=3;
  221. contextEngineID = OctetStr::from_hex_string(ptr);
  222. continue;
  223. }
  224. if ( strstr( argv[x],"-ua")!=0) {
  225. ptr = argv[x]; ptr+=3;
  226. authPassword = ptr;
  227. continue;
  228. }
  229. if ( strstr( argv[x],"-up")!=0) {
  230. ptr = argv[x]; ptr+=3;
  231. privPassword = ptr;
  232. continue;
  233. }
  234. #endif
  235. }
  236. //----------[ create a SNMP++ session ]-----------------------------------
  237. int status;
  238. // bind to any port and use IPv6 if needed
  239. Snmp snmp(status, 0, (address.get_ip_version() == Address::version_ipv6));
  240. if ( status != SNMP_CLASS_SUCCESS) {
  241. cout << "SNMP++ Session Create Fail, " << snmp.error_msg(status) << "\n";
  242. return 1;
  243. }
  244. //---------[ init SnmpV3 ]--------------------------------------------
  245. #ifdef _SNMPv3
  246. if (version == version3) {
  247. char *engineId = "snmpNext";
  248. char *filename = "snmpv3_boot_counter";
  249. unsigned int snmpEngineBoots = 0;
  250. int status;
  251. status = getBootCounter(filename, engineId, snmpEngineBoots);
  252. if ((status != SNMPv3_OK) && (status < SNMPv3_FILEOPEN_ERROR))
  253. {
  254. cout << "Error loading snmpEngineBoots counter: " << status << endl;
  255. return 1;
  256. }
  257. snmpEngineBoots++;
  258. status = saveBootCounter(filename, engineId, snmpEngineBoots);
  259. if (status != SNMPv3_OK)
  260. {
  261. cout << "Error saving snmpEngineBoots counter: " << status << endl;
  262. return 1;
  263. }
  264. int construct_status;
  265. v3_MP = new v3MP(engineId, snmpEngineBoots, construct_status);
  266. USM *usm = v3_MP->get_usm();
  267. usm->add_usm_user(securityName,
  268. authProtocol, privProtocol,
  269. authPassword, privPassword);
  270. }
  271. else
  272. {
  273. // MUST create a dummy v3MP object if _SNMPv3 is enabled!
  274. int construct_status;
  275. v3_MP = new v3MP("dummy", 0, construct_status);
  276. }
  277. #endif
  278. //--------[ build up SNMP++ object needed ]-------------------------------
  279. Pdu pdu; // construct a Pdu object
  280. Vb vb; // construct a Vb object
  281. vb.set_oid( oid); // set the Oid portion of the Vb
  282. pdu += vb; // add the vb to the Pdu
  283. address.set_port(port);
  284. CTarget ctarget( address); // make a target using the address
  285. #ifdef _SNMPv3
  286. UTarget utarget( address);
  287. if (version == version3) {
  288. utarget.set_version( version); // set the SNMP version SNMPV1 or V2 or V3
  289. utarget.set_retry( retries); // set the number of auto retries
  290. utarget.set_timeout( timeout); // set timeout
  291. utarget.set_security_model( securityModel);
  292. utarget.set_security_name( securityName);
  293. pdu.set_security_level( securityLevel);
  294. pdu.set_context_name (contextName);
  295. pdu.set_context_engine_id(contextEngineID);
  296. }
  297. else {
  298. #endif
  299. ctarget.set_version( version); // set the SNMP version SNMPV1 or V2
  300. ctarget.set_retry( retries); // set the number of auto retries
  301. ctarget.set_timeout( timeout); // set timeout
  302. ctarget.set_readcommunity( community); // set the read community name
  303. #ifdef _SNMPv3
  304. }
  305. #endif
  306. //-------[ issue the request, blocked mode ]-----------------------------
  307. cout << "SNMP++ GetNext to " << argv[1] << " SNMPV"
  308. #ifdef _SNMPv3
  309. << ((version==version3) ? (version) : (version+1))
  310. #else
  311. << (version+1)
  312. #endif
  313. << " Retries=" << retries
  314. << " Timeout=" << timeout * 10 <<"ms";
  315. #ifdef _SNMPv3
  316. if (version == version3)
  317. cout << endl
  318. << "securityName= " << securityName.get_printable()
  319. << ", securityLevel= " << securityLevel
  320. << ", securityModel= " << securityModel << endl
  321. << "contextName= " << contextName.get_printable()
  322. << ", contextEngineID= " << contextEngineID.get_printable()
  323. << endl;
  324. else
  325. #endif
  326. cout << " Community=" << community.get_printable() << endl << flush;
  327. SnmpTarget *target;
  328. #ifdef _SNMPv3
  329. if (version == version3)
  330. target = &utarget;
  331. else
  332. #endif
  333. target = &ctarget;
  334. status = snmp.get_next( pdu, *target);
  335. if (status == SNMP_CLASS_SUCCESS)
  336. {
  337. pdu.get_vb( vb,0);
  338. #ifdef _SNMPv3
  339. if (pdu.get_type() == REPORT_MSG) {
  340. cout << "Received a report pdu: "
  341. << snmp.error_msg(vb.get_printable_oid()) << endl;
  342. }
  343. #endif
  344. cout << "Oid = " << vb.get_printable_oid() << endl
  345. << "Value = " << vb.get_printable_value() << endl;
  346. }
  347. else
  348. {
  349. cout << "SNMP++ GetNext Error, " << snmp.error_msg( status)
  350. << " (" << status <<")" << endl;
  351. }
  352. Snmp::socket_cleanup(); // Shut down socket subsystem
  353. }