snmp get_next示例

完成SNMP操作大致需要的步奏为:

1)初始化Winsocket套接字   2)实例化一个Snmp对象  3)实例化一个Ctarget对象 4)调用Snmp对象的操作方法  5)关闭Winsocket套接字


示例程序如下:       

#一般操作均可在该基础上修改,扩充。


Snmp::socket_start_up();                                                //初始化Winsocket套接字

snmp_version version=version1;                                   

int status;

Snmp  snmp(status,0,false);                                           //创建一个snmp对象

Pdu pdu;                                                                         //创建一个pdu对象

Vb vb;                                                                              //创建一个vb对象

vb.set.oid((Oid)"1.3.6.1.2.1.1");                                      

pdu+=vb;                                                                          //设置vb对象并将其添加到对象pdu中

UdpAddress  address("127.0.0.1:161");                            

CTarget  ctarget(address);                                              //创建一个ctarget对象

ctarget.set_version(version);

ctarget.set_retry(1);

ctarget.set_timeout(100);

ctarget.set_readcommunity("public");                             //设置ctarget对象

SnmpTarget *target;

target=&ctarget;                                                                                                                   

status=snmp.get(pdu,*target);                                        //发起一个Get操作                                                               

if(status==SNMP_CLASS_SUCCESS)

{

 pdu.get_vb(vb,0);

CString reply_oid=vb.get_printable_oid();

CString reply_value=vb.get_printable_value();

}                                                                                     //操作成功,将被管理对象的OID和值保存到reply_oid和reply_value中。

Snmp::socket_cleanup();

}


你可能感兴趣的:(C++,SNMP)