简单网络管理协议(SNMP)通过第三方API SNMP++ 提取系统时间信息,并根据时间信息计算系统开机时间与运行时间

本文介绍如何通过SNMP++库在VS2013环境下提取系统时间并计算开机时间与运行时间。通过修改OID,获取16进制时间数据并转换。示例代码展示了提取与转换过程,以及计算运行天数的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇博客中对SNMP++完成了环境配置,下面我们需要对感兴趣的进程信息进行提取。首先需要对例程进行学习。

本人编程能力基本为零,所以效率不高。希望大神提出宝贵意见。

/*_############################################################################
_##
_##  test_app.cpp
_##
_##  SNMP++ v3.3
_##  -----------------------------------------------
_##  Copyright (c) 2001-2013 Jochen Katz, Frank Fock
_##
_##  This software is based on SNMP++2.6 from Hewlett Packard:
_##
_##    Copyright (c) 1996
_##    Hewlett-Packard Company
_##
_##  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
_##  Permission to use, copy, modify, distribute and/or sell this software
_##  and/or its documentation is hereby granted without fee. User agrees
_##  to display the above copyright notice and this license notice in all
_##  copies of the software and any documentation of the software. User
_##  agrees to assume all liability for the use of the software;
_##  Hewlett-Packard and Jochen Katz make no representations about the
_##  suitability of this software for any purpose. It is provided
_##  "AS-IS" without warranty of any kind, either express or implied. User
_##  hereby grants a royalty-free license to any and all derivatives based
_##  upon this software code base.
_##
_##########################################################################*/
char test_app_cpp_version[] = "@(#) SNMP++ $Id: test_app.cpp 2471 2013-11-14 19:49:48Z fock $";
#include <libsnmp.h>
#include<iostream>
#include "snmp_pp/snmp_pp.h"
#include<Windows.h>

#ifdef WIN32
#define strcasecmp _stricmp
#endif

#ifdef SNMP_PP_NAMESPACE
using namespace Snmp_pp;
#endif

// default request oids
#define NUM_SYS_VBS	6
#define sysDescr	"1.3.6.1.2.1.1.1.0"
#define sysObjectID	"1.3.6.1.2.1.1.2.0"
#define sysUpTime	".1.3.6.1.2.1.25.1.2.0"//直接改这里
#define sysContact	"1.3.6.1.2.1.1.4.0"
#define sysName		"1.3.6.1.2.1.1.5.0"
#define sysLocation	"1.3.6.1.2.1.1.6.0"
//#define sysServices	"1.3.6.1.2.1.1.7.0" // not all agents support this...

// default notification oid
#define coldStart	"1.3.6.1.6.3.1.1.4.3.0.1"

int main(int argc, char **argv)
{
	int status;
	char *req_str = (char*) "get";
	//  char *dflt_req_oid = (char*) sysDescr;
	char *dflt_trp_oid = (char*)coldStart;
	char *genAddrStr = (char*) "127.0.0.1";		  // localhost
	char *oid_str = (char*)NULL;

	if (argc > 1) genAddrStr = argv[1];
	if (argc > 2) req_str = argv[2];
	if (argc > 3) oid_str = argv[3];

	Snmp::socket_startup();  // Initialize socket subsystem

	IpAddress ipAddr(genAddrStr);
	if (!ipAddr.valid()) {
		cout << "Invalid destination: " << genAddrStr << endl;
		return(1);
	}

	// bind to any port and use IPv6 if needed
	Snmp snmp(status, 0, (ipAddr.get_ip_version() == Address::ver
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值