snmpwalkのコードを見た後、コードでそれを行う方法の簡単な例を入手しました。
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <string.h>
void
snmp_get_and_print(netsnmp_session * ss, oid * theoid, size_t theoid_len)
{
netsnmp_pdu *pdu, *response;
netsnmp_variable_list *vars;
int status;
pdu = snmp_pdu_create(SNMP_MSG_GET);
snmp_add_null_var(pdu, theoid, theoid_len);
status = snmp_synch_response(ss, pdu, &response);
if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
for (vars = response->variables; vars; vars = vars->next_variable) {
print_variable(vars->name, vars->name_length, vars);
}
}
if (response) {
snmp_free_pdu(response);
}
}
int main(int argc, char ** argv)
{
netsnmp_session session, *ss;
netsnmp_pdu *pdu, *response;
netsnmp_variable_list *vars;
oid name[MAX_OID_LEN];
size_t name_length;
oid root[MAX_OID_LEN];
size_t rootlen;
oid end_oid[MAX_OID_LEN];
size_t end_len = 0;
int count;
int running;
int status = STAT_ERROR;;
init_snmp("snmpwalk");
snmp_sess_init( &session );
session.peername = strdup("SNMP.device.domain");
//session.version = SNMP_VERSION_1;
session.version = SNMP_VERSION_2c;
session.community = "public";
session.community_len = strlen(session.community);
SOCK_STARTUP;
ss = snmp_open(&session);
if (!ss) {
snmp_sess_perror("ack", &session);
SOCK_CLEANUP;
exit(1);
}
rootlen = MAX_OID_LEN;
if (snmp_parse_oid("RFC1213-MIB::ifIndex", root, &rootlen) == NULL) {
snmp_perror("RFC1213-MIB::ifIndex");
exit(1);
}
memmove(end_oid, root, rootlen*sizeof(oid));
end_len = rootlen;
end_oid[end_len-1]++;
memmove(name, root, rootlen * sizeof(oid));
name_length = rootlen;
running = 1;
while (running) {
// create PDU for GETNEXT request and add object name to request
pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
snmp_add_null_var(pdu, name, name_length);
status = snmp_synch_response(ss, pdu, &response);
if (status == STAT_SUCCESS) {
if (response->errstat == SNMP_ERR_NOERROR) {
// check resulting variables
for (vars = response->variables; vars;
vars = vars->next_variable) {
if (snmp_oid_compare(end_oid, end_len,
vars->name, vars->name_length) <= 0) {
//not part of this subtree
running = 0;
continue;
}
print_variable(vars->name, vars->name_length, vars);
memmove((char *) name, (char *) vars->name,
vars->name_length * sizeof(oid));
name_length = vars->name_length;
}
}
}
if (response)
snmp_free_pdu(response);
}
snmp_close(ss);
SOCK_CLEANUP;
return (0);
} // main()