免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 14305 | 回复: 34
打印 上一主题 下一主题

陷入困境了,请大家帮忙啊(snmp问题,google没找到解决办法) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-01-10 17:15 |只看该作者 |倒序浏览
这样的,我在机器上安装了net-snmp的最新版本,5.2,然后用它提供的APIs来编程,写一些管理进程的应用。但是处理一个最简单的例子时,遇到了一个困难,下面是net-snmp本身提供的例子的源代码:
  1. #include <net-snmp/net-snmp-config.h>;
  2. #include <net-snmp/net-snmp-includes.h>;
  3. #include <string.h>;
  4. #include <stdlib.h>;
  5. #include <stdio.h>;

  6. /* change the word "define" to "undef" to try the (insecure) SNMPv1 version */
  7. #define DEMO_USE_SNMP_VERSION_3

  8. #ifdef DEMO_USE_SNMP_VERSION_3
  9. const char *our_v3_passphrase = "The UCD Demo Password";
  10. #endif

  11. int
  12. main (int argc, char **argv)
  13. {
  14.   struct snmp_session session, *ss;
  15.   struct snmp_pdu *pdu;
  16.   struct snmp_pdu *response;

  17.   oid anOID[MAX_OID_LEN];
  18.   size_t anOID_len = MAX_OID_LEN;

  19.   struct variable_list *vars;
  20.   int status;
  21.   int count = 1;
  22.   /*
  23.    * Initialize the SNMP library
  24.    */
  25.   init_snmp ("snmpapp");

  26.   /*
  27.    * Initialize a "session" that defines who we're going to talk to
  28.    */
  29.   snmp_sess_init (&session);    /* set up defaults */
  30.   session.peername = strdup ("test.net-snmp.org");

  31.   /* set up the authentication parameters for talking to the server */

  32. #ifdef DEMO_USE_SNMP_VERSION_3

  33.   /* Use SNMPv3 to talk to the experimental server */

  34.   /* set the SNMP version number */
  35.   session.version = SNMP_VERSION_3;

  36.   /* set the SNMPv3 user name */
  37.   session.securityName = strdup ("MD5User");
  38.   session.securityNameLen = strlen (session.securityName);

  39.   /* set the security level to authenticated, but not encrypted */
  40.   session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;

  41.   /* set the authentication method to MD5 */
  42.   session.securityAuthProto = usmHMACMD5AuthProtocol;
  43.   session.securityAuthProtoLen =
  44.     sizeof (usmHMACMD5AuthProtocol) / sizeof (oid);
  45.   session.securityAuthKeyLen = USM_AUTH_KU_LEN;

  46.   /* set the authentication key to a MD5 hashed version of our
  47.      passphrase "The UCD Demo Password" (which must be at least 8
  48.      characters long) */
  49.   if (generate_Ku (session.securityAuthProto,
  50.                    session.securityAuthProtoLen,
  51.                    (u_char *) our_v3_passphrase, strlen (our_v3_passphrase),
  52.                    session.securityAuthKey,
  53.                    &session.securityAuthKeyLen) != SNMPERR_SUCCESS)
  54.     {
  55.       snmp_perror (argv[0]);
  56.       snmp_log (LOG_ERR,
  57.                 "Error generating Ku from authentication pass phrase. \n");
  58.       exit (1);
  59.     }

  60. #else /* we'll use the insecure (but simplier) SNMPv1 */

  61.   /* set the SNMP version number */
  62.   session.version = SNMP_VERSION_1;

  63.   /* set the SNMPv1 community name used for authentication */
  64.   session.community = "demopublic";
  65.   session.community_len = strlen (session.community);

  66. #endif /* SNMPv1 */

  67.   /*
  68.    * Open the session
  69.    */
  70.   SOCK_STARTUP;
  71.   ss = snmp_open (&session);    /* establish the session */

  72.   if (!ss)/*看这里,ss的返回值总是NULL,所以通不过*/
  73.     {
  74.       snmp_perror ("ack");
  75.       snmp_log (LOG_ERR, "something horrible happened!!!\n");
  76.       exit (2);
  77.     }

  78.   /*
  79.    * Create the PDU for the data for our request.
  80.    *   1) We're going to GET the system.sysDescr.0 node.
  81.    */
  82.   pdu = snmp_pdu_create (SNMP_MSG_GET);
  83.   read_objid (".1.3.6.1.2.1.1.1.0", anOID, &anOID_len);

  84. #if OTHER_METHODS
  85.   get_node ("sysDescr.0", anOID, &anOID_len);
  86.   read_objid ("system.sysDescr.0", anOID, &anOID_len);
  87. #endif

  88.   snmp_add_null_var (pdu, anOID, anOID_len);

  89.   /*
  90.    * Send the Request out.
  91.    */
  92.   status = snmp_synch_response (ss, pdu, &response);

  93.   /*
  94.    * Process the response.
  95.    */
  96.   if (status == STAT_SUCCESS && response->;errstat == SNMP_ERR_NOERROR)
  97.     {
  98.       /*
  99.        * SUCCESS: Print the result variables
  100.        */

  101.       for (vars = response->;variables; vars; vars = vars->;next_variable)
  102.         print_variable (vars->;name, vars->;name_length, vars);

  103.       /* manipuate the information ourselves */
  104.       for (vars = response->;variables; vars; vars = vars->;next_variable)
  105.         {
  106.           if (vars->;type == ASN_OCTET_STR)
  107.             {
  108.               char *sp = (char *) malloc (1 + vars->;val_len);
  109.               memcpy (sp, vars->;val.string, vars->;val_len);
  110.               sp[vars->;val_len] = '\0';
  111.               printf ("value #%d is a string: %s\n", count++, sp);
  112.               free (sp);
  113.             }
  114.           else
  115.             printf ("value #%d is NOT a string! Ack!\n", count++);
  116.         }
  117.     }
  118.   else
  119.     {
  120.       /*
  121.        * FAILURE: print what went wrong!
  122.        */

  123.       if (status == STAT_SUCCESS)
  124.         fprintf (stderr, "Error in packet\nReason: %s\n",
  125.                  snmp_errstring (response->;errstat));
  126.       else
  127.         snmp_sess_perror ("snmpget", ss);

  128.     }

  129.   /*
  130.    * Clean up:
  131.    *  1) free the response.
  132.    *  2) close the session.
  133.    */
  134. if (response)
  135.     snmp_free_pdu (response);
  136.   snmp_close (ss);

  137.   SOCK_CLEANUP;
  138.   return (0);
  139. }                               /* main() */
复制代码


上面我标出了ss = snmp_open(&session)一句,我试了好多次,返回值总是NULL。本来正确的返回值应该是新malloc了一个struct snmp_session的指针,这肯定是snmp_open()中调用了malloc函数。唉,说不清楚,看看输出吧:
  1. [root@localhost example]# ./snmpdemoapp
  2. ack: Out of memory (malloc failure)
  3. something horrible happened!!!
复制代码


Out of memory这句不是我打印的,定是某个net-snmp提供的函数间接打印的,但是我find了net-snmp的源码包,没找到snmp_open()函数的源代码,而且/usr/include/net-snmp/目录下也找不到,只有ldd能显示用了那些库,但是又没办法看。



另外,程序中的agent指定为test.net-snmp.org是没问题的,我用snmpget、snmpwalk等程序试验过,都好使的。

大侠救命啊!![list=]

论坛徽章:
0
2 [报告]
发表于 2005-01-10 18:39 |只看该作者

陷入困境了,请大家帮忙啊(snmp问题,google没找到解决办法)

snmp_open()的原形在snmplib/snmp-api.c里.
出现这个问题是因为你的执行参数有问题, snmp_open()本身是没有问题的!

论坛徽章:
0
3 [报告]
发表于 2005-01-10 18:53 |只看该作者

陷入困境了,请大家帮忙啊(snmp问题,google没找到解决办法)

一般你要执行一个snmp_open之前要传递的参数至少要有
  1. -c community ipaddress
复制代码

这两项,而且在snmp_open()之前一定要执行
  1. SOCK_STARTUP;
复制代码

才可以!再试试看吧 :em11:

论坛徽章:
0
4 [报告]
发表于 2005-01-11 10:10 |只看该作者

陷入困境了,请大家帮忙啊(snmp问题,google没找到解决办法)

原帖由 "黄山松" 发表:
一般你要执行一个snmp_open之前要传递的参数至少要有
代码:
-c community ipaddress

这两项,而且在snmp_open()之前一定要执行
代码:
SOCK_STARTUP;

才可以!再试试看吧


谢谢回复!!我看net-snmp手册上说,SOCK_STARTUP在UNIX系统上是一个no-op,而且代码中也加了,请问您的第一句话怎么理解?这好象是命令行 的参数啊

论坛徽章:
0
5 [报告]
发表于 2005-01-11 12:39 |只看该作者

陷入困境了,请大家帮忙啊(snmp问题,google没找到解决办法)

恩,更正点错误,SOCK_STARTUP确实可以不要.
这两个参数一个是snmp的community,一个是目标IP地址,前一个可以不要,但在执行snmp_open()之前IP地址是一定要初始化的,如果要采用默认的端口什么的还要执行一个
  1. snmp_sess_init(&session)
复制代码

然后才可以open,做了一个小程序,你参照并理解一下:
  1. int main()
  2. {
  3.     char ipaddr[] = "127.0.0.1";
  4.        
  5.     // SOCK_STARTUP;

  6.     snmp_sess_init(&session);

  7.     session.peername = ipaddr;

  8.     ss = snmp_open(&session);

  9.     if (ss == NULL)
  10.                 printf("ss=Null.\n");
  11.     else
  12.                 free(ss);
  13.     exit(1);

  14. }
复制代码

论坛徽章:
0
6 [报告]
发表于 2005-01-11 13:03 |只看该作者

陷入困境了,请大家帮忙啊(snmp问题,google没找到解决办法)

谢谢您的帮助,我再试一下……

论坛徽章:
0
7 [报告]
发表于 2005-01-11 13:42 |只看该作者

陷入困境了,请大家帮忙啊(snmp问题,google没找到解决办法)

问题解决了吗?我看了,- 黄山松 的代码成分源程序里有,所以可以尝试着在源程序注释掉一些代码,直到调通,然后反过来,可以找到问题

论坛徽章:
0
8 [报告]
发表于 2005-01-11 14:01 |只看该作者

陷入困境了,请大家帮忙啊(snmp问题,google没找到解决办法)

[quote]原帖由 "Yufei00772002"]问题解决了吗?我看了,- 黄山松 的代码成分源程序里有,所以可以尝试着在源程序注释掉一些代码,直到调通,然后反过来,可以找到问题[/quote 发表:


谢谢,我也是正在按照这个路子走,把SNMPv3的选项都去掉了,只用v1,目前snmp_open正常返回,只是response返回状态不是SUCCESS,我再慢慢调着看看

论坛徽章:
0
9 [报告]
发表于 2005-01-11 14:17 |只看该作者

陷入困境了,请大家帮忙啊(snmp问题,google没找到解决办法)

adfasdfdsa

论坛徽章:
0
10 [报告]
发表于 2005-01-11 14:20 |只看该作者

陷入困境了,请大家帮忙啊(snmp问题,google没找到解决办法)

原帖由 "黄山松" 发表:


现成的源代码还要费这么大劲,学习能力那叫一个差 .


关键是以前没接触snmp,上司又要求2天之内拿出程序,实在是有点…有点倒行逆施了   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP