Command Line Calling Order

From Net-SNMP Wiki
Revision as of 23:18, 17 February 2010 by Wes (Talk | contribs) (expand snmp_parse_args)

Jump to: navigation, search

Most of the Net-SNMP command line applications (like snmpget, snmpwalk, snmpset, ...) have similar internal code. This page describes the calling structure of these applications. If you're writing your own applications this should help you understand the process flow of the applications.

Comments are written in // style for simplicity, but obviously this isn't callable code anyway...

Code in blue is application code, and code in black is functions called by the library code.

main()
   netsnmp_session session, *ss;
   netsnmp_pdu    *pdu;
   netsnmp_pdu    *response;
   netsnmp_variable_list *vars;

   // the only thing you can do before snmp_parse_args is call the netsnmp_ds_set_(bool|int|string) functions

   snmp_parse_args(argv, argc, &session, ...);   // accepts argv, argc, a session to initialize and optional additional arguments
       snmp_sess_init(session);
       getopt();                                 // and process
       init_snmp("snmpapp");                     // change to your app name if something special
       // setup special session params
       // like v3/USM keys

       session->peername = NEXT_ARGV_AFTER_OPTS
   
   SOCK_STARTUP;                                 // a win32 specific required macro

   ss = snmp_open(&session);
 
   pdu = snmp_pdu_create(SNMP_MSG_(GET|SET|GETNEXT|...));
   snmp_add_null_var(pdu, ...);                 // adding in variables you want to get/set

   status = snmp_sync_response(ss, pdu, &response);  // Note: pdu is "consumed" by this call; don't use it again!

   // results are in the response list, assuming no errors.  See the snmpget code for example error handling

   if (response)
     snmp_free_pdu(response);
   snmp_close(ss);
   SOCK_CLEANUP;