简单网络协议SNMP之net-snmp开发snmpget

简单网络协议SNMP之net-snmp开发snmpget

目的:通过net-snmp关于snmpget源码的开发,从snmp代理设备获得自己想要的信息

准备:从http://www.net-snmp.org/网址下载net-snmp-5.6.1.1.tar.gz源码包

 

开发过程:

1.解压源码

2.进入解压源码的目录

1) Run ./configure --prefix=/usr/local/net-snmp
     (type "./configure --help" for a quick usage summary.)
     (--prefix=PATH will change the default /usr/local installation path.)
     (see "Compilers and Options" on changing the compiler to use)

 2) make

 3) make install

3.安装完毕

在/usr/local/net-snmp底下内容如下:

root@an-virtual-machine:/usr/local/net-snmp# ls
bin  include  lib  sbin  share

进入lib,可以看到内容如下:

root@an-virtual-machine:/usr/local/net-snmp/lib# ls
libnetsnmp.a                 libnetsnmpmibs.la
libnetsnmpagent.a            libnetsnmpmibs.so
libnetsnmpagent.la           libnetsnmpmibs.so.25
libnetsnmpagent.so           libnetsnmpmibs.so.25.0.2
libnetsnmpagent.so.25        libnetsnmp.so
libnetsnmpagent.so.25.0.2    libnetsnmp.so.25
libnetsnmphelpers.a          libnetsnmp.so.25.0.2
libnetsnmphelpers.la         libnetsnmptrapd.a
libnetsnmphelpers.so         libnetsnmptrapd.la
libnetsnmphelpers.so.25      libnetsnmptrapd.so
libnetsnmphelpers.so.25.0.2  libnetsnmptrapd.so.25
libnetsnmp.la                libnetsnmptrapd.so.25.0.2
libnetsnmpmibs.a

4.在Qtcreator,新建一个项目TestNetSNMp

把上面lib里面的libnetsnmp.a  放到项目底下

在.pro里面添加内容如下:
INCLUDEPATH +=/usr/local/net-snmp/include

LIBS += -L/root/wyz/TestNetSNMP -lnetsnmp

 

5.在main里面添加代码如下:

  1. #include <net-snmp/net-snmp-config.h>   
  2.   
  3. #if HAVE_STDLIB_H   
  4. #include <stdlib.h>   
  5. #endif   
  6. #if HAVE_UNISTD_H   
  7. #include <unistd.h>   
  8. #endif   
  9. #if HAVE_STRING_H   
  10. #include <string.h>   
  11. #else   
  12. #include <strings.h>   
  13. #endif   
  14. #include <sys/types.h>   
  15. #if HAVE_NETINET_IN_H   
  16. #include <netinet/in.h>   
  17. #endif   
  18. #include <stdio.h>   
  19. #include <ctype.h>   
  20. #if TIME_WITH_SYS_TIME   
  21. # ifdef WIN32   
  22. #  include <sys/timeb.h>   
  23. # else   
  24. #  include <sys/time.h>   
  25. # endif   
  26. # include <time.h>   
  27. #else   
  28. # if HAVE_SYS_TIME_H   
  29. #  include <sys/time.h>   
  30. # else   
  31. #  include <time.h>   
  32. # endif   
  33. #endif   
  34. #if HAVE_SYS_SELECT_H   
  35. #include <sys/select.h>   
  36. #endif   
  37. #if HAVE_WINSOCK_H   
  38. #include <winsock.h>   
  39. #endif   
  40. #if HAVE_NETDB_H   
  41. #include <netdb.h>   
  42. #endif   
  43. #if HAVE_ARPA_INET_H   
  44. #include <arpa/inet.h>   
  45. #endif   
  46.   
  47. #include <net-snmp/utilities.h>   
  48.   
  49. #include <net-snmp/net-snmp-includes.h>   
  50. #include <vector>   
  51. #include <map>   
  52. #include <iostream>   
  53. using namespace  std;  
  54.   
  55. #define NETSNMP_DS_APP_DONT_FIX_PDUS 0   
  56.   
  57.    
  58.   
  59. string fprint_variable_1(const oid * objid,size_t objidlen, const netsnmp_variable_list * variable)  
  60. {  
  61.     u_char         *buf = NULL;  
  62.     size_t          buf_len = 256, out_len = 0;  
  63.     if ((buf = (u_char *) calloc(buf_len, 1)) == NULL) {  
  64.           
  65.         return "";  
  66.     } else {  
  67.         if (sprint_realloc_variable(&buf, &buf_len, &out_len, 1,  
  68.                                     objid, objidlen, variable)) {  
  69.             string strTemp((char*)buf);  
  70.               
  71.             int iFirst=strTemp.find_first_of(":");  
  72.             string strInfo=strTemp.substr(iFirst,strTemp.length()-1);  
  73.               
  74.             return strInfo;  
  75.               
  76.         } else {  
  77.             fprintf(stdout, "%s [TRUNCATED]\n", buf);  
  78.         }  
  79.     }  
  80.     SNMP_FREE(buf);  
  81. }  
  82.   
  83. string fprint_variable_2(const oid * objid,size_t objidlen, const netsnmp_variable_list * variable)  
  84. {  
  85.     u_char         *buf = NULL;  
  86.     size_t          buf_len = 256, out_len = 0;  
  87.     if ((buf = (u_char *) calloc(buf_len, 1)) == NULL) {  
  88.           
  89.         return "";  
  90.     } else {  
  91.         if (sprint_realloc_variable(&buf, &buf_len, &out_len, 1,  
  92.                                     objid, objidlen, variable)) {  
  93.             string strTemp((char*)buf);  
  94.               
  95.             return strTemp;  
  96.               
  97.         } else {  
  98.             fprintf(stdout, "%s [TRUNCATED]\n", buf);  
  99.         }  
  100.     }  
  101.     SNMP_FREE(buf);  
  102. }  
  103.   
  104. string SnmpGet(char *community,char *ip,char *oid_array)//snmpget -v2c -c public 10.0.0.11 1.3.6.1.2.1.1.5.0   
  105. {  
  106.     netsnmp_session session, *ss=(netsnmp_session*)malloc(sizeof(netsnmp_session));  
  107.     netsnmp_pdu    *response=(netsnmp_pdu*)malloc(sizeof(netsnmp_pdu));  
  108.       
  109.     netsnmp_variable_list *vars=NULL;  
  110.     netsnmp_pdu    *pdu;  
  111.       
  112.     if(!ss)  
  113.         ss = (netsnmp_session*)malloc(sizeof(netsnmp_session));  
  114.     if(!response)  
  115.         response=(netsnmp_pdu*)malloc(sizeof(netsnmp_pdu));  
  116.       
  117.       
  118.     int             count;  
  119.     int             current_name = 0;  
  120.     char           *names[SNMP_MAX_CMDLINE_OIDS];  
  121.     oid             name[MAX_OID_LEN];  
  122.     size_t          name_length;  
  123.     int             status;  
  124.     int             failures = 0;  
  125.     int             exitval = 0;  
  126.       
  127.     snmp_sess_init(&session);  
  128.       
  129.     session.version = SNMP_VERSION_2c;  
  130.       
  131.     session.peername = ip;  
  132.     session.community = (unsigned char*)community;  
  133.     session.community_len = strlen(community);  
  134.       
  135.       
  136.     names[0] = oid_array;  
  137.     current_name = 1;  
  138.       
  139.     SOCK_STARTUP;  
  140.       
  141.     snmp_close(ss);  
  142.     ss = snmp_open(&session);  
  143.       
  144.     if (ss == NULL) {  
  145.         snmp_sess_perror("snmpget", &session);  
  146.         SOCK_CLEANUP;  
  147.         return "";  
  148.     }  
  149.       
  150.     pdu = snmp_pdu_create(SNMP_MSG_GET);  
  151.     for (count = 0; count < current_name; count++) {  
  152.         name_length = MAX_OID_LEN;  
  153.         if (!snmp_parse_oid(names[count], name, &name_length)) {  
  154.             snmp_perror(names[count]);  
  155.             failures++;  
  156.         }  
  157.         else  
  158.         {  
  159.             snmp_add_null_var(pdu, name, name_length);  
  160.         }  
  161.     }  
  162.     if (failures) {  
  163.         snmp_close(ss);  
  164.         SOCK_CLEANUP;  
  165.         return "";  
  166.     }  
  167.       
  168.     retry:  
  169.     status = snmp_synch_response(ss, pdu, &response);  
  170.     if (status == STAT_SUCCESS) {  
  171.         if (response->errstat == SNMP_ERR_NOERROR)  
  172.         {     
  173.             vars = response->variables;  
  174.               
  175.             string strTemp=fprint_variable_1(vars->name, vars->name_length, vars);  
  176.               
  177.             return strTemp;  
  178.         }  
  179.         else  
  180.         {  
  181.             printf("2\n");  
  182.             fprintf(stderr, "Error in packet\nReason: %s\n",  
  183.                     snmp_errstring(response->errstat));  
  184.               
  185.             if (response->errindex != 0) {  
  186.                 fprintf(stderr, "Failed object: ");  
  187.                 for (count = 1, vars = response->variables;  
  188.                      vars && count != response->errindex;  
  189.                      vars = vars->next_variable, count++)  
  190.                     /*EMPTY*/;  
  191.                 if (vars) {  
  192.                     fprint_objid(stderr, vars->name, vars->name_length);  
  193.                 }  
  194.                 fprintf(stderr, "\n");  
  195.             }  
  196.             exitval = 2;  
  197.               
  198.               
  199.             if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,  
  200.                                         NETSNMP_DS_APP_DONT_FIX_PDUS)) {  
  201.                 pdu = snmp_fix_pdu(response, SNMP_MSG_GET);  
  202.                 snmp_free_pdu(response);  
  203.                 response = NULL;  
  204.                 if (pdu != NULL) {  
  205.                     goto retry;  
  206.                 }  
  207.             }  
  208.         }                       /* endif -- SNMP_ERR_NOERROR */  
  209.           
  210.     } else if (status == STAT_TIMEOUT) {  
  211.         fprintf(stderr, "Timeout: No Response from %s.\n",  
  212.                 session.peername);  
  213.         exitval = 1;  
  214.           
  215.     } else {                    /* status == STAT_ERROR */  
  216.         snmp_sess_perror("snmpget", ss);  
  217.         exitval = 1;  
  218.           
  219.     }                           /* endif -- STAT_SUCCESS */      
  220.       
  221.     if (response)  
  222.         snmp_free_pdu(response);  
  223.     snmp_close(ss);  
  224.       
  225.     SOCK_CLEANUP;  
  226.       
  227.     return "";  
  228. }  
  229.   
  230.    int main()  
  231. {  
  232.     string strSnmpInfo;  
  233.     //    strSnmpInfo=SnmpGet("2c","public","192.168.1.241",".1.3.6.1.4.1.11.2.3.9.1.1.7.0");   
  234.     //    strSnmpInfo=SnmpGet("2c","public","192.168.1.241",".1.3.6.1.2.1.1.1.0");   
  235.     strSnmpInfo=SnmpGet("public","192.168.1.133","1.3.6.1.2.1.1.1.0");  
  236.         
  237.     cout<<"wyz---------------snmpInfo::"<<strSnmpInfo<<endl;  
  238.     return 0;  
  239. }  
#include <net-snmp/net-snmp-config.h>

#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#include <sys/types.h>
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include <stdio.h>
#include <ctype.h>
#if TIME_WITH_SYS_TIME
# ifdef WIN32
#  include <sys/timeb.h>
# else
#  include <sys/time.h>
# endif
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif
#if HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#if HAVE_WINSOCK_H
#include <winsock.h>
#endif
#if HAVE_NETDB_H
#include <netdb.h>
#endif
#if HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif

#include <net-snmp/utilities.h>

#include <net-snmp/net-snmp-includes.h>
#include <vector>
#include <map>
#include <iostream>
using namespace  std;

#define NETSNMP_DS_APP_DONT_FIX_PDUS 0

 

string fprint_variable_1(const oid * objid,size_t objidlen, const netsnmp_variable_list * variable)
{
    u_char         *buf = NULL;
    size_t          buf_len = 256, out_len = 0;
    if ((buf = (u_char *) calloc(buf_len, 1)) == NULL) {
        
        return "";
    } else {
        if (sprint_realloc_variable(&buf, &buf_len, &out_len, 1,
                                    objid, objidlen, variable)) {
            string strTemp((char*)buf);
            
            int iFirst=strTemp.find_first_of(":");
            string strInfo=strTemp.substr(iFirst,strTemp.length()-1);
            
            return strInfo;
            
        } else {
            fprintf(stdout, "%s [TRUNCATED]\n", buf);
        }
    }
    SNMP_FREE(buf);
}

string fprint_variable_2(const oid * objid,size_t objidlen, const netsnmp_variable_list * variable)
{
    u_char         *buf = NULL;
    size_t          buf_len = 256, out_len = 0;
    if ((buf = (u_char *) calloc(buf_len, 1)) == NULL) {
        
        return "";
    } else {
        if (sprint_realloc_variable(&buf, &buf_len, &out_len, 1,
                                    objid, objidlen, variable)) {
            string strTemp((char*)buf);
            
            return strTemp;
            
        } else {
            fprintf(stdout, "%s [TRUNCATED]\n", buf);
        }
    }
    SNMP_FREE(buf);
}

