Skip to content

Commit

Permalink
adding SNMP example
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-naumov committed Feb 3, 2015
1 parent 28d7262 commit 4fa0011
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions C/sockets/UDP/SNMP/snmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// gcc `net-snmp-config --cflags` `net-snmp-config --libs` `net-snmp-config --external-libs` snmp.c -o snmp

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

int main(int argc, char ** argv)
{
struct snmp_session session;
struct snmp_session *sess_handle;
struct snmp_pdu *pdu;
struct snmp_pdu *response;
struct variable_list *vars;

oid id_oid[MAX_OID_LEN];

size_t id_len = MAX_OID_LEN;

if(argv[1] == NULL){
printf("Please supply a hostname\n");
exit(1);
}

init_snmp("APC Check");

snmp_sess_init( &session );
session.version = SNMP_VERSION_2c;
session.community = "public";
session.community_len = strlen(session.community);
session.peername = argv[1];
sess_handle = snmp_open(&session);

pdu = snmp_pdu_create(SNMP_MSG_GET);

read_objid("SNMPv2-MIB::sysDescr.0", id_oid, &id_len);
snmp_add_null_var(pdu, id_oid, id_len);

if ((snmp_synch_response(sess_handle, pdu, &response)) == 0)
for(vars = response->variables; vars; vars = vars->next_variable)
print_value(vars->name, vars->name_length, vars);
else
printf("NO SNMP\n");

snmp_free_pdu(response);
snmp_close(sess_handle);

return (0);
}

0 comments on commit 4fa0011

Please sign in to comment.