+ -
当前位置:首页 → 问答吧 → 请教个问题.scalar_int.c 获取值的问题.

请教个问题.scalar_int.c 获取值的问题.

时间:2010-09-03

来源:互联网

本帖最后由 unix_pro 于 2010-09-03 11:07 编辑

小弟纯粹新手,各位多多指教.

我想让 test这个函数能够 得到 example1这个值.

example1这个值不是 已经定义好的,而是 snmp set过的值.

http://linux.die.net/man/3/netsnmp_instance 这里有    netsnmp_register_int_instance 定义.
  1. /**  @example scalar_int.c
  2. *  This example creates some scalar registrations that allows
  3. *  some simple variables to be accessed via SNMP.  In a more
  4. *  realistic example, it is likely that these variables would also be
  5. *  manipulated in other ways outside of SNMP gets/sets.
  6. *
  7. *  If this module is compiled into an agent, you should be able to
  8. *  issue snmp commands that look something like (authentication
  9. *  information not shown in these commands):
  10. *
  11. *  - snmpget localhost netSnmpExampleInteger.0
  12. *  - netSnmpExampleScalars = 42
  13. *
  14. *  - snmpset localhost netSnmpExampleInteger.0 = 1234
  15. *  - netSnmpExampleScalars = 1234
  16. *  
  17. *  - snmpget localhost netSnmpExampleInteger.0
  18. *  - netSnmpExampleScalars = 1234
  19. *
  20. */

  21. /*
  22. * start be including the appropriate header files
  23. */

  24. #include <net-snmp/net-snmp-config.h>
  25. #include <net-snmp/net-snmp-includes.h>
  26. #include <net-snmp/agent/net-snmp-agent-includes.h>

  27. /*
  28. * Then, we declare the variables we want to be accessed
  29. */
  30. static int      example1 = 42;  /* default value */
  31. //int test(int);
  32. void  test (int);
  33. /*
  34. * our initialization routine, automatically called by the agent
  35. * (to get called, the function name must match init_FILENAME())
  36. */
  37. void
  38. init_scalar_int(void)
  39. {
  40.     /*
  41.      * the OID we want to register our integer at.  This should be a
  42.      * fully qualified instance.  In our case, it's a scalar at:
  43.      * NET-SNMP-EXAMPLES-MIB::netSnmpExampleInteger.0 (note the
  44.      * trailing 0 which is required for any instantiation of any
  45.      * scalar object)
  46.      */
  47.     oid             my_registration_oid[] =
  48.         { 1, 3, 6, 1, 4, 1, 17711, 2, 1, 1, 0 };

  49.     /*
  50.      * a debugging statement.  Run the agent with -Dexample_scalar_int to see
  51.      * the output of this debugging statement.
  52.      */
  53.     DEBUGMSGTL(("example_scalar_int",
  54.                 "Initalizing example scalar int.  Default value = %d\n",
  55.                 example1));

  56.     /*
  57.      * the line below registers our "example1" variable above as
  58.      * accessible and makes it writable.  A read only version of the
  59.      * same registration would merely call
  60.      * register_read_only_int_instance() instead.
  61.      *
  62.      * If we wanted a callback when the value was retrieved or set
  63.      * (even though the details of doing this are handled for you),
  64.      * you could change the NULL pointer below to a valid handler
  65.      * function.
  66.      */
  67.     netsnmp_register_int_instance("my example int variable",
  68.                                   my_registration_oid,
  69.                                   OID_LENGTH(my_registration_oid),
  70.                                   &example1, test(example1)); //调用其他执行函数

  71.     DEBUGMSGTL(("example_scalar_int",
  72.                 "Done initalizing example scalar int\n"));


  73. }


  74. void test (int example1){
  75.         static char tmp[50];
  76.         sprintf(tmp,"iwconfig ath1 essid eth%d",&example1);
  77.         system(tmp);
  78. }

  79. /*
  80. int test        (void){
  81.         static char                                tmp[50];
  82.         int                  essidvar = &example1;
  83.         sprintf(tmp,"iwconfig ath1 essid eth%d",essidvar);
  84.         system(tmp);
  85. }
  86. */
  87. //int test (void)
  88. //{
  89. //        system("echo b > /var/sys.conf");
  90.         //return EXIT_SUCCESS;
  91. //}
复制代码

作者: unix_pro   发布时间: 2010-09-03

  1. int
  2. netsnmp_register_int_instance(const char *name,
  3.                               oid * reg_oid, size_t reg_oid_len,
  4.                               int *it, Netsnmp_Node_Handler * subhandler)
  5. {
  6.     netsnmp_handler_registration *myreg;

  7.     myreg = get_reg(name, "int_handler", reg_oid, reg_oid_len, it,
  8.                     HANDLER_CAN_RWRITE, netsnmp_instance_int_handler,
  9.                     subhandler, NULL);
  10.     return netsnmp_register_instance(myreg);
  11. }
复制代码

作者: unix_pro   发布时间: 2010-09-03

相关阅读 更多