📄 mibbrowserview.cpp
字号:
Line=Line.Left(Line.Find("--"));
Line.TrimLeft();
Line.TrimRight();
Line1=Line1+' '+Line;
if (Line.Find('}')!=-1) break;
}
}
pNode->PIndex=Line1;
IndexString=Line1;
continue;
}
if ((Line.Find("DESCRIPTION")==0))
{
CString Line1;
while (MibFile.ReadString(Line))
{
if (Line.GetLength()==0) continue;
if (Line.Find("--")==0) continue;
if (Line.Find("--")>0)
Line=Line.Left(Line.Find("--"));
Line.TrimLeft();
Line.TrimRight();
Line1+=Line;
//第一个字符为‘“ ’,从第3个字符开始判断;
if (Line.Find('"',2)>2) break;
}
pNode->PDescr=Line1;
continue;
}
//最后一行,退出;
if ((Line.Find("::=")==0)&&(Line.Find('}')>3)&&(Line.Find('{')>1))
{
int n1=Line.Find('{');
int n2=Line.Find('}')-n1-1;
Line=Line.Mid(n1+1,n2);
Line.TrimLeft();
Line.TrimRight();
ParentName=Line.Left(Line.Find(' '));
Pos=Line.Mid(Line.Find(' ')+1);
AddObject(HRoot,ParentName,NodeName,Pos,pNode);
break;
}
}// while (MibFile.ReadString(Line))
continue;
}
}//while (MibFile.ReadString(Line))
MibFile.Close();
return true;
}
//使用递归算法,遍历整个以hSubRoot为根节点的子树,查找是否存在label为&text
//的节点,如存在,返回该节点的句柄,否则返回NULL;
HTREEITEM CMibBrowserView::FindNode(HTREEITEM hSubRoot,const CString &text)
{
if (hSubRoot==NULL)
return NULL;
//判断是否符合条件,如是,返回该节点的举柄;
if (m_tree.GetItemText(hSubRoot)==text)
return hSubRoot;
hSubRoot=m_tree.GetChildItem(hSubRoot);
do
{
HTREEITEM ht;
//递归调用FindNode;
if ((ht=FindNode(hSubRoot,text)) !=NULL)
return ht;
} while ((hSubRoot=m_tree.GetNextSiblingItem(hSubRoot)) !=NULL);
return NULL;
}
//初始化一个结构数据,所有指针设置为NULL;
void CMibBrowserView::InitNodeData(MibNode *Pnode)
{
Pnode->PSnytax=' ';
Pnode->PAccess=' ';
Pnode->PDescr=' ';
Pnode->PIndex=' ';
Pnode->PStatus=' ';
Pnode->POid=' ';
Pnode->PInteger=NULL;
}
void CMibBrowserView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
//增加的代码;
CurrDir=_pgmptr;
CurrDir.MakeReverse();
int path=CurrDir.Find('\\');
CurrDir.MakeReverse();
CurrDir=CurrDir.Left(CurrDir.GetLength()-path);
//初始化列表控件;
m_ipadd.SetAddress(127,0,0,1);
CString pass="public";
m_community.SetWindowText(pass);
pass="1.3.6.1.2.1.1.1.0";
m_oid.SetWindowText(pass);
m_list.InsertColumn(0," OID",LVCFMT_RIGHT,180,0);
m_list.InsertColumn(1," Value",LVCFMT_LEFT,320,1);
m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
//初始化,装载图标;
m_iImageList.Create(16, 16, TRUE,1, 0);
HICON hIcon = NULL;
//0,root;
hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_ROOT1), IMAGE_ICON, 32, 32, 0);
m_iImageList.Add(hIcon);
//1,closed box;
hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_CLOSE_BOX), IMAGE_ICON, 16, 16, 0);
m_iImageList.Add(hIcon);
//2,opened box;
hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_OPEN_BOX), IMAGE_ICON, 16, 16, 0);
m_iImageList.Add(hIcon);
//3,leaf node
hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_LEAF), IMAGE_ICON, 16, 16, 0);
m_iImageList.Add(hIcon);
//4,key node
hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_KEY), IMAGE_ICON, 16, 16, 0);
m_iImageList.Add(hIcon);
//5,entry;
hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_ENTRY), IMAGE_ICON, 16, 16, 0);
m_iImageList.Add(hIcon);
//6,table;
hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_TABLE), IMAGE_ICON, 16, 16, 0);
m_iImageList.Add(hIcon);
m_tree.SetImageList ( &m_iImageList,TVSIL_NORMAL );
//增加根节点;
MibNode* pNode;
HRoot= m_tree.InsertItem("MIB",0,0,NULL);
pNode=new MibNode;
InitNodeData(pNode);
pNode->POid="1.3.6.1";
pNode->PAccess="not-accessable";
//增加几个主要辅助节点;
//mgmt
m_tree.SetItemData(HRoot,(DWORD)pNode);
HSecond= m_tree.InsertItem("mgmt",1,1,HRoot);
pNode=new MibNode;
InitNodeData(pNode);
pNode->POid="1.3.6.1.2";
pNode->PAccess="not-accessable";
m_tree.SetItemData(HSecond,(DWORD)pNode);
//private;
HSecond= m_tree.InsertItem("private",1,1,HRoot);
pNode=new MibNode;
InitNodeData(pNode);
pNode->POid="1.3.6.1.4";
pNode->PAccess="not-accessable";
m_tree.SetItemData(HSecond,(DWORD)pNode);
//enterprises;
HSecond= m_tree.InsertItem("enterprises",1,1,HSecond);
pNode=new MibNode;
InitNodeData(pNode);
pNode->POid="1.3.6.1.4.1";
pNode->PAccess="not-accessable";
m_tree.SetItemData(HSecond,(DWORD)pNode);
//cisco
HSecond= m_tree.InsertItem("cisco",1,1,HSecond);
pNode=new MibNode;
InitNodeData(pNode);
pNode->POid="1.3.6.1.4.1.9";
pNode->PAccess="not-accessable";
m_tree.SetItemData(HSecond,(DWORD)pNode);
//ciscomgmt
HSecond=m_tree.InsertItem("ciscoMgmt",1,1,HSecond);
pNode=new MibNode;
InitNodeData(pNode);
pNode->POid="1.3.6.1.4.1.9.9";
pNode->PAccess="not-accessable";
m_tree.SetItemData(HSecond,(DWORD)pNode);
//microsoft
HSecond=m_tree.InsertItem("microsoft",
1,1,m_tree.GetParentItem(m_tree.GetParentItem(HSecond)));
pNode=new MibNode;
InitNodeData(pNode);
pNode->POid="1.3.6.1.4.1.311";
pNode->PAccess="not-accessable";
m_tree.SetItemData(HSecond,(DWORD)pNode);
//打开OID文件,将用户增加的辅助(顶端)节点增加的MIB树中;
//每个顶端节点占3行,第一行为父节点名,第二行为辅助节点名,
//第三行为它的OID;
CString tempfile=CurrDir+"mib/oid.txt";
if (MibFile.Open((LPCTSTR)tempfile,CFile::modeRead)==false)
{
AfxMessageBox("打开oid文件时失败");
return;
}
//逐行读入并处理;
while (MibFile.ReadString(Line))
{
CString n_name,o_name;
Line.TrimLeft();
Line.TrimRight();
if (Line.GetLength()==0) continue;
HTREEITEM hr;
hr=m_tree.GetRootItem();
HTREEITEM hp=FindNode(hr,Line);
//读入节点名;
MibFile.ReadString(Line);
n_name=Line;
//OID;
MibFile.ReadString(Line);
o_name=Line;
if (hp!=NULL)
{
//插入新节点并设置data项;
hp=m_tree.InsertItem((LPCTSTR)n_name,1,1,hp);
MibNode *pNode=new MibNode;
InitNodeData(pNode);
pNode->POid=o_name;
pNode->PAccess="not-accessable";
m_tree.SetItemData(hp,(DWORD)pNode);
}
}
MibFile.Close();
//装载MIB,所有已装载MIB文件以txt格式保存在当前目录的mib子目
//录下,搜索目录中所有MIB文件并依次装载。
tempfile=CurrDir+"mib/*.txt";
CFileFind fileread;
BOOL nextfile=fileread.FindFile((LPCTSTR)tempfile);
while (nextfile)
{
nextfile=fileread.FindNextFile();
//跳过oid.txt文件;
if (fileread.GetFileName()=="oid.txt") continue;
tempfile=fileread.GetFilePath();
//调用函数装载MIB文件;
if (LoadMib(tempfile)==false)
AfxMessageBox("装载文件出错!");
}
}
/////////////////////////////////////////////////////////////////////////////
// CMibBrowserView printing
BOOL CMibBrowserView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMibBrowserView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMibBrowserView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CMibBrowserView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CMibBrowserView diagnostics
#ifdef _DEBUG
void CMibBrowserView::AssertValid() const
{
CFormView::AssertValid();
}
void CMibBrowserView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CMibBrowserDoc* CMibBrowserView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMibBrowserDoc)));
return (CMibBrowserDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMibBrowserView message handlers
//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();
CString reply_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)
{
POSITION index=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();
int row=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);
}
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();
CString reply_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)
{
POSITION index=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();
int row=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);
}
void CMibBrowserView::OnGetsubtree()
{
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 Baseoid((LPCTSTR)oidstr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -