Example of UCD-SNMP code (1)

Source: Internet
Author: User

After learning the SNMP protocol, we then perform an instance operation on the UCD-SNMP software. So we have already talked about some basic content about this software. The following network management program will be analyzed in detail.

A simple SNMP Network Management Program

Next, let's first use ucd-snmp to write an SNMP Network Program, and try to use this software package from the application perspective. this program is so simple that it does not support any command line parameters. It only obtains the system description from the agent.

The program code is as follows:

1) /* snmpapp.c - a simple SNMP application */
2) #include <ucd-snmp/ucd-snmp-config.h>;
3) #include <ucd-snmp/ucd-snmp-includes.h>;
4) #include <ucd-snmp/system.h>;
5) int main(int argc, char * argv[])
6) {
7) struct snmp_session session, *ss;
8) struct snmp_pdu *request, *response;
9)
10) oid myoid[MAX_OID_LEN];
11) size_t myoid_len = MAX_OID_LEN;
12) struct variable_list *vars;
13) int status;
14) init_snmp("snmpapp");
15) snmp_sess_init( &session );
16) session.version = SNMP_VERSION_1;
17) session.peername = "localhost";
18) session.community = "public";
19) session.community_len = strlen(session.community);
20) SOCK_STARTUP;
21) ss = snmp_open(&session);
22) request = snmp_pdu_create(SNMP_MSG_GET);
23) read_objid("system.sysDescr.0", myoid, &myoid_len);
24) snmp_add_null_var(request, myoid, myoid_len);
25) status = snmp_synch_response(ss, request, &response);
26) if (status == STAT_SUCCESS &&
27) response->;errstat == SNMP_ERR_NOERROR)
28) {
29) for(vars = response->;variables; vars; vars = vars->;next_variable)
30) print_variable(vars->;name, vars->;name_length, vars);
31) } else {
32) if (status == STAT_SUCCESS)
33) fprintf(stderr, "Error in packet\nReason: %s\n",
34) snmp_errstring(response->;errstat));
35) else
36) snmp_sess_perror("snmpget", ss);
37) }
38) if (response)
39) snmp_free_pdu(response);
40) snmp_close(ss);
41) SOCK_CLEANUP;
42) return (0);
43) }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.