1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| void SnmpUtilPrintAsnAny(AsnAny *any)
{
unsigned int i;
//printf("(%p)\n", any);
switch (any->asnType)
{
case ASN_NULL: printf("Null value\n"); return;
case ASN_INTEGER32: printf("Integer32 %d\n", any->asnValue.number); return;
case ASN_UNSIGNED32: printf("Unsigned32 %u\n", any->asnValue.unsigned32); return;
case ASN_COUNTER32: printf("Counter32 %u\n", any->asnValue.counter); return;
case ASN_GAUGE32: printf("Gauge32 %u\n", any->asnValue.gauge); return;
case ASN_TIMETICKS: printf("Timeticks %u\n", any->asnValue.ticks); return;
case ASN_COUNTER64:
{
printf("Counter64 %x%08x\n", (DWORD)(any->asnValue.counter64.QuadPart>>32),(DWORD)any->asnValue.counter64.QuadPart);
return;
}
case ASN_OCTETSTRING:
{
printf("String ");
for (i = 0; i < any->asnValue.string.length; i++)
{
//printf("%c", any->asnValue.string.stream[i]);
cout << any->asnValue.string.stream[i];
}
printf("\n");
break;
}
case ASN_IPADDRESS:
{
printf("IpAddress ");
if (any->asnValue.string.length < 4)
{
printf("Invalid\n");
return;
}
for (i = 0; i < 4; i++)
{
printf("%u", any->asnValue.string.stream[i]);
if (i < 3) printf(".");
}
printf("\n");
return;
}
case ASN_BITS:
{
printf("Bits ");
for (i = 0; i < any->asnValue.string.length; i++)
{
printf("0x%02x", any->asnValue.string.stream[i]);
if (i < any->asnValue.object.idLength - 1) printf(" ");
}
printf("\n");
return;
}
case ASN_OPAQUE:
{
printf("Opaque ");
for (i = 0; i < any->asnValue.string.length; i++)
{
printf("0x%02x", any->asnValue.string.stream[i]);
if (i < any->asnValue.object.idLength - 1) printf(" ");
}
printf("\n");
return;
}
case ASN_OBJECTIDENTIFIER:
{
printf("ObjectID ");
for (i = 0; i < any->asnValue.object.idLength; i++)
{
printf("%u", any->asnValue.object.ids[i]);
if (i < any->asnValue.object.idLength - 1) printf(".");
}
printf("\n");
return;
}
default:
{
printf("Invalid type %d\n", any->asnType);
return;
}
}
}
LireMultOid(char* id, char *resultat , LPSNMP_MGR_SESSION session)
{
SnmpVarBindList snmpVarList; //Structure variable Snmp
AsnInteger errorStatus,errorIndex;
unsigned int i;
snmpVarList.list = NULL;
snmpVarList.len = 1;
snmpVarList.list = (SnmpVarBind *)realloc(snmpVarList.list, sizeof(SnmpVarBind) *snmpVarList.len);
SnmpMgrStrToOid(id,&snmpVarList.list[0].name);
if (SnmpMgrRequest(session,SNMP_PDU_GET,&snmpVarList,&errorStatus,&errorIndex)!=0)
{
if (errorStatus==SNMP_ERRORSTATUS_NOERROR)
{
//printf("Lecture OK\n");
printf("[%s]\n",id); // AFFICHE L'OID CHERCHER
SnmpUtilPrintAsnAny(&snmpVarList.list[0].value); //AFFICHE LA VALEUR DE L'OID
AsnAny* any= &snmpVarList.list->value;
if (any->asnType==ASN_OCTETSTRING)
{
// cout<< string(reinterpret_cast<char*>(any->asnValue.string.stream), any->asnValue.string.length).c_str()<< endl;
}
}
}
return 0;
}
string ConvertionIntToString(int in)
{
ostringstream oss;
string z;
oss << in;
z = oss.str();
return z;
}
int main(int argc, char* argv[])
{
LPSNMP_MGR_SESSION session;
char resultat[MAX_PATH];
string oid;
string ID;
string z;
oid = ".1.3.6.1.4.1.11.2.14.11.5.1.7.1.3.1.1.3."; // PORTNAME
// oid = "1.3.6.1.4.1.11.2.14.11.5.1.9.4.2.1.2."; //hpswitchstatistic
session = SnmpMgrOpen("192.168.2.82","public",1000,3); //ouverture de session
if (session!=0)
{
printf("Connexion OK\n");
for(int j=25;j<=72;j++)
{
z = ConvertionIntToString(j);
ID = oid + z;
size_t size = ID.size() + 1; // créer le buffer pour copier la chaîne
char * buffer = new char[ size ];
strncpy( buffer, ID.c_str(), size ); // copier la chaîne
LireMultOid(buffer ,resultat,session); //id racine
}
SnmpMgrClose(session); //fermeture
}
else printf("Erreur SnmpMgrOpen\n");
system("PAUSE");
return 0;
} |
Partager