SNMP++的深入學習(二)
阿新 • • 發佈:2019-01-23
二、必須經過的階段Vb類、Pdu類
使用Vb類,主要目的是在處理返回的SNMP應答包時,獲得返回的SNMP變數值,有時也需要獲得返回的SNMP變數OID。
原始碼如下:
#include "snmp_pp.h"
#include "oid.h"
#include "vb.h"
#include <iostream.h>
void main()
{
// -------[Ways to construct Vb objects ]-------
// construct a single Vb object 定義一個vb1
Vb vb1;
// construct a Vb object with an Oid object
// this sets the Oid portion of the Vb
Oid d1("1.3.6.1.4.12"); //定義了一個Oid d1,值為1.3.6.1.4.12
Vb vb2(d1);//用d1來為vb2賦值
cout<<"vb2's oid is "<<vb2.get_printable_oid()<<endl;
// construct a Vb object with a dotted string 用一個點分制字串來定義vb3的oid值
Vb vb3( (Oid) "1.2.3.4.5.6");
cout<<"vb3's oid is "<<vb3.get_printable_oid()<<endl;
// construct an array of ten Vbs
Vb vbs[10];
//------[Ways to set and get the Oid portion of Vb objects ]
// set and get the Oid portion portion的意思是part piece
Oid d2((Oid)"1.2.3.4.5.6");
vb1.set_oid(d2); //用d2的值來定義vb1的oid值,由於現在d2值為1.2.3.4.5.6,所以vb1的oid值也為1.2.3.4.5.6
cout<<"vb1's oid is "<<vb1.get_printable_oid()<<endl;
Oid d3;
vb1.get_oid(d3); //d3取vb1的oid,由於vb1的oid值是用d2的值來定義的,所以d3的值也為1.2.3.4.5.6
cout<<"d3's oid is "<<d3.get_printable()<<endl;
if (d2==d3) cout << "They better be equal!!/n";
Vb ten_vbs[10];
int z;
for (z=0;z<10;z++)
ten_vbs[0].set_oid((Oid)"1.2.3.4.5"); //先對ten_vbs[0]的值作了一個定義
//-------[ ways to set and get values ]
// set & get ints
int x,y;
x=5;
vb1.set_value(x); //將vb1的value值設為x的值,即為5
cout<<"vb1'value is "<<vb1.get_printable_value()<<endl;
vb1.get_value(y);//將vb1的value值設給y
cout<<"y is "<<y<<endl;
if ( x == y) cout << "x equals y/n";
// set and get long ints
long int a,b;
a=100;
cout<<"a is "<<a<<endl;
//-------[ ways to set and get values ]
if ( a == b) cout << "a equals b/n" ;
else cout<<"a is not equals b/n";
// set & get unsigned long ints
unsigned long int c,d;
c = 1000;
vbs[0].set_value( c); //將vbs[0]的value值設為1000
vbs[0].get_value( d); //用vbs[0]值設y的值
if ( c == d) cout << "c equals d/n";
// set a 64 bit counter
Counter64 c64(1000,1001);
vbs[1].set_value( c64);
// get and set an oid as a value
Oid o1, o2; //定義兩個Oid o1 o2
o1 = "1.2.3.4.5.6";
vbs[2].set_value( o1); //將vbs[2]的value值設為o1即1.2.3.4.5.6
vbs[2].get_value( o2); //將o2的值設為1.2.3.4.5.6
if ( o1 == o2) cout << "o1 equals o2/n";
// set and get an octet string
unsigned char data[4],outdata[4];
unsigned long len,outlen;
len =4; data[0] = 10; data[1] = 12; data[2] = 12; data[3] = 13;
OctetStr octetstr(data,len);
vbs[3].set_value( octetstr);
vbs[3].get_value( octetstr);
// get & set a string
char beer[80]; //定義了一個字串beer
char good_beer[80]; //定義了一個字串good_beer
strcpy( beer,"Sierra Nevada Pale Ale");
cout<<"beer is "<<beer<<endl;
vbs[4].set_value( beer); //將beer的值設定給vbs[4]的value
vbs[4].get_value( good_beer); //又將good_beer的值設定為beer的值
printf("Good Beer = %s/n",good_beer);
// get and set an ip an address
IpAddress ipaddress1, ipaddress2;
ipaddress1 = "10.4.8.5";
vbs[5].set_value( ipaddress1);
vbs[5].get_value( ipaddress2);
cout << "ipaddress2 is "<<ipaddress2<<endl;
} // end vb example
執行結果如圖7所示
使用Vb類,主要目的是在處理返回的SNMP應答包時,獲得返回的SNMP變數值,有時也需要獲得返回的SNMP變數OID。
原始碼如下:
#include "snmp_pp.h"
#include "oid.h"
#include "vb.h"
#include <iostream.h>
void main()
{
// -------[Ways to construct Vb objects ]-------
// construct a single Vb object 定義一個vb1
Vb vb1;
// construct a Vb object with an Oid object
// this sets the Oid portion of the Vb
Oid d1("1.3.6.1.4.12"); //定義了一個Oid d1,值為1.3.6.1.4.12
Vb vb2(d1);//用d1來為vb2賦值
cout<<"vb2's oid is "<<vb2.get_printable_oid()<<endl;
// construct a Vb object with a dotted string 用一個點分制字串來定義vb3的oid值
Vb vb3( (Oid) "1.2.3.4.5.6");
cout<<"vb3's oid is "<<vb3.get_printable_oid()<<endl;
// construct an array of ten Vbs
Vb vbs[10];
//------[Ways to set and get the Oid portion of Vb objects ]
// set and get the Oid portion portion的意思是part piece
Oid d2((Oid)"1.2.3.4.5.6");
vb1.set_oid(d2); //用d2的值來定義vb1的oid值,由於現在d2值為1.2.3.4.5.6,所以vb1的oid值也為1.2.3.4.5.6
cout<<"vb1's oid is "<<vb1.get_printable_oid()<<endl;
Oid d3;
vb1.get_oid(d3); //d3取vb1的oid,由於vb1的oid值是用d2的值來定義的,所以d3的值也為1.2.3.4.5.6
cout<<"d3's oid is "<<d3.get_printable()<<endl;
if (d2==d3) cout << "They better be equal!!/n";
Vb ten_vbs[10];
int z;
for (z=0;z<10;z++)
ten_vbs[0].set_oid((Oid)"1.2.3.4.5"); //先對ten_vbs[0]的值作了一個定義
//-------[ ways to set and get values ]
// set & get ints
int x,y;
x=5;
vb1.set_value(x); //將vb1的value值設為x的值,即為5
cout<<"vb1'value is "<<vb1.get_printable_value()<<endl;
vb1.get_value(y);//將vb1的value值設給y
cout<<"y is "<<y<<endl;
if ( x == y) cout << "x equals y/n";
// set and get long ints
long int a,b;
a=100;
cout<<"a is "<<a<<endl;
//-------[ ways to set and get values ]
if ( a == b) cout << "a equals b/n" ;
else cout<<"a is not equals b/n";
// set & get unsigned long ints
unsigned long int c,d;
c = 1000;
vbs[0].set_value( c); //將vbs[0]的value值設為1000
vbs[0].get_value( d); //用vbs[0]值設y的值
if ( c == d) cout << "c equals d/n";
// set a 64 bit counter
Counter64 c64(1000,1001);
vbs[1].set_value( c64);
// get and set an oid as a value
Oid o1, o2; //定義兩個Oid o1 o2
o1 = "1.2.3.4.5.6";
vbs[2].set_value( o1); //將vbs[2]的value值設為o1即1.2.3.4.5.6
vbs[2].get_value( o2); //將o2的值設為1.2.3.4.5.6
if ( o1 == o2) cout << "o1 equals o2/n";
// set and get an octet string
unsigned char data[4],outdata[4];
unsigned long len,outlen;
len =4; data[0] = 10; data[1] = 12; data[2] = 12; data[3] = 13;
OctetStr octetstr(data,len);
vbs[3].set_value( octetstr);
vbs[3].get_value( octetstr);
// get & set a string
char beer[80]; //定義了一個字串beer
char good_beer[80]; //定義了一個字串good_beer
strcpy( beer,"Sierra Nevada Pale Ale");
cout<<"beer is "<<beer<<endl;
vbs[4].set_value( beer); //將beer的值設定給vbs[4]的value
vbs[4].get_value( good_beer); //又將good_beer的值設定為beer的值
printf("Good Beer = %s/n",good_beer);
// get and set an ip an address
IpAddress ipaddress1, ipaddress2;
ipaddress1 = "10.4.8.5";
vbs[5].set_value( ipaddress1);
vbs[5].get_value( ipaddress2);
cout << "ipaddress2 is "<<ipaddress2<<endl;
} // end vb example
執行結果如圖7所示