1

我正在尝试创建一个简单的 SNMP 陷阱生成器程序,以便更好地理解 Net-SNMP 库。使用以下代码,我不断收到错误

snmptrap:找到配置数据,但无法配置传输

会话未打开

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

#include "snmptrap_simple.h"

int main (int argc, char **argv) {
  oid             objid_enterprise[] = { 1, 3, 6, 1, 4, 1, 3, 1, 1 };
  oid             objid_sysdescr[] = { 1, 3, 6, 1, 2, 1, 1, 1, 0 };
  oid             objid_sysuptime[] = { 1, 3, 6, 1, 2, 1, 1, 3, 0 };
  oid             objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };

  netsnmp_session session, *ss;
  netsnmp_pdu    *pdu, *response;
  oid             name[MAX_OID_LEN];
  size_t          name_length;
  int             arg;
  int             status;
  char           *trap = NULL;
  char           *prognam;
  int             exitval = 0;

  session.version       = SNMP_VERSION_1;
  session.retries       = 2;
  char address[]        = "127.0.0.1";
  char *ptrAddress      = address;
  session.peername      = ptrAddress;
  u_char comm[]         = "public";
  const u_char *community = comm;
  session.community     = community;
  session.community_len = strlen(session.community);

  /* windows32 specific initialization (is a noop on unix) */
  SOCK_STARTUP;
  // open snmp session
  ss = snmp_open(&session);
  if (!ss) {
    snmp_perror("ack");
    snmp_sess_perror("snmptrap", &session);
    snmp_log(LOG_DEBUG, "Session did not open\n");
    exit(2);
  }
  snmp_log(LOG_DEBUG, "DEBUG OUTPUT");
  // create the PDU
  pdu = snmp_pdu_create(SNMP_MSG_TRAP);
  if ( !pdu ) {
      fprintf(stderr, "Failed to create trap PDU\n");
      SOCK_CLEANUP;
      exit(1);
  }

  // add variables to the PDU
  char *value;
  *value = '42';
  if(snmp_add_var (pdu, name, name_length, 'i', value)){
    //void snmp_perror(const char *prog_string)
    snmp_perror("add variable");
    SOCK_CLEANUP;
    exit(1);
  };

  send_trap_to_sess (&session, pdu);

  return 0;
}

我究竟做错了什么?

我在这里有一个代码的 github 存储库:https ://github.com/EhevuTov/tcp2snmp/blob/master/test/snmptrap_simple.c

4

2 回答 2

1

您可能只需要init_snmp("snmptrap_simple");snmp_open().

于 2013-01-31T22:36:56.593 回答
0

你需要使用这个

snmp_sess_init(&ss);
于 2017-09-21T21:53:24.647 回答