test_app.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*_############################################################################
  2. _##
  3. _## test_app.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 test_app_cpp_version[]="@(#) SNMP++ $Id: test_app.cpp 80 2004-05-28 18:47:24Z katz $";
  30. #include <stdlib.h> // For exit() function prototype
  31. #include "snmp_pp/snmp_pp.h"
  32. #ifdef SNMP_PP_NAMESPACE
  33. using namespace Snmp_pp;
  34. #endif
  35. #if (__GNUC__ > 2)
  36. #include <iostream>
  37. using std::cerr;
  38. using std::cout;
  39. using std::endl;
  40. using std::flush;
  41. #else
  42. #include <iostream.h>
  43. #endif
  44. // default request oids
  45. #define NUM_SYS_VBS 6
  46. #define sysDescr "1.3.6.1.2.1.1.1.0"
  47. #define sysObjectID "1.3.6.1.2.1.1.2.0"
  48. #define sysUpTime "1.3.6.1.2.1.1.3.0"
  49. #define sysContact "1.3.6.1.2.1.1.4.0"
  50. #define sysName "1.3.6.1.2.1.1.5.0"
  51. #define sysLocation "1.3.6.1.2.1.1.6.0"
  52. //#define sysServices "1.3.6.1.2.1.1.7.0" // not all agents support this...
  53. // default notification oid
  54. #define coldStart "1.3.6.1.6.3.1.1.4.3.0.1"
  55. int main(int argc, char **argv)
  56. {
  57. int status;
  58. char *req_str = (char*) "get";
  59. // char *dflt_req_oid = (char*) sysDescr;
  60. char *dflt_trp_oid = (char*) coldStart;
  61. char *genAddrStr = (char*) "127.0.0.1" ; // localhost
  62. char *oid_str = (char*) NULL;
  63. if (argc > 1) genAddrStr = argv[1];
  64. if (argc > 2) req_str = argv[2];
  65. if (argc > 3) oid_str = argv[3];
  66. Snmp::socket_startup(); // Initialize socket subsystem
  67. IpAddress ipAddr(genAddrStr);
  68. if (!ipAddr.valid()) {
  69. cout << "Invalid destination: " << genAddrStr << endl;
  70. return(1);
  71. }
  72. // bind to any port and use IPv6 if needed
  73. Snmp snmp(status, 0, (ipAddr.get_ip_version() == Address::version_ipv6));
  74. if (status){
  75. cout << "Failed to create SNMP Session: " << status << endl;
  76. return(1);
  77. }
  78. cout << "Created session successfully" << endl;
  79. CTarget target(ipAddr);
  80. if (! target.valid()) {
  81. cout << "Invalid target" << endl;
  82. return(1);
  83. }
  84. Pdu pdu;
  85. Vb vb;
  86. if ( strcmp(req_str, "get") == 0 ) {
  87. Vb vbl[NUM_SYS_VBS];
  88. vbl[0].set_oid(sysDescr);
  89. vbl[1].set_oid(sysObjectID);
  90. vbl[2].set_oid(sysUpTime);
  91. vbl[3].set_oid(sysContact);
  92. vbl[4].set_oid(sysName);
  93. vbl[5].set_oid(sysLocation);
  94. // vbl[6].set_oid(sysServices);
  95. cout << "Send a GET-REQUEST to: " << ipAddr.get_printable() << endl;
  96. if ( ! oid_str ) {
  97. if ( strcmp(genAddrStr,"localhost" ) == 0 ||
  98. strcmp(genAddrStr, "127.0.0.1") == 0 ){
  99. pdu.set_vblist(vbl, NUM_SYS_VBS);
  100. } else {
  101. for (int i=0; i<NUM_SYS_VBS;i++)
  102. pdu += vbl[i];
  103. }
  104. }
  105. else {
  106. Oid req_oid(oid_str);
  107. if ( ! req_oid.valid() ) {
  108. cout << "Request oid constructor failed for:" << oid_str << endl;
  109. return(1);
  110. }
  111. vb.set_oid(req_oid);
  112. pdu += vb;
  113. }
  114. status = snmp.get(pdu, target);
  115. if (status){
  116. cout << "Failed to issue SNMP Get: (" << status << ") "
  117. << snmp.error_msg(status) << endl;
  118. return(1);
  119. }
  120. else{
  121. cout << "Issued get successfully" << endl;
  122. int vbcount = pdu.get_vb_count();
  123. if ( vbcount == NUM_SYS_VBS ) {
  124. pdu.get_vblist(vbl, vbcount);
  125. for ( int i=0; i<vbcount ; i++ ) {
  126. cout << vbl[i].get_printable_oid() << " : " <<
  127. vbl[i].get_printable_value() << endl;
  128. }
  129. } else {
  130. for ( int i=0; i<vbcount ; i++ ) {
  131. pdu.get_vb(vb, i);
  132. cout << vb.get_printable_oid() << " : " <<
  133. vb.get_printable_value() << endl;
  134. }
  135. }
  136. }
  137. }
  138. else if ( strcmp(req_str, "trap") == 0 ) {
  139. cout << "Send a TRAP to: " << ipAddr.get_printable() << endl;
  140. if ( ! oid_str )
  141. oid_str = dflt_trp_oid;
  142. Oid notify_oid(oid_str);
  143. if ( ! notify_oid.valid() ) {
  144. cout << "Notify oid constructor failed for:" << oid_str << endl;
  145. return(1);
  146. }
  147. pdu.set_notify_id(notify_oid);
  148. // Use a simple payload
  149. vb.set_oid(sysLocation);
  150. vb.set_value("This is a test");
  151. pdu += vb;
  152. status = snmp.trap(pdu, target);
  153. if (status){
  154. cout << "Failed to issue SNMP Trap: (" << status << ") "
  155. << snmp.error_msg(status) << endl;
  156. return(1);
  157. } else {
  158. cout << "Success" << endl;
  159. }
  160. }
  161. else {
  162. cout << "Invalid SNMP operation: " << req_str << endl ;
  163. cout << "Usage: " << argv[0] << " hostname [get | trap]" << endl;
  164. return(1);
  165. }
  166. Snmp::socket_cleanup(); // Shut down socket subsystem
  167. return(0);
  168. }