gosnmp walk windows已安装软件软件名称中文和安装时间正确显示方法

// Copyright 2012 The GoSNMP Authors. All rights reserved.  Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.

// This program demonstrates BulkWalk.
package main

import (
	"fmt"
	"os"
	"strings"
	"time"

	"github.com/gosnmp/gosnmp"

	"golang.org/x/text/encoding/simplifiedchinese"
)

func snmpTimeString(c []byte) string {
	year := (int(c[0]) << 8) | int(c[1])
	return fmt.Sprintf("%d-%d-%d %02d:%02d:%02d.%d", year, c[2], c[3], c[4], c[5], c[6], c[7])
}

func main() {

	gosnmp.Default.Target = "127.0.0.1"
	gosnmp.Default.Community = "public"
	oid := ".1.3.6.1.2.1.25.6.3.1"
	gosnmp.Default.Timeout = time.Duration(10 * time.Second) // Timeout better suited to walking
	err := gosnmp.Default.Connect()
	if err != nil {
		fmt.Printf("Connect err: %v\n", err)
		os.Exit(1)
	}
	defer gosnmp.Default.Conn.Close()

	err = gosnmp.Default.BulkWalk(oid, printValue)
	if err != nil {
		fmt.Printf("Walk Error: %v\n", err)
		os.Exit(1)
	}
}

func printValue(pdu gosnmp.SnmpPDU) error {

	if strings.Contains(pdu.Name, ".1.3.6.1.2.1.25.6.3.1.2") {
		fmt.Printf("%s = ", pdu.Name)
		b := pdu.Value.([]byte)
		utf8Data2, _ := simplifiedchinese.GBK.NewDecoder().Bytes(b)
		fmt.Printf("STRING: %s\n", utf8Data2)
	}
	if strings.Contains(pdu.Name, ".1.3.6.1.2.1.25.6.3.1.4") {
		fmt.Printf("%s = ", pdu.Name)
		fmt.Printf("TYPE %d: %d\n", pdu.Type, gosnmp.ToBigInt(pdu.Value))
	}
	if strings.Contains(pdu.Name, ".1.3.6.1.2.1.25.6.3.1.5") {
		fmt.Printf("%s = ", pdu.Name)
		b := pdu.Value.([]byte)
		fmt.Printf("STRING: %s\n", snmpTimeString(b))
	}

	return nil
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值