⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmp探测代码.txt

📁 可以对黑客编程有一定的了解
💻 TXT
📖 第 1 页 / 共 3 页
字号:

status = SnmpSetRetry(pSendInfo->hManagerEntity, pSendInfo->dwRetransmitCont); 

if(status == SNMPAPI_FAILURE) 

{ 

lMsg("设置管理者重试次数SnmpSetRetry时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//设置管理者超时时间 

status = SnmpSetTimeout(pSendInfo->hManagerEntity, pSendInfo->dwTimeOut); 

if(status == SNMPAPI_FAILURE) 

{ 

lMsg("设置管理者超时时间SnmpSetTimeout时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//设置代理重试次数 

status = SnmpSetRetry(pSendInfo->hAgentEntity, pSendInfo->dwRetransmitCont); 

if(status == SNMPAPI_FAILURE) 

{ 

lMsg("设置代理重试次数SnmpSetRetry时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//设置代理超时时间 

status = SnmpSetTimeout(pSendInfo->hAgentEntity, pSendInfo->dwTimeOut); 

if(status == SNMPAPI_FAILURE) 

{ 

lMsg("设置代理超时时间SnmpSetTimeout时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//设置重发模式 

status = SnmpSetRetransmitMode(SNMPAPI_ON); 

//------------------------------------------ 

//如果已经包含了PDU,则直接发送 

if(pSendInfo->hPdu != NULL) 

{ 

// 发送SNMP消息 

status = SnmpSendMsg(hSnmpSession, pSendInfo->hManagerEntity, 

pSendInfo->hAgentEntity, pSendInfo->hViewContext, pSendInfo->hPdu); 

if(status == SNMPAPI_FAILURE) 

{ 

lMsg("发送SNMP消息SnmpSendMsg时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

} 

else 

if(pSendInfo->hVbl != NULL) //如果没有包含PDU句柄,但是有VBL句柄,则做相应的处理 

{ //再发送 

HSNMP_PDU hTempPdu = NULL; 

DWORD dwRequestId = 0; 

//每发一个新的消息时请求ID应该加一 

if(pSendInfo->nPduType != SNMP_PDU_RESPONSE) 

{ 

*pRequestId = *pRequestId + 1; 

} 

// 创建一个PDU 

hTempPdu = SnmpCreatePdu(hSnmpSession, pSendInfo->nPduType, *pRequestId, 

pSendInfo->nErrorStatus, pSendInfo->nErrorIndex, 

pSendInfo->hVbl); 

if(hTempPdu == SNMPAPI_FAILURE) 

{ 

lMsg("创建一个PDU SnmpCreatePdu时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

// 发送SNMP消息 

status = SnmpSendMsg(hSnmpSession, pSendInfo->hManagerEntity, 

pSendInfo->hAgentEntity, pSendInfo->hViewContext, hTempPdu); 

if(status == SNMPAPI_FAILURE) 

{ 

lMsg("发送SNMP消息SnmpSendMsg时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//释放资源 

SnmpFreePdu(hTempPdu); 

} 

else //如果PDU和VBL的句柄都为NULL,则用vblValue中的信息来发送信息 

{ 

if(!pSendInfo->nVblCount) 

{ 

lMsg("没有变量可以被绑定"); 

return SNMPAPI_FAILURE; 

} 

HSNMP_VBL hTempVbl = NULL; 

HSNMP_PDU hTempPdu = NULL; 

//------------------------------------ 

//绑定数据列表 

hTempVbl = SnmpCreateVbl(hSnmpSession, NULL, NULL); 

if(hTempVbl == SNMPAPI_FAILURE) 

{ 

lMsg("绑定数据列表SnmpCreateVbl时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//!!!!!!!!注意!!!!!!!!!!!// 

//使用SnmpSetVb函数做插入操作时要将Index项设置为0!! 

for(int i = 0; i < pSendInfo->nVblCount; i++) 

{ 

SnmpSetVb(hTempVbl, 0, &pSendInfo->vblOid[i], &pSendInfo->vblValue[i]); 

} 

/////////////////////////////////////////////// 

//每发一个新的消息时请求ID应该加一 

if(pSendInfo->nPduType != SNMP_PDU_RESPONSE) 

{ 

*pRequestId = *pRequestId + 1; 

} 

// 创建一个PDU 

hTempPdu = SnmpCreatePdu(hSnmpSession, pSendInfo->nPduType, *pRequestId, 

pSendInfo->nErrorStatus, pSendInfo->nErrorIndex, hTempVbl); 

if(hTempPdu == SNMPAPI_FAILURE) 

{ 

lMsg("创建一个PDU SnmpCreatePdu时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

// 发送SNMP消息 

status = SnmpSendMsg(hSnmpSession, pSendInfo->hManagerEntity, 

pSendInfo->hAgentEntity, 

pSendInfo->hViewContext, hTempPdu); 

if(status == SNMPAPI_FAILURE) 

{ 

lMsg("发送SNMP消息SnmpSendMsg时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//释放资源 

SnmpFreeVbl(hTempVbl); 

SnmpFreePdu(hTempPdu); 

} 

//================================================ 

return SNMPAPI_SUCCESS; 

} 





//---------------------------------- 

//名称:SendSnmpMessage 

//描述:简单的发送Snmp包 

//---------------------------------- 

SNMPAPI_STATUS SendSnmpMessage(HSNMP_SESSION hSnmpSession, CString strManagerIp, 

CString strAgentIp, CString strCommunity, CString strOidNumber[MAX_VBL], 

DWORD dwRetransmitCont, DWORD dwTimeOut, DWORD * pRequestId, 

int nPduType, smiVALUE value[MAX_VBL], int nValueCount, int nErrorStatus, 

int nErrorIndex) 

{ 

HSNMP_VBL hVbl = NULL; 

HSNMP_PDU hPdu = NULL; 

HSNMP_ENTITY hAgentEntity = NULL; 

HSNMP_ENTITY hManagerEntity = NULL; 

HSNMP_CONTEXT hViewContext = NULL; 

smiOCTETS smiCommunity; 

smiOID oid; 



SNMPAPI_STATUS status; 



// Set community 

smiCommunity.len = strCommunity.GetLength(); 

smiCommunity.ptr = (smiLPBYTE) strCommunity.GetBuffer(strCommunity.GetLength()); 

//得到管理实体句柄信息strAgentIp 

hManagerEntity = SnmpStrToEntity(hSnmpSession, strManagerIp); 

if(hManagerEntity == SNMPAPI_FAILURE) 

{ 

lMsg("得到管理实体句柄信息SnmpStrToEntity时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

hAgentEntity = SnmpStrToEntity(hSnmpSession, strAgentIp); 

if(hAgentEntity == SNMPAPI_FAILURE) 

{ 

lMsg("得到代理实体句柄信息SnmpStrToEntity时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//得到SNMP设备上下文信息句柄,管理者可以用它作为snmpSendMsg和snmpRegister的参数 

hViewContext = SnmpStrToContext(hSnmpSession, &smiCommunity); 

if(hViewContext == SNMPAPI_FAILURE) 

{ 

lMsg("SNMP设备上下文信息句柄SnmpStrToContext时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//-------------------------------------------------------------------------------- 

//设置管理者重试次数 

status = SnmpSetRetry(hManagerEntity, dwRetransmitCont); 

if(status == SNMPAPI_FAILURE) 

{ 

lMsg("设置管理者重试次数SnmpSetRetry时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//设置管理者超时时间 

status = SnmpSetTimeout(hManagerEntity, dwTimeOut); 

if(status == SNMPAPI_FAILURE) 

{ 

lMsg("设置管理者超时时间SnmpSetTimeout时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//设置代理重试次数 

status = SnmpSetRetry(hAgentEntity, dwRetransmitCont); 

if(status == SNMPAPI_FAILURE) 

{ 

lMsg("设置代理重试次数SnmpSetRetry时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//设置代理超时时间 

status = SnmpSetTimeout(hAgentEntity, dwTimeOut); 

if(status == SNMPAPI_FAILURE) 

{ 

lMsg("设置代理超时时间SnmpSetTimeout时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//设置重发模式 

status = SnmpSetRetransmitMode(SNMPAPI_ON); 

//--------------------------------------------------------------------------------- 

//绑定数据列表 

hVbl = SnmpCreateVbl(hSnmpSession, NULL, NULL); 

if(hVbl == SNMPAPI_FAILURE) 

{ 

lMsg("绑定数据列表SnmpCreateVbl时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//!!!!!!!!注意!!!!!!!!!!!// 

//使用SnmpSetVb函数做插入操作时要将Index项设置为0!! 

// SnmpSetVb(hVbl, 0, &oid, &value); 

if(nValueCount != 1) 

{ 

for(int i = 0; i < nValueCount; i++) 

{ 

oid.len = 0; 

SnmpStrToOid(strOidNumber[i], &oid); 

SnmpSetVb(hVbl, 0, &oid, &value[i]); 

SnmpFreeDescriptor(SNMP_SYNTAX_OID, (smiLPOPAQUE)&oid); 

} 

} 

/////////////////////////////////////////////// 

//每发一个新的消息时请求ID应该加一 

*pRequestId = *pRequestId + 1; 

// 创建一个PDU 

hPdu = SnmpCreatePdu(hSnmpSession, nPduType, *pRequestId, nErrorStatus, nErrorIndex 

, hVbl); 

if(hPdu == SNMPAPI_FAILURE) 

{ 

lMsg("创建一个PDU SnmpCreatePdu时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

// 发送SNMP消息 

status = SnmpSendMsg(hSnmpSession, hManagerEntity, hAgentEntity, hViewContext, hPdu); 

if(status == SNMPAPI_FAILURE) 

{ 

lMsg("发送SNMP消息SnmpSendMsg时错误!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

// 释放资源 

SnmpFreeVbl(hVbl); 

SnmpFreePdu(hPdu); 

SnmpFreeEntity(hAgentEntity); 

SnmpFreeEntity(hManagerEntity); 

SnmpFreeContext(hViewContext); 



return SNMPAPI_SUCCESS; 

} 



//---------------------------------- 

//名称:GetTrapMessage 

//描述:得到TRAP包中的信息 

//---------------------------------- 

SNMPAPI_STATUS GetTrapMessage(HSNMP_SESSION hSnmpSession, CTrapPduInfo *pTpiStore) 

{ 

ZeroMemory(pTpiStore, sizeof(CTrapPduInfo)); 

SNMPAPI_STATUS s; 

char szTemp[MAXOBJIDSTRSIZE]; 

smiOCTETS oct; 

CString strHead = "1.3.6.1.6.3.1.1.5."; 

// 

s = SnmpRecvMsg(hSnmpSession, &pTpiStore->hRcvAgentEntity, &pTpiStore->hRcvManagerEntity, 

&pTpiStore->hRcvViewContext, &pTpiStore->hRcvPdu); 

if(s == SNMPAPI_FAILURE) 

{ 

lMsg("Trap: SnmpRecvMsg时出错!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//得到发送消息代理的IP 

SnmpEntityToStr(pTpiStore->hRcvAgentEntity, 40, pTpiStore->szSourceIp); 

//得到共用体名称 

::SnmpContextToStr(pTpiStore->hRcvViewContext, &oct); 

strcpy(pTpiStore->szCommunity, (const char *)oct.ptr); 

pTpiStore->szCommunity[oct.len] = 0; 

//----------------------------------------------------------------//得到PDU 

s = SnmpGetPduData(pTpiStore->hRcvPdu, &pTpiStore->nPduType, &pTpiStore->nRequestId, 

&pTpiStore->nErrorStatus, &pTpiStore->nErrorIndex, &pTpiStore->hRcvVbl); 

if(s == SNMPAPI_FAILURE) 

{ 

lMsg("Trap: SnmpGetPduData时出错!!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//得到此PDU中包含的VBlist数量 

pTpiStore->nVblCount = SnmpCountVbl(pTpiStore->hRcvVbl); 

for (int i = 1; i <= pTpiStore->nVblCount; i++) 

{ 

//!!!!!!!!!!注意!!!!!!!!!!!!!!!// 

//SnmpGetVb中的index是从1开始! 

s = SnmpGetVb(pTpiStore->hRcvVbl, i, &pTpiStore->vblOid[i - 1], 

&pTpiStore->vblValue[i - 1]); 

if(s == SNMPAPI_FAILURE) 

{ 

lMsg("Trap: SnmpGetVb时出错!!"); 

GetSnmpErrorDescribe(hSnmpSession); 

return SNMPAPI_FAILURE; 

} 

//---------------------------------解出TRAP中的信息 

switch(pTpiStore->vblValue[i - 1].syntax) 

{ 

case SNMP_SYNTAX_TIMETICKS : 

pTpiStore->dwTimeTicks = pTpiStore->vblValue[i - 1].value.uNumber; 

break; 

case SNMP_SYNTAX_OID : 

//得到陷阱信息! 

memset(szTemp, 0, MAXOBJIDSTRSIZE); 

::SnmpOidToStr(&pTpiStore->vblValue[i - 1].value.oid, MAXOBJIDSTRSIZE, szTemp); 

if(i == 2) 

{ 

int nTempArray[40]; 

memset(nTempArray, 0, 80); 

char *pTemp = NULL, *pSource = szTemp; 

//-----------------------------提取其中的数字 

for(int j = 0; j < 40; j++) 

{ 

nTempArray[j] = strtoul(pSource, 

&pTemp, 10); 

if(pTemp == pSource) 

break; 

pSource = pTemp + 1; 

} 

if(nTempArray[j - 2] == 0) 

{ 

pTpiStore->genericTrap = 6; 

pTpiStore->specificTrap = nTempArray[j - 1]; 

} 

else 

pTpiStore->genericTrap = nTempArray[j - 1] - 1; 

} 

if(i == pTpiStore->nVblCount) 

{ 

//得到企业OID 

strcpy(pTpiStore->szEnterpriseOid, szTemp); 

} 

break; 

//------------------------------------ 

default: 

break; 

} 

pTpiStore->nUsefulVbCount = pTpiStore->nVblCount - 3; 

} 

//!!!注意:释放资源!!!! 

SnmpFreeDescriptor(SNMP_SYNTAX_OCTETS, &oct); 

return SNMPAPI_SUCCESS; 

} 



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -