I am using this code to loop the GET_BULK multiple times to get the logs of a Switch. I am using loop because there are a lot of values in the logs subtree. This code works perfectly fine except for the fact that it only prints the data for one GET_BULK request. The output of this code is same as that of when I use GETBULK in the MIB Browser. At first I thought it might be an issue with the output buffer of C, but now it seems like it has some problem with the GET_BULK buffer or either the loop logic. I am new to net-snmp, and can't find where I am going wrong with this.
#include <stdio.h>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
int main()
{
oid target_oid[] = {1, 3, 6, 1, 4, 1, 89, 82, 2, 9, 1, 6};
size_t target_oid_len = OID_LENGTH(target_oid);
init_snmp("snmpget");
// SNMP
netsnmp_session session, *ss;
snmp_sess_init(&session);
session.peername = strdup("172.16.100.32");
session.version = SNMP_VERSION_2c;
session.community = "public";
session.community_len = strlen(session.community);
// Sock
SOCK_STARTUP;
ss = snmp_open(&session);
if (!ss)
{
snmp_perror("snmp_open");
SOCK_CLEANUP;
exit(1);
}
// SNMP
netsnmp_pdu *response;
netsnmp_pdu *pdu;
int status;
int start_index = 1;
int num_requests = 20;
for (int i = 0; i < num_requests; i++)
{
pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
pdu->non_repeaters = 0;
pdu->max_repetitions = 1000;
snmp_pdu_add_variable(pdu, target_oid, target_oid_len, ASN_NULL, NULL, 0);
pdu->variables->name[target_oid_len - 1] = start_index + i;
pdu->variables->name_length = target_oid_len + 1;
status = snmp_synch_response(ss, pdu, &response);
if (status == STAT_SUCCESS && response->variables)
{
for (netsnmp_variable_list *vars = response->variables; vars; vars = vars->next_variable)
{
if (vars->name[11] == 6 && vars->name[9] == 9)
{
printf("%.*s\n", (int)vars->val_len, vars->val.string);
}
}
}
else
{
fprintf(stderr, "Error: %s\n", snmp_api_errstring(snmp_errno));
snmp_sess_perror("snmpget", ss);
}
// Clean up
snmp_free_pdu(response);
}
snmp_close(ss);
SOCK_CLEANUP;
return 0;
}