一.实验目的
1、基本掌握了 MIB的结构;
2、掌握C++环境下SNMP编程的基本方法。
二.实验环境
1、VC++ 6.0
2、《Visual C++开发基于SNMP网络管理软件》书的第七章内容和源代码,其主要源代码在工程文件的MibBrowserView.cpp中。
三.实验要求
由于MibBrowser.dsw给出的源程序代码中,每次只能得到一个对象的值,因此本实验的要求就是:修改Get和Getnext操作的显示结果,使其能够把一次性把一个OID子树下的所有对象的内容能够显现出来。具体要求如下:
1. 如果OID子树下没有表对象,把该子树下的所有对象的值显示在右侧的表格中。
2. 如果OID子树下有标量对象,也有表对象,把该子树下的所有标量对象的值和表对象中所有值显示在右侧的表格中。标量对象的显示方式采用原来的显示方式,而表对象内容的显示必须是表格,可以采用连接的形式,在现在显示List的右边以表格的形式显示素有所有对象的值。
3. 如果OID子树下只有表对象,把该子树下的所有对象中所有值显示在右侧的表格中,要求采用表格的形式,而不是原来的树形结构。
四.实验报告的内容
//GET操作;
void CMibBrowserView::OnGet()
{
HTREEITEM hNode;
MibNode* pNodeData;
CString ipadd,community,oidstr;
m_ipadd.GetWindowText(ipadd);
if(m_ipadd.IsBlank())
{
AfxMessageBox("IP地址错误!");
return ;
}
m_ipadd.GetWindowText(ipadd);
m_community.GetWindowText(community);
m_oid.GetWindowText(oidstr);
ipadd+=":161";
Snmp::socket_startup();
UdpAddress address((LPCTSTR)ipadd);
Oid oid((LPCTSTR)oidstr);
snmp_version version=version1;
int status;
Snmp snmp(status, 0, false);
Pdu pdu;
Vb vb;
vb.set_oid(oid);
pdu += vb;
CTarget ctarget(address);
ctarget.set_version(version);
ctarget.set_retry(1);
ctarget.set_timeout(100);
ctarget.set_readcommunity((LPCTSTR)community);
SnmpTarget *target;
target = &ctarget;
status = snmp.get(pdu, *target);
if (status ==SNMP_CLASS_SUCCESS)
{
pdu.get_vb(vb,0);
CString reply_oid=vb.get_printable_oid();
CStringreply_value=vb.get_printable_value();
hNode=SearchNode(reply_oid);
if (hNode!=NULL)
{
pNodeData=(MibNode*)m_tree.GetItemData(hNode);
reply_oid.Replace((LPCTSTR)pNodeData->POid,
(LPCTSTR)m_tree.GetItemText(hNode));
if (pNodeData->PInteger!=NULL)
{
POSITIONindex=pNodeData->PInteger->Find(reply_value);
if (index!=NULL)
{
pNodeData->PInteger->GetNext(index);
reply_value=pNodeData->PInteger->GetNext(index);
}
}
}
if (m_list.GetItemCount()>0)
m_list.DeleteAllItems();
introw=m_list.InsertItem(1,reply_oid);
m_list.SetItemText(row,1,reply_value);
}
Snmp::socket_cleanup();
CMainFrame *pF=(CMainFrame *)AfxGetMainWnd();
int num=m_list.GetItemCount();
oidstr.Format("%d",num);
oidstr="共取回 "+oidstr+" 个对象";
pF->m_wndStatusBar.SetPaneText(0,oidstr);
}
//GETNEXT操作:
void CMibBrowserView::OnGetnext()
{
HTREEITEM hNode;
MibNode* pNodeData;
CString ipadd,community,oidstr;
m_ipadd.GetWindowText(ipadd);
if(m_ipadd.IsBlank())
{
AfxMessageBox("IP地址错误!");
return ;
}
m_ipadd.GetWindowText(ipadd);
ipadd+=":161";
m_community.GetWindowText(community);
m_oid.GetWindowText(oidstr);
Snmp::socket_startup();
UdpAddress address((LPCTSTR)ipadd);
Oid oid((LPCTSTR)oidstr);
snmp_version version=version1;
int status;
Snmp snmp(status, 0, false);
Pdu pdu;
Vb vb;
vb.set_oid(oid);
pdu += vb;
CTarget ctarget( address);
ctarget.set_version(version);
ctarget.set_retry(1);
ctarget.set_timeout(100);
ctarget.set_readcommunity((LPCTSTR)community);
SnmpTarget *target;
target = &ctarget;
status = snmp.get_next(pdu,*target);
if (status ==SNMP_CLASS_SUCCESS)
{
pdu.get_vb(vb,0);
CString reply_oid=vb.get_printable_oid();
CStringreply_value=vb.get_printable_value();
hNode=SearchNode(reply_oid);
if (hNode!=NULL)
{
pNodeData=(MibNode*)m_tree.GetItemData(hNode);
reply_oid.Replace((LPCTSTR)pNodeData->POid,
(LPCTSTR)m_tree.GetItemText(hNode));
if(pNodeData->PInteger!=NULL)
{
POSITIONindex=pNodeData->PInteger->Find(reply_value);
if (index!=NULL)
{
pNodeData->PInteger->GetNext(index);
reply_value=pNodeData->PInteger->GetNext(index);
}
}
}
if (m_list.GetItemCount()>0)
m_list.DeleteAllItems();
introw=m_list.InsertItem(1,reply_oid);
m_list.SetItemText(row,1,reply_value);
}
else
AfxMessageBox("操作失败,请检查配置!");
Snmp::socket_cleanup();
CMainFrame *pF=(CMainFrame *)AfxGetMainWnd();
int num=m_list.GetItemCount();
oidstr.Format("%d",num);
oidstr="共取回 "+oidstr+" 个对象";
pF->m_wndStatusBar.SetPaneText(0,oidstr);
}
五.参考资料
Visual C++开发基于SNMP网络管理软件(含盘),作者: 任相臣,徐䶮,武孟军,
出版社: 人民邮电出版社,2007
|