使用SNMP编辑(添加/删除/修改)ARP表
2010-07-20 20:44:55 来源:WEB开发网编辑ARP表条目
EditEntry函数可以添加、修改、删除ARP条目。
添加动态条目,就像:arp.exe 1.2.3.4 11-22-33-44-55-66
添加静态条目,就像:arp.exe –s 1.2.3.4 11-22-33-44-55-66
删除条目,就像:arp.exe –d 1.2.3.4
添加新条目,如果IP地址在ARP表中不存在则添加进去,如果已经存在则被更新。
删除条目,只需要提供NIC接口中的IP地址,MAC地址不需要提供。
函数有四个参数:4字节的IPAddress,6字节的MACAddress,条目类型(2:删除,
3:动态,4:静态)
AdapterIndex:NIC适配器编号
我使用4个OID设置ARP表条目:
OID[0] = "1.3.6.1.2.1.4.22.1.1", 设置接口编号
OID[1] = "1.3.6.1.2.1.4.22.1.2", 设置MAC地址
OID[3] = "1.3.6.1.2.1.4.22.1.3", 设置IP地址
OID[2] = "1.3.6.1.2.1.4.22.1.4", 设置条目类型 (静态或动态), 或删除
//-----------------------------------------------------------------------
// Function: EditEntry: Add/Modify/Remove ARP entry for specific NIC interface.
//
// Parameters:
// IPAddress Array of 4 BYTES, 4 octs of IP Address
// MACAddress Array of 4 BYTES, 6 octs of MAC Address
// Type Entry type (2:Remove, 3:Dynamic, 4:Static)
// AdapterIndex NIC Adapter index number
//
// Returns:
// TRUE if set successfully, FALSE otherwise.
//-----------------------------------------------------------------------
BOOL CARP::EditEntry(unsigned char IPAddress[4], unsigned char MACAddress[6], unsigned long Type, int AdapterIndex)
{
if (!bInitialized)
return 0;
SnmpVarBindList SVBList;
SnmpVarBind SVBVars[4];
UINT OID[4][10];
AsnInteger32 aiErrorStatus, aiErrorIndex;
BOOL bReturn = FALSE;
//-----------------------------------------------------------------------
// Fill array of 4 OIDs
//
// OID[0] : "1.3.6.1.2.1.4.22.1.1", ipNetToMediaIfIndex
// The interface on which this entry's equivalence is effective
//
// OID[1] : "1.3.6.1.2.1.4.22.1.2", ipNetToMediaPhysAddress
// The media-dependent 'physical' address
//
// OID[2] : "1.3.6.1.2.1.4.22.1.3", ipNetToMediaNetAddress
// The IpAddress corresponding to the media-dependent 'physical' address
//
// OID[3] : "1.3.6.1.2.1.4.22.1.4", ipNetToMediaType
// Entry type: 1:Other, 2:Invalid(Remove), 3:Dynamic, 4:Static
//-----------------------------------------------------------------------
for (int count=0; count<4; count++)
{
OID[count][0] = 1;
OID[count][1] = 3;
OID[count][2] = 6;
OID[count][3] = 1;
OID[count][4] = 2;
OID[count][5] = 1;
OID[count][6] = 4;
OID[count][7] = 22;
OID[count][8] = 1;
OID[count][9] = 1 + count;
switch(count)
{
case 0:
// OID[0] : "1.3.6.1.2.1.4.22.1.1", ipNetToMediaIfIndex
// The interface on which this entry's equivalence is effective
SVBVars[count].value.asnType = ASN_INTEGER;
SVBVars[count].value.asnValue.number = AdapterIndex;
break;
case 1:
// OID[1] : "1.3.6.1.2.1.4.22.1.2", ipNetToMediaPhysAddress
// The media-dependent 'physical' address
SVBVars[count].value.asnType = ASN_OCTETSTRING;
SVBVars[count].value.asnValue.string.stream = MACAddress;
SVBVars[count].value.asnValue.string.length = 6; // MAC Address length
SVBVars[count].value.asnValue.string.dynamic= FALSE;
break;
case 2:
// OID[2] : "1.3.6.1.2.1.4.22.1.3", ipNetToMediaNetAddress
// The IpAddress corresponding to the media-dependent 'physical' address
SVBVars[count].value.asnType = ASN_IPADDRESS;
SVBVars[count].value.asnValue.string.stream = IPAddress;
SVBVars[count].value.asnValue.string.length = 4; // IP Address length
SVBVars[count].value.asnValue.string.dynamic= FALSE;
break;
case 3:
// OID[3] : "1.3.6.1.2.1.4.22.1.4", ipNetToMediaType
// Entry type: 2:Remove, 3:Dynamic, 4:Static
SVBVars[count].value.asnType = ASN_INTEGER;
SVBVars[count].value.asnValue.number = Type;
break;
}
AsnObjectIdentifier AsnOID = {sizeof(OID[count])/sizeof(UINT), OID[count]};
SnmpUtilOidCpy(&SVBVars[count].name, &AsnOID);
}
SVBList.len = 4;
SVBList.list = SVBVars;
aiErrorStatus = 0;
aiErrorIndex = 0;
// Set information of entry (4 OIDs)
if (pfnSnmpExtensionQuery(SNMP_PDU_SET, &SVBList, &aiErrorStatus, &aiErrorIndex))
if (aiErrorStatus == SNMP_ERRORSTATUS_NOERROR)
bReturn = TRUE; // If success set bReturn = true
// Frees the memory allocated for the specified object identifiers
SnmpUtilOidFree(&SVBVars[3].name);
SnmpUtilOidFree(&SVBVars[2].name);
SnmpUtilOidFree(&SVBVars[1].name);
SnmpUtilOidFree(&SVBVars[0].name);
return bReturn; // TRUE if set successfully, FALSE otherwise.
}
更多精彩
赞助商链接