📄 mibbrowserview.cpp
字号:
snmp_version version=version1;
int status;
Snmp snmp(status, 0, false);
Pdu pdu;
Vb vb;
vb.set_oid(Baseoid);
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;
Oid NextOid(Baseoid);
NextOid+=".1";
int number=1;
if (m_list.GetItemCount()>0)
m_list.DeleteAllItems();
CString reply_oid;
CString reply_value;
MibNode* pNodeData;
HTREEITEM hNode=NULL;
//bool seek=true;
while (Baseoid.nCompare(Baseoid.len(),NextOid)==0)
{
status = snmp.get_next(pdu, *target);
if (status == SNMP_CLASS_SUCCESS)
{
pdu.get_vb(vb,0);
reply_oid=vb.get_printable_oid();
reply_value=vb.get_printable_value();
vb.get_oid(NextOid);
if (Baseoid.nCompare(Baseoid.len(),NextOid)==0)
{
hNode=SearchNode(reply_oid);
if (hNode!=NULL)
{
pNodeData=(MibNode*)m_tree.GetItemData(hNode);
if (reply_oid.Find((LPCTSTR)pNodeData->POid)==0)
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);
}
}
}
//在列表中显示;
int row=m_list.InsertItem(number,reply_oid);
m_list.SetItemText(row,1,reply_value);
number++;
}
}
vb.set_oid(NextOid);
vb.set_null();
pdu.set_vb(vb,0);
}
Snmp::socket_cleanup();
CMainFrame *pF=(CMainFrame *)AfxGetMainWnd();
int num=m_list.GetItemCount();
oidstr.Format("%d",num);
oidstr="共取回 "+oidstr+" 个对象";
pF->m_wndStatusBar.SetPaneText(0,oidstr);
}
//遍历MIB的函数;
void CMibBrowserView::OnWalk()
{
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);
snmp_version version=version1;
int status;
Snmp snmp(status, 0, false);
Pdu pdu;
Vb vb;
vb.set_oid(BaseOid);
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;
Oid NextOid(BaseOid);
int number=1;
if (m_list.GetItemCount()>0)
m_list.DeleteAllItems();
CString reply_oid;
CString reply_value;
MibNode* pNodeData;
HTREEITEM hNode=NULL;
// bool Seek=true;
do
{
BaseOid=NextOid;
status = snmp.get_next(pdu, *target);
if (status == SNMP_CLASS_SUCCESS)
{
pdu.get_vb(vb,0);
reply_oid=vb.get_printable_oid();
reply_value=vb.get_printable_value();
hNode=SearchNode(reply_oid);
if (hNode!=NULL)
{
pNodeData=(MibNode*)m_tree.GetItemData(hNode);
if (reply_oid.Find((LPCTSTR)pNodeData->POid)==0)
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);
}
}
}
vb.get_oid(NextOid);
int row=m_list.InsertItem(number,reply_oid);
m_list.SetItemText(row,1,reply_value);
number++;
}
vb.set_oid(NextOid);
vb.set_null();
pdu.set_vb(vb,0);
} while (BaseOid!=NextOid) ;
Snmp::socket_cleanup();
CMainFrame *pF=(CMainFrame *)AfxGetMainWnd();
int num=m_list.GetItemCount();
oidstr.Format("%d",num);
oidstr="共取回 "+oidstr+" 个对象";
pF->m_wndStatusBar.SetPaneText(0,oidstr);
}
//set;
void CMibBrowserView::OnSet()
{
CString ipadd,community,oidstr,n_value;
int n_type;
CSetDig set;
if(set.DoModal()==IDOK)
{
n_value=set.m_value;
n_type=set.m_type;
if ((n_value.GetLength()==0)||(n_type==-1))
{
AfxMessageBox("未选择对象类型或值!");
return;
}
}
m_ipadd.GetWindowText(ipadd);
if(m_ipadd.IsBlank())
{
AfxMessageBox("IP地址错误!");
return ;
}
Pdu pdu;
Vb vb;
switch (n_type)
{
case 0:
{
unsigned long i;
i = atol((LPCTSTR)n_value);
long l;
l = (long) i;
vb.set_value(l);
}
case 1:
{
OctetStr octetstr((LPCTSTR)n_value);
if ( octetstr.valid())
vb.set_value(octetstr);
}
case 2:
{
Oid oid1((LPCTSTR)n_value);
if ( oid1.valid())
vb.set_value(oid1);
}
}
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);
vb.set_oid(oid);
snmp_version version=version1;
pdu += vb;
CTarget ctarget(address);
ctarget.set_version(version);
ctarget.set_retry(1);
ctarget.set_timeout(100);
ctarget.set_writecommunity((LPCTSTR)community);
SnmpTarget *target;
target = &ctarget;
int status;
Snmp snmp(status, 0, false);
if ( status != SNMP_CLASS_SUCCESS)
{
AfxMessageBox("SNMP++ Session Create Failed");
return ;
}
status=snmp.set(pdu, *target);
if ( status == SNMP_CLASS_SUCCESS)
AfxMessageBox("设置操作成功!");
else
AfxMessageBox(snmp.error_msg(status));
Snmp::socket_cleanup(); // Shut down socket subsystem
}
//增加的事件处理函数,在程序退出时销毁动态生成的对象;
void CMibBrowserView::OnDestroy()
{
CFormView::OnDestroy();
HRoot=m_tree.GetRootItem();//从根节点开始;
travl(HRoot);//遍历整个树;
}
//在节点展开与闭合时改变节点图标;
void CMibBrowserView::OnItemexpandedTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
HTREEITEM hnode=pNMTreeView->itemNew.hItem;
//不是叶子节点和根节点时才改变图标;
if ((m_tree.ItemHasChildren(hnode))&&(m_tree.GetRootItem()!=hnode))
{
if (pNMTreeView->itemNew.iImage==1)
m_tree.SetItemImage(hnode,2,2);
if (pNMTreeView->itemNew.iImage==2)
m_tree.SetItemImage(hnode,1,1);
}//增加的代码
*pResult = 0;
}
void CMibBrowserView::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
HTREEITEM hnode=pNMTreeView->itemNew.hItem;
//二级节点,MIB文件名,返回;
// if (m_tree.GetParentItem(hnode)==m_tree.GetRootItem())
// return;
//不是根节点,显示节点详细信息;
if (m_tree.GetRootItem()!=hnode)
{
MibNode *pMibNode;
//获取节点data指针;
pMibNode=(MibNode *)m_tree.GetItemData(hnode);
CString str;
//获取节点语法信息;
str="SYNTAX: "+pMibNode->PSnytax+"\r\n";
CString str1;
//如果是枚举型整数,获取保存在PInteger中的信息;
if (pMibNode->PInteger!=NULL)
{
POSITION pos;
CString str2;
//遍历整个列表,第一项为数字,紧随的一项是数字的意义解释;
for(pos=pMibNode->PInteger->GetHeadPosition(); pos!=NULL;)
{
str2=pMibNode->PInteger->GetNext(pos)+" : ";
str2=str2+pMibNode->PInteger->GetNext(pos)+"\r\n";
str1=str1+str2;
}
}
str+=str1;
str=str+"ACCESS:"+pMibNode->PAccess+"\r\n";
str=str+"STATUS:"+pMibNode->PStatus+"\r\n";
//如果有INDEX信息,取出;
if (pMibNode->PIndex!=' ')
str=str+"INDEX:"+pMibNode->PIndex+"\r\n";
str=str+"OID:"+pMibNode->POid+"\r\n";
str=str+"DESCRPTION:"+pMibNode->PDescr+"\r\n";
//在编辑控件中显示;
m_edit1=((LPCTSTR)str);
m_oid.SetWindowText(pMibNode->POid);
UpdateData(FALSE);
}
//以上是增加的代码
*pResult = 0;
}
void CMibBrowserView::OnLoad()
{
// TODO: Add your command handler code here
CString filename,Dfilename;
CFileDialog openfile(TRUE,NULL," *.txt",OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST,
"MIB files");
openfile.DoModal();
Dfilename=openfile.GetFileName();
filename=openfile.GetPathName();
if (LoadMib(filename)==false)
AfxMessageBox("装载文件 "+filename+" 出错!");
else
{
AfxMessageBox("装载成功!");
Dfilename=CurrDir+"mib/"+Dfilename;
CopyFile((LPCTSTR)filename,(LPCTSTR)Dfilename,false);
}
}
//注册节点,需要输入父节点,新增加节点以及新节点OID。每次
//注册成功的节点同时写入OID.TXT文件,下次程序启动时候读入;
void CMibBrowserView::OnOid()
{
// TODO: Add your command handler code here
CString parent;
CString name,oidstr;
CDlg_input inputD;
inputD.m_ParentName="enterprises";
if (inputD.DoModal()==IDOK)
{
parent=inputD.m_ParentName;
name=inputD.m_NodeName;
oidstr=inputD.m_NodeOid;
oidstr="1.3.6.1.4.1."+oidstr;
HTREEITEM hp,hr;
hr=m_tree.GetRootItem();
hp=FindNode(hr,parent);
if (hp!=NULL)
{
hr=m_tree.InsertItem((LPCTSTR)name,1,1,hp);
MibNode* pNode;
pNode=new MibNode;
InitNodeData(pNode);
pNode->POid=oidstr;
pNode->PAccess="not-accessable";
m_tree.SetItemData(hr,(DWORD)pNode);
AfxMessageBox("注册成功!");
CStdioFile OidFile;
if(OidFile.Open("mib/oid.txt",CFile::modeWrite))
{
OidFile.SeekToEnd();
OidFile.WriteString(parent+"\r\n");
OidFile.WriteString(name+"\r\n");
OidFile.WriteString(oidstr+"\r\n");
OidFile.Close();
}
else
AfxMessageBox("打开文件oid.txt错误!");
}
}
}
void CMibBrowserView::OnSave()
{
// TODO: Add your command handler code here
CStdioFile kvFile;
CString line;
CFileDialog openfile(FALSE,"*.txt","mib",
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"Text files(*.txt)|*.txt");
if (openfile.DoModal()==IDOK)
{
if(kvFile.Open(openfile.GetPathName(),
CFile::modeWrite|CFile::modeCreate))
{
int num=m_list.GetItemCount();
for (int i=0;i<num;++i)
{
line=m_list.GetItemText(i,0);
line=line+"="+m_list.GetItemText(i,1)+"\r\n";
kvFile.WriteString(line);
}
}
kvFile.Close();
}
}
void CMibBrowserView::OnUserman()
{
// TODO: Add your command handler code here
//CUsers ddd;
// curUser.GetUser_type();
// if (User_type == 2)
if (curUser.GetUser_type() == 2)
//if (ddd.GetUser_type() == 2)
{ //普通用户不能管理用户信息
MessageBox("没有权限");
return;
}
CUserManDlg dlg;
dlg.DoModal();
}
// if (curUser.GetUser_type() == 2)
// { //普通用户不能管理用户信息
// MessageBox("没有权限");
// return;
// }
// CUserManDlg dlg;
// dlg.DoModal();
void CMibBrowserView::OnPwdman()
{
// TODO: Add your command handler code here
CChangePwdDlg dlg;
dlg.m_UserName = curUser.GetUserName();
dlg.DoModal();
}
// void CHrSysDlg::OnPwdman()
// {
// // TODO: Add your command handler code here
// CChangePwdDlg dlg;
// dlg.m_UserName = curUser.GetUserName();
// dlg.DoModal();
// }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -