Programming using the net-snmp API

Source: Internet
Author: User
Tags snmp snmp api sprintf

A Redback sms10000 Access Server is used in a project. As an additional requirement, an SNMP interface program is required to send a subscriber Reauth snmp package to the access server;
Because there are relatively few SNMP routines, we will make a note and record the SNMP-related part in this cut-down program.
The program uses the net-snmp API, so the system must install net-snmp in advance;
During compilation, use the line command to bring netsnmp:
Gcc-O redweb. C-lkstat-lm-lsocket-ladm-lnetsnmp-lcrypto
Don't be more arrogant. They are all written in the program. The so-called knowledge has no value.
Static char snmpreauth [] = ". 1.3.6.1.4.1.2352.2.14.1.2.1.1 .";
Struct snmp_session session, * ss;
Struct snmp_pdu * response;
Struct snmp_pdu * PDU;
Char snmpcommunity [32];
Char snmpstr [256];
Char tmpstr [128];
Char * endptr;
Oid name [512];
Int name_length;
Int CNT, id = 0;
Int snmpsetvalue = 1;
Init_mib ();
Add_mibdir ("/opt/weblogin/mibs ");
// Include Redback mibs File
Snmp_sess_init (& session );
Session. Version = snmp_version_1;
Session. peername = strdup (data. nas_address );
Session. Community = strdup (snmpcommunity );
Session. community_len = strlen (session. Community );
Session. retries = 0;
Session. Timeout = 12000;
Session. sessid = strtol (data. timeid, & endptr, 0 );
Sock_startup;
/* Macro
* Open an SNMP session
*/
Ss = snmp_open (& session );
If (Ss = NULL ){
/*
* Diagnose snmp_open errors with the input netsnmp_session pointer
*/
Snmp_sess_perror ("snmptable", & session );
Sock_cleanup;
}
/*
* Create PDU for Set Request and add object names and values to request
*/
PDU = (struct snmp_pdu *) snmp_pdu_create (snmp_msg_set );
If (PDU = NULL)
{
Printf ("unable to create the PDU ");
}
Sprintf (snmpstr, "% S17", snmpreauth );
For (CNT = 0; CNT <17; CNT ++)
{
Sprintf (tmpstr, ". % d", (char *) data. session_id [CNT]);
// Data. session_id Mac Format String
Strcat (snmpstr, tmpstr );
}
// Fixme
Name_length = 512;
If (! Read_objid (snmpstr, name, & name_length )){
Printf ("error in read_objid ");
Exit (1 );
};
Snmp_pdu_add_variable (PDU, name, name_length, asn_integer,
& Snmpsetvalue, sizeof (snmpsetvalue ));
Status = snmp_synch_response (SS, PDU, & response );
// Sync SNMP
If (status = stat_success ){
If (response-> errstat = snmp_err_noerror ){
}
Exitval = 2;
} Else if (status = stat_timeout ){
  
  
Fprintf (stderr, "SNMP Timeout: no response from % s ",
Session. peername );
Exitval = 1;
} Else {
Snmp_sess_perror ("snmpset", SS );
Printf ("SNMP session else ");
Exitval = 1;
}
If (response)
Snmp_free_pdu (response );
Snmp_close (SS );
Sock_cleanup;
/* Macro off
* Final a snmp session
*/

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.