snmpDiscover.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*_############################################################################
  2. _##
  3. _## snmpDiscover.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. char snmpdiscover_cpp_version[]="@(#) SNMP++ $Id: snmpDiscover.cpp 264 2006-06-16 20:53:15Z fock $";
  30. #include "snmp_pp/snmp_pp.h"
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #ifdef SNMP_PP_NAMESPACE
  34. using namespace Snmp_pp;
  35. #endif
  36. #if (__GNUC__ > 2)
  37. #include <iostream>
  38. using std::cerr;
  39. using std::cout;
  40. using std::endl;
  41. using std::flush;
  42. #else
  43. #include <iostream.h>
  44. #endif
  45. int main(int argc, char **argv)
  46. {
  47. //---------[ check the arg count ]----------------------------------------
  48. if ( argc < 2) {
  49. cout << "Usage:\n";
  50. cout << argv[0] << " BroadcastIpAddress [options]\n";
  51. cout << "options: -vN , use SNMP version 1, 2 or 3, default is 1\n";
  52. cout << " -PPort , remote port to use\n";
  53. cout << " -CCommunity_name, specify community default is 'public' \n";
  54. cout << " -rN , retries default is N = 1 retry\n";
  55. cout << " -tN , timeout in hundredths of seconds; default is N = 100\n";
  56. return 1;
  57. }
  58. Snmp::socket_startup(); // Initialize socket subsystem
  59. //---------[ make a GenAddress and Oid object to retrieve ]---------------
  60. UdpAddress address( argv[1]); // make a SNMP++ Generic address
  61. if ( !address.valid()) { // check validity of address
  62. cout << "Invalid Address or DNS Name, " << argv[1] << "\n";
  63. return 1;
  64. }
  65. Oid oid("1.3.6.1.2.1.1.1.0"); // default is sysDescr
  66. //---------[ determine options to use ]-----------------------------------
  67. snmp_version version=version1; // default is v1
  68. int retries=1; // default retries is 1
  69. int timeout=100; // default is 1 second
  70. u_short port=161; // default snmp port is 161
  71. OctetStr community("public"); // community name
  72. #ifdef _SNMPv3
  73. v3MP *v3_MP;
  74. #endif
  75. char *ptr;
  76. for(int x=1;x<argc;x++) { // parse for version
  77. if ( strstr( argv[x],"-v2")!= 0) {
  78. version = version2c;
  79. continue;
  80. }
  81. if ( strstr( argv[x],"-r")!= 0) { // parse for retries
  82. ptr = argv[x]; ptr++; ptr++;
  83. retries = atoi(ptr);
  84. if (( retries<0)|| (retries>5)) retries=1;
  85. continue;
  86. }
  87. if ( strstr( argv[x], "-t")!=0) { // parse for timeout
  88. ptr = argv[x]; ptr++; ptr++;
  89. timeout = atoi( ptr);
  90. if (( timeout < 100)||( timeout>500)) timeout=100;
  91. continue;
  92. }
  93. if ( strstr( argv[x],"-C")!=0) {
  94. ptr = argv[x]; ptr++; ptr++;
  95. community = ptr;
  96. continue;
  97. }
  98. if ( strstr( argv[x],"-P")!=0) {
  99. ptr = argv[x]; ptr++; ptr++;
  100. sscanf(ptr, "%hu", &port);
  101. continue;
  102. }
  103. #ifdef _SNMPv3
  104. if ( strstr( argv[x],"-v3")!= 0) {
  105. version = version3;
  106. continue;
  107. }
  108. #endif
  109. }
  110. //----------[ create a SNMP++ session ]-----------------------------------
  111. int status;
  112. // bind to any port and use IPv6 if needed
  113. Snmp snmp(status, 0, (address.get_ip_version() == Address::version_ipv6));
  114. if ( status != SNMP_CLASS_SUCCESS) {
  115. cout << "SNMP++ Session Create Fail, " << snmp.error_msg(status) << "\n";
  116. return 1;
  117. }
  118. //---------[ init SnmpV3 ]--------------------------------------------
  119. #ifdef _SNMPv3
  120. // MUST create a dummy v3MP object if _SNMPv3 is enabled!
  121. int construct_status;
  122. v3_MP = new v3MP("dummy", 0, construct_status);
  123. #endif
  124. //--------[ build up SNMP++ object needed ]-------------------------------
  125. address.set_port(port);
  126. //-------[ issue the broadcast, blocked mode ]----------------------------
  127. cout << "SNMP++ broadcast to " << argv[1] << " SNMPV"
  128. #ifdef _SNMPv3
  129. << ((version==version3) ? (version) : (version+1))
  130. #else
  131. << (version+1)
  132. #endif
  133. << " Retries=" << retries
  134. << " Timeout=" << timeout * 10 <<"ms";
  135. #ifdef _SNMPv3
  136. if (version == version3)
  137. cout << endl;
  138. else
  139. #endif
  140. cout << " Community=" << community.get_printable() << endl << flush;
  141. UdpAddressCollection addresses;
  142. // do multiple loops as requested from "retries"
  143. for (int loops=1; loops<= retries + 1; ++loops)
  144. {
  145. status = snmp.broadcast_discovery(addresses,
  146. (timeout + 99) / 100,
  147. address, version, &community);
  148. if (status == SNMP_CLASS_SUCCESS)
  149. {
  150. cout << "SNMP++ Success sending broadcast " << loops << "." << endl;
  151. }
  152. else
  153. {
  154. cout << "SNMP++ Broadcast Error, " << snmp.error_msg( status)
  155. << " (" << status <<")" << endl;
  156. }
  157. }
  158. // filter out duplicates
  159. UdpAddressCollection filtered_addrs;
  160. int dummy_pos;
  161. for (int n=0; n < addresses.size(); ++n)
  162. if (filtered_addrs.find(addresses[n], dummy_pos) == FALSE)
  163. filtered_addrs += addresses[n];
  164. // print out all addressess
  165. cout << "Found " << filtered_addrs.size() << " agents." << endl;
  166. for (int m=0; m < filtered_addrs.size(); ++m)
  167. cout << "Answer received from: " << filtered_addrs[m].get_printable()
  168. << endl;
  169. Snmp::socket_cleanup(); // Shut down socket subsystem
  170. }