string SnmpGet(char *community,char *ip,char *oid_array)//snmpget -v2c -c public 10.0.0.11 1.3.6.1.2.1.1.5.0
{
    netsnmp_session session, *ss=(netsnmp_session*)malloc(sizeof(netsnmp_session));
    netsnmp_pdu    *response=(netsnmp_pdu*)malloc(sizeof(netsnmp_pdu));
    
    netsnmp_variable_list *vars=NULL;
    netsnmp_pdu    *pdu;
    
    if(!ss)
        ss = (netsnmp_session*)malloc(sizeof(netsnmp_session));
    if(!response)
        response=(netsnmp_pdu*)malloc(sizeof(netsnmp_pdu));
    
    
    int             count;
    int             current_name = 0;
    char           *names[SNMP_MAX_CMDLINE_OIDS];
    oid             name[MAX_OID_LEN];
    size_t          name_length;
    int             status;
    int             failures = 0;
    int             exitval = 0;
    
    snmp_sess_init(&session);
    
    session.version = SNMP_VERSION_2c;
    
    session.peername = ip;
    session.community = (unsigned char*)community;
    session.community_len = strlen(community);
    
    
    names[0] = oid_array;
    current_name = 1;
    
    SOCK_STARTUP;
    
    snmp_close(ss);
    ss = snmp_open(&session);
    
    if (ss == NULL) {
        snmp_sess_perror("snmpget", &session);
        SOCK_CLEANUP;
        return "";
    }
    
    pdu = snmp_pdu_create(SNMP_MSG_GET);
    for (count = 0; count < current_name; count++) {
        name_length = MAX_OID_LEN;
        if (!snmp_parse_oid(names[count], name, &name_length)) {
            snmp_perror(names[count]);
            failures++;
        }
        else
        {
            snmp_add_null_var(pdu, name, name_length);
        }
    }
    if (failures) {
        snmp_close(ss);
        SOCK_CLEANUP;
        return "";
    }
    
    retry:
    status = snmp_synch_response(ss, pdu, &response);
    if (status == STAT_SUCCESS) {
        if (response->errstat == SNMP_ERR_NOERROR)
        {   
            vars = response->variables;
            
            string strTemp=fprint_variable_1(vars->name, vars->name_length, vars);
            
            return strTemp;
        }
        else
        {
            printf("2\n");
            fprintf(stderr, "Error in packet\nReason: %s\n",
                    snmp_errstring(response->errstat));
            
            if (response->errindex != 0) {
                fprintf(stderr, "Failed object: ");
                for (count = 1, vars = response->variables;
                     vars && count != response->errindex;
                     vars = vars->next_variable, count++)
                    /*EMPTY*/;
                if (vars) {
                    fprint_objid(stderr, vars->name, vars->name_length);
                }
                fprintf(stderr, "\n");
            }
            exitval = 2;
            
            
            if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
                                        NETSNMP_DS_APP_DONT_FIX_PDUS)) {
                pdu = snmp_fix_pdu(response, SNMP_MSG_GET);
                snmp_free_pdu(response);
                response = NULL;
                if (pdu != NULL) {
                    goto retry;
                }
            }
        }                       /* endif -- SNMP_ERR_NOERROR */
        
    } else if (status == STAT_TIMEOUT) {
        fprintf(stderr, "Timeout: No Response from %s.\n",
                session.peername);
        exitval = 1;
        
    } else {                    /* status == STAT_ERROR */
        snmp_sess_perror("snmpget", ss);
        exitval = 1;
        
    }                           /* endif -- STAT_SUCCESS */    
    
    if (response)
        snmp_free_pdu(response);
    snmp_close(ss);
    
    SOCK_CLEANUP;
    
    return "";
}

   int main()
{
    string strSnmpInfo;
    //    strSnmpInfo=SnmpGet("2c","public","192.168.1.241",".1.3.6.1.4.1.11.2.3.9.1.1.7.0");
    //    strSnmpInfo=SnmpGet("2c","public","192.168.1.241",".1.3.6.1.2.1.1.1.0");
    strSnmpInfo=SnmpGet("public","192.168.1.133","1.3.6.1.2.1.1.1.0");
      
    cout<<"wyz---------------snmpInfo::"<<strSnmpInfo<<endl;
    return 0;
}


 

编译,运行结果如下:

 root@an-virtual-machine:~/wyz/TestNetSNMP-build-desktop# ./TestNetSNMP
wyz---------------snmpInfo::: "Linux an-virtual-machine 2.6.35-22-generic #33-Ubuntu SMP Sun Sep 19 20:34:50 UTC 2010 i686"

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值