Created
June 15, 2018 13:08
-
-
Save Jed-Giblin/f5a4b2ad1098ea31be67581f23e4127a to your computer and use it in GitHub Desktop.
Perform a noAuthNoPriv SNMP-GET request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdint.h> | |
#include <net-snmp/net-snmp-config.h> | |
#include <net-snmp/net-snmp-includes.h> | |
#include <net-snmp/library/snmpv3.h> | |
#include <net-snmp/library/snmpusm.h> | |
#include <net-snmp/library/snmp_api.h> | |
#include <net-snmp/net-snmp-includes.h> | |
int main(void) { | |
// Create some variables | |
struct snmp_session session, *ss; | |
struct snmp_pdu *pdu; | |
struct snmp_pdu *response; | |
oid anOID[MAX_OID_LEN]; | |
size_t anOID_len = MAX_OID_LEN; | |
struct variable_list *vars; | |
int status; | |
// Startup the SNMP lib | |
init_snmp("snmpapp"); | |
snmp_sess_init( &session ); | |
// Configure the session | |
session.peername = "127.0.0.1"; | |
session.version=SNMP_VERSION_3; | |
session.securityName = strdup("snmpnoauth"); | |
session.securityNameLen = strlen(session.securityName); | |
session.securityLevel = SNMP_SEC_LEVEL_NOAUTH; | |
// Set the defaults of the AuthProto since noAuthNoPriv doesn't care | |
session.securityAuthProto = usmHMACSHA1AuthProtocol; | |
session.securityAuthProtoLen = sizeof(usmHMACSHA1AuthProtocol) / sizeof(oid);; | |
session.securityAuthKeyLen = USM_AUTH_KU_LEN; | |
// Set the defaults of the Priv since noAuthNoPriv doesn't care | |
session.securityPrivProto = usmDESPrivProtocol; | |
session.securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN; | |
session.securityPrivKeyLen = USM_PRIV_KU_LEN; | |
// Print out some of these symbols | |
printf("%d\n", (int) sizeof(usmHMACSHA1AuthProtocol)); | |
printf("%d\n", (int) sizeof(oid)); | |
printf("%d\n", (int) USM_PRIV_PROTO_DES_LEN); | |
SOCK_STARTUP; | |
ss = snmp_open(&session); | |
pdu = snmp_pdu_create(SNMP_MSG_GET); | |
anOID_len = MAX_OID_LEN; | |
if (!snmp_parse_oid("1.3.6.1.2.1.2.2.1.2.2", anOID, &anOID_len)) { | |
snmp_perror("1.3.6.1.2.1.2.2.1.2.2"); | |
SOCK_CLEANUP; | |
exit(1); | |
} | |
snmp_add_null_var(pdu, anOID, anOID_len); | |
status = snmp_synch_response(ss, pdu, &response); | |
if (status == STAT_SUCCESS) | |
for (vars = response->variables; vars; vars = vars->next_variable) | |
print_variable(vars->name, vars->name_length, vars); | |
else if (status == STAT_TIMEOUT) | |
fprintf(stderr, "Timeout: No response from %s.\n", session.peername); | |
else { | |
snmp_sess_perror("unknown error\n", ss); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can then compile this and run it:
gcc gist.c -o snmpgist -I /usr/include/ -lnetsnmp
and run it:
./snmpgist
and see:
80 8 10 IF-MIB::ifDescr.2 = STRING: GigabitEthernet0/1