snmpWalk.cpp 14 KB

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