net-snmp获取各个类型的数据
2017-05-30 10:19
197 查看
原文地址http://blog.chinaunix.net/uid-17260303-id-3042593.html
/* manipuate the information ourselves */
for(vars = response->variables; vars; vars = vars->next_variable) //pdu is a
snmp_pdu style variable
{
printf("var type is %d\n", vars->type);
if (vars->type == ASN_OCTET_STR) //vars is a
variable_list style variable
{
//判断是字符串还是Hex-STRING
int hex = 0;
int x;
u_char * cp;
int allow_realloc = 1;
u_char *buf = NULL;
size_t buf_len = 256, out_len = 0;
for (cp = vars->val.string, x = 0; x < (int) vars->val_len; x++, cp++)
{
if (!isprint(*cp) && !isspace(*cp))
{
hex = 1;
}
}
if(!hex) //字符串
{
char *sp = (char *)malloc(1 + vars->val_len);
memcpy(sp, vars->val.string, vars->val_len); //netsnmp_vardata is a
netsnmp_vardata style variable
sp[vars->val_len] = '\0';
printf("value #%d is a string: %s\n", count++, sp);
free(sp);
}
else //Hex-STRING
{
buf = (u_char *) calloc(buf_len, 1);
snmp_cstrcat(&buf, &buf_len, &out_len, allow_realloc, "");
sprint_realloc_hexstring(&buf, &buf_len, &out_len, allow_realloc,vars->val.string, vars->val_len);
printf("value #%d is a hex-string: %s\n", count++, buf);
free(buf);
}
}
else if(vars->type == ASN_TIMETICKS)
{
long timetick = *vars->val.integer;
printf("value #%d is a timetick: %d\n", count++, timetick);
}
else if(vars->type == ASN_OBJECT_ID)
{
printf("value #%d is a oid: ", count++);
for(i=0; i<vars->name_length; i++)
{
if(*(vars->name_loc+i) == 0)
break;
printf(".%d", *(vars->name_loc+i));
}
printf(" and value is ");
for(i=0; i<(vars->val_len/sizeof(int)); i++)
{
printf(".%d", *(vars->val.objid+i));
}
printf("\n");
}
else if(vars->type == ASN_INTEGER)
{
printf("value #%d is a integer: %d\n", count++, *(vars->val.integer));
}
else if(vars->type == ASN_COUNTER)
{
printf("value #%d is a count: %u\n", count++, (unsigned int)(*vars->val.integer & 0xffffffff));
}
else if(vars->type == ASN_GAUGE)
{
printf("value #%d is a gauge: %u\n", count++, *(vars->val.integer));
}
else if(vars->type == ASN_IPADDRESS)
{
u_char *ip = vars->val.string;
printf("value #%d is a ipaddress: %d.%d.%d.%d\n", count++, ip[0], ip[1], ip[2], ip[3]);
}
else if(vars->type == ASN_NULL)
{
printf("value #%d is a null: \n", count++);
}
else
printf("value #%d is NOT a string! Ack!\n", count++);
}
}
/* manipuate the information ourselves */
for(vars = response->variables; vars; vars = vars->next_variable) //pdu is a
snmp_pdu style variable
{
printf("var type is %d\n", vars->type);
if (vars->type == ASN_OCTET_STR) //vars is a
variable_list style variable
{
//判断是字符串还是Hex-STRING
int hex = 0;
int x;
u_char * cp;
int allow_realloc = 1;
u_char *buf = NULL;
size_t buf_len = 256, out_len = 0;
for (cp = vars->val.string, x = 0; x < (int) vars->val_len; x++, cp++)
{
if (!isprint(*cp) && !isspace(*cp))
{
hex = 1;
}
}
if(!hex) //字符串
{
char *sp = (char *)malloc(1 + vars->val_len);
memcpy(sp, vars->val.string, vars->val_len); //netsnmp_vardata is a
netsnmp_vardata style variable
sp[vars->val_len] = '\0';
printf("value #%d is a string: %s\n", count++, sp);
free(sp);
}
else //Hex-STRING
{
buf = (u_char *) calloc(buf_len, 1);
snmp_cstrcat(&buf, &buf_len, &out_len, allow_realloc, "");
sprint_realloc_hexstring(&buf, &buf_len, &out_len, allow_realloc,vars->val.string, vars->val_len);
printf("value #%d is a hex-string: %s\n", count++, buf);
free(buf);
}
}
else if(vars->type == ASN_TIMETICKS)
{
long timetick = *vars->val.integer;
printf("value #%d is a timetick: %d\n", count++, timetick);
}
else if(vars->type == ASN_OBJECT_ID)
{
printf("value #%d is a oid: ", count++);
for(i=0; i<vars->name_length; i++)
{
if(*(vars->name_loc+i) == 0)
break;
printf(".%d", *(vars->name_loc+i));
}
printf(" and value is ");
for(i=0; i<(vars->val_len/sizeof(int)); i++)
{
printf(".%d", *(vars->val.objid+i));
}
printf("\n");
}
else if(vars->type == ASN_INTEGER)
{
printf("value #%d is a integer: %d\n", count++, *(vars->val.integer));
}
else if(vars->type == ASN_COUNTER)
{
printf("value #%d is a count: %u\n", count++, (unsigned int)(*vars->val.integer & 0xffffffff));
}
else if(vars->type == ASN_GAUGE)
{
printf("value #%d is a gauge: %u\n", count++, *(vars->val.integer));
}
else if(vars->type == ASN_IPADDRESS)
{
u_char *ip = vars->val.string;
printf("value #%d is a ipaddress: %d.%d.%d.%d\n", count++, ip[0], ip[1], ip[2], ip[3]);
}
else if(vars->type == ASN_NULL)
{
printf("value #%d is a null: \n", count++);
}
else
printf("value #%d is NOT a string! Ack!\n", count++);
}
}
相关文章推荐
- net-snmp的dateandtime数据类型
- 求教C#使用(SnmpSharpNet.dll)Snmp协议,如何获取整个局域网段内的IP设备类型(例PC、路由器等)MAC和主机名?
- vb.net 获取数据表 字段名和数据类型
- .net Response.ContentType获取返回类型及数据对应的详细列表
- Sql与Asp.Net数据类型对应(引用MsDN)
- The NOTE of learning ASP.NET [9] .NET的基本数据类型及其常用方法
- ASP.NET Web Forms 4.5的新特性(一):强类型数据控件和Bundling
- ASP.NET C# 向 ACCESS 插入日期型数据 标准表达式中数据类型不匹配
- ADO.NET中的数据访问--强类型
- ASP.NET AJAX客户端编程之旅(三)——让JavaScript和C#无障碍沟通:数据类型自动转换&序列化
- 如何获取指定表的列和数据类型
- [导入]ASP.NET AJAX客户端编程之旅(三)——让JavaScript和C#无障碍沟通:数据类型自动转换&序列化
- 获取变量数据类型的程序
- 获取MySql每一列的数据类型和长度默认值等信息
- dojo从asp.net中获取json数据
- [Java]nio( 4000 二)——转换数据(编码问题)和获取基本数据类型
- MSSQL获取指定表的列名信息,描述,数据类型,长度
- JQuery以POST方法从ASP.NET服务器获取Json数据完整示例 .
- Asp.Net 上传大文件专题(3)--从请求流中获取数据并保存为文件[上]
- Asp.Net 上传大文件专题(3)--从请求流中获取数据并保存为文件[上]