|
|
 |
 |
 |
软 件 安 装 |
 |
软 件 知 识 |
 |
经 验 技 巧 |
 |
学 习 指 南 |
 |
相 关 下 载 |
|
 |
|
 |
 |
| |
|
|
|
|
|
 |
|
| 作者:未知 时间:2004-07-14 22:32:48 |
|
maxen (enthusiast)
去年在一个项目中使用了Redback SMS10000 的接入服务器,作为附加要求,需要做一个snmp的接口程序,目的是起发送一个 subscriber reauth 的 snmp 包给接入服务器; 由于snmp的例程相对较少,所以在此截选程序中的snmp 相关部分以后,做一个注脚,记录下来。
程序使用了net-snmp 的API,所以系统要预先安装net-snmp; 编译时需要使用带入netsnmp的行命令:gcc -o redweb redweb.c -lkstat -lm -lsocket -ladm -lnetsnmp -lcrypto 不要更多罗嗦了,都写在程序里了。所谓知识无价值。
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 */
|
|
|
|
 |
|
|
|
|