📄 snmptools.h
字号:
#ifndef SNMPTOOLS
#define SNMPTOOLS
#include <snmp.h>
#include <mgmtapi.h>
#pragma comment (lib,"Snmpapi")
#pragma comment (lib,"Mgmtapi")
class SnmpTool
{
public :
char output[12][3*4+3+1];
short which;
SnmpTool(ULONG DestIP,char* CommunityName )
{
which=0;
this->Error=false;
this->DestIP=DestIP;
sprintf(this->CommunityName,CommunityName);
CString DestIPAddr;
DestIPAddr.Format("%s",this->NetIpToString(this->DestIP));
this->m_lpMgrSession=SnmpMgrOpen(DestIPAddr.GetBuffer(),this->CommunityName,1000,3);
if(this->m_lpMgrSession==NULL)
{
this->Error=true;
}
if(SnmpMgrStrToOid(".1.3.6.1.2.1.4.20.1.1",&(this->IpTable))==0||
SnmpMgrStrToOid(".1.3.6.1.2.1.4.21.1.7",&(this->NextHop))==0||
SnmpMgrStrToOid("..1.3.6.1.2.1.2.2.1.5",&(this->EthSpeed))==0||
SnmpMgrStrToOid(".1.3.6.1.2.1.4.20.1.3",&(this->NetMask))==0||
SnmpMgrStrToOid(".1.3.6.1.2.1.2.2.1.2",&(this->EthDescription))==0)
{
this->Error=true;
}
}
bool GetEnterfaceSpeed(ULONG*Tab,int &Length)
{
if(this->Error)
{
return false;
}
AsnInteger errorStatus=0; // Error type that is returned if encountered
AsnInteger errorIndex=0; // Works with variable above
SnmpVarBindList snmpVarList;
snmpVarList.list = NULL;
snmpVarList.len = 0;
snmpVarList.len++;
snmpVarList.list = (SnmpVarBind *)SNMP_realloc(snmpVarList.list, sizeof(SnmpVarBind)*snmpVarList.len);
if(snmpVarList.list==NULL)
{
return false;
}
SnmpUtilOidCpy(&snmpVarList.list[0].name,&(this->EthSpeed));
snmpVarList.list[0].value.asnType = ASN_NULL;
ULONG SpeedTable[256];
int Len=0;
int Fault=0;
while(1)
{
if(SnmpMgrRequest(m_lpMgrSession,SNMP_PDU_GETNEXT,&snmpVarList,&errorStatus,&errorIndex)==0)
{
Fault++;
if(Fault==3)
{
return false;
}
}
if(SnmpUtilOidNCmp(&snmpVarList.list[0].name,&(this->EthSpeed),this->EthSpeed.idLength))
{
break;
}
if(snmpVarList.list[0].value.asnType!=ASN_RFC1155_GAUGE)
{
return false;
}
else
{
SpeedTable[Len]=snmpVarList.list[0].value.asnValue.gauge;
}
Len++;
if(Len==Length)
{
return false;
}
}
SnmpUtilOidFree(&snmpVarList.list[0].name); //释放SnmpUtilOidCpy调用时分配的空间
SNMP_free(snmpVarList.list); //释放SNMP_realloc分配的空间
Length=Len;
for (int i=0;i<Len;i++)
{
Tab[i]=SpeedTable[i];
}
return true;
}
bool GetEnterfaceDescription(CString *Tab,int &Length)
{
if(this->Error)
{
return false;
}
AsnInteger errorStatus=0; // Error type that is returned if encountered
AsnInteger errorIndex=0; // Works with variable above
SnmpVarBindList snmpVarList;
snmpVarList.list = NULL;
snmpVarList.len = 0;
snmpVarList.len++;
snmpVarList.list = (SnmpVarBind *)SNMP_realloc(snmpVarList.list, sizeof(SnmpVarBind)*snmpVarList.len);
if(snmpVarList.list==NULL)
{
return false;
}
SnmpUtilOidCpy(&snmpVarList.list[0].name,&(this->EthDescription));
snmpVarList.list[0].value.asnType = ASN_NULL;
CString DesTable[256];
int Len=0;
int Fault=0;
while(1)
{
if(SnmpMgrRequest(m_lpMgrSession,SNMP_PDU_GETNEXT,&snmpVarList,&errorStatus,&errorIndex)==0)
{
Fault++;
if(Fault==3)
{
return false;
}
}
if(SnmpUtilOidNCmp(&snmpVarList.list[0].name,&(this->EthDescription),this->EthDescription.idLength))
{
break;
}
if(snmpVarList.list[0].value.asnType!=ASN_OCTETSTRING)
{
return false;
}
else
{
if(snmpVarList.list[0].value.asnValue.string.length==0)
{
return false;
}
else
{
for(UINT k=0;k<snmpVarList.list[0].value.asnValue.string.length;k++)
{
DesTable[Len].AppendChar(snmpVarList.list[0].value.asnValue.string.stream[k]);
}
DesTable[Len].Append("\0");
}
}
Len++;
if(Len==Length)
{
return false;
}
}
SnmpUtilOidFree(&snmpVarList.list[0].name); //释放SnmpUtilOidCpy调用时分配的空间
SNMP_free(snmpVarList.list); //释放SNMP_realloc分配的空间
Length=Len;
for (int i=0;i<Len;i++)
{
Tab[i]=DesTable[i];
}
return true;
}
bool GetIpTable(ULONG *Tab,int &Length)
{
if(this->Error)
{
return false;
}
AsnInteger errorStatus=0; // Error type that is returned if encountered
AsnInteger errorIndex=0; // Works with variable above
SnmpVarBindList snmpVarList;
snmpVarList.list = NULL;
snmpVarList.len = 0;
snmpVarList.len++;
snmpVarList.list = (SnmpVarBind *)SNMP_realloc(snmpVarList.list, sizeof(SnmpVarBind)*snmpVarList.len);
if(snmpVarList.list==NULL)
{
return false;
}
SnmpUtilOidCpy(&snmpVarList.list[0].name,&(this->IpTable));
snmpVarList.list[0].value.asnType = ASN_NULL;
ULONG IpTable[256];
int Len=0;
int Fault=0;
while(1)
{
if(SnmpMgrRequest(m_lpMgrSession,SNMP_PDU_GETNEXT,&snmpVarList,&errorStatus,&errorIndex)==0)
{
Fault++;
if(Fault==3)
{
return false;
}
}
if(SnmpUtilOidNCmp(&snmpVarList.list[0].name,&(this->IpTable),this->IpTable.idLength))
{
break;
}
if(snmpVarList.list[0].value.asnType!=ASN_RFC1155_IPADDRESS)
{
return false;
}
else
{
if(snmpVarList.list[0].value.asnValue.address.length!=4)
{
return false;
}
else
{
ULONG Tmp=(*(ULONG*)(snmpVarList.list[0].value.asnValue.address.stream));//+((ULONG)(snmpVarList.list[0].value.asnValue.address.stream[1]))+((ULONG)(snmpVarList.list[0].value.asnValue.address.stream[2]))+((ULONG)(snmpVarList.list[0].value.asnValue.address.stream[3]));
if(/*Tmp==inet_addr("127.0.0.1")||*/Tmp==0)
{
continue ;
}
else
{
IpTable[Len]=Tmp;
}
}
}
Len++;
if(Len==Length)
{
return false;
}
}
SnmpUtilOidFree(&snmpVarList.list[0].name); //释放SnmpUtilOidCpy调用时分配的空间
SNMP_free(snmpVarList.list); //释放SNMP_realloc分配的空间
Length=Len;
for (int i=0;i<Len;i++)
{
Tab[i]=IpTable[i];
}
return true;
}
bool GetNetMaskTable(ULONG *Tab,int &Length)
{
if(this->Error)
{
return false;
}
AsnInteger errorStatus=0; // Error type that is returned if encountered
AsnInteger errorIndex=0; // Works with variable above
SnmpVarBindList snmpVarList;
snmpVarList.list = NULL;
snmpVarList.len = 0;
snmpVarList.len++;
snmpVarList.list = (SnmpVarBind *)SNMP_realloc(snmpVarList.list, sizeof(SnmpVarBind)*snmpVarList.len);
if(snmpVarList.list==NULL)
{
return false;
}
SnmpUtilOidCpy(&snmpVarList.list[0].name,&(this->NetMask));
snmpVarList.list[0].value.asnType = ASN_NULL;
ULONG IpTable[256];
int Len=0;
int Fault=0;
while(1)
{
if(SnmpMgrRequest(m_lpMgrSession,SNMP_PDU_GETNEXT,&snmpVarList,&errorStatus,&errorIndex)==0)
{
Fault++;
if(Fault==3)
{
return false;
}
}
if(SnmpUtilOidNCmp(&snmpVarList.list[0].name,&(this->NetMask),this->NetMask.idLength))
{
break;
}
if(snmpVarList.list[0].value.asnType!=ASN_RFC1155_IPADDRESS)
{
return false;
}
else
{
if(snmpVarList.list[0].value.asnValue.address.length!=4)
{
return false;
}
else
{
ULONG Tmp=(*(ULONG*)(snmpVarList.list[0].value.asnValue.address.stream));//+((ULONG)(snmpVarList.list[0].value.asnValue.address.stream[1]))+((ULONG)(snmpVarList.list[0].value.asnValue.address.stream[2]))+((ULONG)(snmpVarList.list[0].value.asnValue.address.stream[3]));
IpTable[Len]=Tmp;
}
}
Len++;
if(Len==Length)
{
return false;
}
}
SnmpUtilOidFree(&snmpVarList.list[0].name); //释放SnmpUtilOidCpy调用时分配的空间
SNMP_free(snmpVarList.list); //释放SNMP_realloc分配的空间
Length=Len;
for (int i=0;i<Len;i++)
{
Tab[i]=IpTable[i];
}
return true;
}
bool GetNextHopTable(ULONG *Tab,int &Length)
{
if(this->Error)
{
return false;
}
AsnInteger errorStatus=0; // Error type that is returned if encountered
AsnInteger errorIndex=0; // Works with variable above
SnmpVarBindList snmpVarList;
snmpVarList.list = NULL;
snmpVarList.len = 0;
snmpVarList.len++;
snmpVarList.list = (SnmpVarBind *)SNMP_realloc(snmpVarList.list, sizeof(SnmpVarBind)*snmpVarList.len);
if(snmpVarList.list==NULL)
{
return false;
}
SnmpUtilOidCpy(&snmpVarList.list[0].name,&(this->NextHop));
snmpVarList.list[0].value.asnType = ASN_NULL;
ULONG IpTable[256];
int Len=0;
int Fault=0;
while(1)
{
if(SnmpMgrRequest(m_lpMgrSession,SNMP_PDU_GETNEXT,&snmpVarList,&errorStatus,&errorIndex)==0)
{
Fault++;
if(Fault==3)
{
return false;
}
}
if(SnmpUtilOidNCmp(&snmpVarList.list[0].name,&(this->NextHop),this->NextHop.idLength))
{
break;
}
if(snmpVarList.list[0].value.asnType!=ASN_RFC1155_IPADDRESS)
{
return false;
}
else
{
if(snmpVarList.list[0].value.asnValue.address.length!=4)
{
return false;
}
else
{
ULONG Tmp=(*(ULONG*)(snmpVarList.list[0].value.asnValue.address.stream));//+((ULONG)(snmpVarList.list[0].value.asnValue.address.stream[1]))+((ULONG)(snmpVarList.list[0].value.asnValue.address.stream[2]))+((ULONG)(snmpVarList.list[0].value.asnValue.address.stream[3]));
if(Tmp==inet_addr("127.0.0.1")||Tmp==0)
{
continue ;
}
else
{
IpTable[Len]=Tmp;
}
}
}
Len++;
if(Len==Length)
{
return false;
}
}
SnmpUtilOidFree(&snmpVarList.list[0].name); //释放SnmpUtilOidCpy调用时分配的空间
SNMP_free(snmpVarList.list); //释放SNMP_realloc分配的空间
Length=Len;
for (int i=0;i<Len;i++)
{
Tab[i]=IpTable[i];
}
return true;
}
~SnmpTool()
{
SnmpMgrClose(this->m_lpMgrSession);
}
char *NetIpToString(u_long in)
{
u_char *p;
p = (u_char *)∈
which = (which + 1 == 12 ? 0 : which + 1);
sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
return output[which];
}
private:
AsnObjectIdentifier IpTable;
AsnObjectIdentifier NextHop;
AsnObjectIdentifier EthDescription;
AsnObjectIdentifier EthSpeed;
AsnObjectIdentifier NetMask;
char CommunityName[64];
ULONG DestIP;
LPSNMP_MGR_SESSION m_lpMgrSession;
bool Error;
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -