SNMP++的深入学习(一)

着上次的snmp_pp.lib文件的编译成功,我对于SNMP++的学习得以进一步学入,这次是学习SNMP++软件包中的各种类,它们分别是Oid类、IpAddress和UdpAddress类、Vb类、Pdu类、Snmp类,通过对这几种类的学习,最终我们可以通过SNMP++对支持SNMP管理的网络设备(如CISCO的交换机)进行SNMP操作(比如GET等)。
一、 自娱自乐的Oid类、IpAddress和UdpAddress类
为什么这么说,因为这两种类操作时没有网络通信的操作,Oid类主要作用就是构造Oid对象。我们可以通过snmp_pp.doc文件中提供的例子学习该类的使用(snmp_pp.doc是什么,就是官方的使用手册,从百度上可以找到中文翻译版,从www.yahoo.com上可以找到英文的原版,我把两个版本都下载下来了,对照着看收获很大)。Snmp_pp.doc文档中提供的例子太长了,为了让大家有耐心把源代码敲进去,我打算把这个例子分成两部分,一部分是一个最基本的Oid例子,另一部分是比较完整的例子。
1、 基本的Oid例子
在此我对于在VC++6.0中的操作也作一个简要的说明
1)新建一个Win32 Console Application程序,取名为snmpoid1,如图1所示:

SNMP++的深入学习(一)

图1
2)新建一个C++源程序,取名为snmpoid1,并添加到工程snmpoid1中,如图2所示:

SNMP++的深入学习(一)

图2
3)为了翻译时不出一大堆错误,我们需要在“Project”-“Settings”-“Link”中进行一些必要的设置,一是在“Object/library modules:”中添加snmp_pp.lib和ws2_32.lib,二是在“Project Options”中添加/nodefaultlib:libcmtd,如图3所示:

SNMP++的深入学习(一)

图3
4)源程序及运行结果如图4所示:

SNMP++的深入学习(一)

图4
2、 完整的Oid例子
有了上面的基础,我们已经可以确认我们有能力在VC++6.0中使用SNMP++的Oid类了,所以不妨全面的了解一下Oid类。源代表码如下(需要说明一点,这个源程序的最后一部分我没有调试通过,所以把它注释起来了,如果有谁知道sprintf这条语句怎么用,一定要告诉我一声啊):
#include "snmp_pp.h"
#include "oid.h"
#include
//#include
//#include
//#include
//#include
void main()
{
   // construct an Oid with a dotted string and print it out
 //用点分八进制定义一个Oid并输出它
   Oid o1("1.2.3.4.5.6.7.8.9.1");
   cout << "o1= " << o1.get_printable ()<
   // construct an Oid with another Oid and print it out
   //用一个Oid定义另一个Oid并输出它
   Oid o2(o1);
   cout << "o2= " << o2.get_printable()<
   // trim o2's last value and print it out
   //trim的作用是删除Oid最右边的n个子单元,默认是1个,从输出的结果看最右边的1被删掉了
   o2.trim(1);
   cout << "o2= " << o2.get_printable()<
 
   // add a 2 value to the end of o2 and print it out
   //把2这个值加到o2后面并输出它
  o2+=2;
   cout << "o2= " << o2.get_printable()<
 
   // create a new Oid, o3
   //定义一个新的Oid,o3
   Oid o3;
   // assign o3 a value and print it out
   //定义o3的值并输出它
   o3="1.2.3.4.5.6.7.8.9.3";
   cout << "o3= " << o3.get_printable()<
 
   // create o4
   //创建一个Oid,o4
   Oid o4;


   // assign o4 o1's value
   //用o1来定义o4的值
   o4=o1;
 
   // trim off o4 by 1
   //删除o4最右边的一个值
   o4.trim(1);
 
   // concat a 4 onto o4 and print it out
   //o4后面加上".4"并输出
  o4+=".4";
   cout << "o4= " << o4.get_printable()<
 
   // make o5 from o1 and print it out
   //用o1来定义并输出它
   Oid o5(o1);
   cout << "o5= " << o5.get_printable()<
   // compare two not equal oids
   //比较两个oid的值是否相等,因为o1的最后一个为1,o2的最后一位为2,所以不相等
   if (o1==o2)   cout << "O1 EQUALS O2"<
   else    cout << "o1 not equal to o2"<
              
   // print out a piece of o1
   //输出o1的一片,strval的作用是get string value of a variable,结合到本例是取1-3
   cout << "strval(3) of O1 = " << o1.get_printable(3)<
 
   // print out a piece of o1
   //输出从左边数1至3
   cout << "strval(1,3) of O1 = " << o1.get_printable(1,3)<
 
   // set o1's last subid
  o1[ o1.len()-1] = 49;
   cout << "O1 modified = " << o1.get_printable()<
 
   // set o1's 3rd subid
   o1[2]=49;
   cout << "O1 modified = " << o1.get_printable()<
  
   // get the last subid of 02
   cout << "last of o2 = " << o2[o2.len()-1]<
  
   // get the 3rd subid of 02
   cout << "3rd of o2 = " << o2[2]<
  
   // ncompare
   //ncompare的作用是从左至右比较oid的前n个子单元,因为本例中o1与o2有三个单元相同,所以输出“==”
   if (o1.nCompare(3,o2))
      cout << "nCompare o1,o2,3 =="<
   else
     cout << "nCompare o1,o2,3 !="<
    
   // make an array of oids
   Oid oids[30]; int w;
   for ( w=0;w<30;w++)                          
   {
     oids[w] = "300.301.302.303.304.305.306.307";
     oids[w] += (w+1);
   }  
 /*for (w=0;w<25;w++)
   {
     sprintf( msg,"Oids[%d] = %s",w, oids[w].get_printable());
     printf("%s",msg, strlen(msg));
    }*/ 
}
3、IpAddress和UdpAddress类
1)第一个源程序代码:
#include "snmp_pp.h"
#include
void main()
{
 GenAddress address1("10.4.8.5");    // make an IP GenAddress生成一个GenAddress IP地址
 cout<<
GenAddress address2("01020304-10111213141516");    // make an IPX GenAddress
cout<<
GenAddress address3("01:02:03:04:05:06");    // make a MAC GenAddress生成一个GenAddress MAC地址
 
cout << address3.get_printable()<
 
if ( !address1.valid())    // check validity
    cout << "address1 ! valid";
}
运行结果如图5所示

SNMP++的深入学习(一)

图5
2)第二个程序源代码
// address class examples
#include
#include "snmp_pp.h"
#include "address.h"
void main()
{
   //--------------[ IPAddress construction ]------------------------------------------------------
   IpAddress ip1("");    // makes an invalid IpAddress object,如果是写成Ipaddress ip1();那就是不能为ip1赋值,但是
   //如果写成IpAddress ip1("");,就可以为ip1赋值
   IpAddress ip2("10.4.8.5");    // makes a IpAddress verifies dotted format
   //用点分十进制的形式定义了IpAddress ip2
   IpAddress ip3(ip2);    // makes an IpAddress using another IpAddress
   //用ip2定义ip3
  IpAddress ip4("trout.rose.hp.com");       // makes an IpAddress does DNS on string
    cout << "ip2="<
 cout << "ip3="<
 cout << "ip4="<
   //--------------[ MAC Address construction ]-----------------------------------------------------
   MacAddress mac1("");    // makes an invalid MAC address 同样的道理,在括号里加入了""可以为mac1赋值了
   MacAddress mac2("08:09:12:34:52:12");    // makes and verifies a MAC address
   //定义了MacAddress mac2
   MacAddress mac3(mac2);   // makes a MAC from another MAC
   //用mac2定义了mac3
  cout<<"mac2="<
 cout<<"mac3="<
 //从输出结果看mac2和mac3是一样的
 //---------------[ Gen Address Construction ]-----------------------------------------------------
 GenAddress addr1("10.4.8.5"); //定义了GenAddress addr1
 GenAddress addr2("01020304:050607080900"); //定义了GenAddress addr2
 cout<<"addr1="<
 cout<<"addr2="<
  //---------------[ assigning Addresses ]------------------------------------------------------------
   ip1="15.29.33.10";
   cout<<"ip1="<
//   ipx1 = "00000001-080912345212";
   mac1 = "08:09:12:34:52:12";
   cout<<"mac1="<
}
输出结果如图6所示

SNMP++的深入学习(一)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值