一.實驗目的
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
|