3 Star 0 Fork 0

Gitee 极速下载 / gosnmp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/soniah/gosnmp
克隆/下载
example2.go 1.73 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2012-2014 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.
package main
import (
"fmt"
"log"
"os"
"strconv"
"time"
g "github.com/soniah/gosnmp"
)
func main() {
// get Target and Port from environment
envTarget := os.Getenv("GOSNMP_TARGET")
envPort := os.Getenv("GOSNMP_PORT")
if len(envTarget) <= 0 {
log.Fatalf("environment variable not set: GOSNMP_TARGET")
}
if len(envPort) <= 0 {
log.Fatalf("environment variable not set: GOSNMP_PORT")
}
port, _ := strconv.ParseUint(envPort, 10, 16)
// Build our own GoSNMP struct, rather than using g.Default.
// Do verbose logging of packets.
params := &g.GoSNMP{
Target: envTarget,
Port: uint16(port),
Community: "public",
Version: g.Version2c,
Timeout: time.Duration(2) * time.Second,
Logger: log.New(os.Stdout, "", 0),
}
err := params.Connect()
if err != nil {
log.Fatalf("Connect() err: %v", err)
}
defer params.Conn.Close()
oids := []string{"1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.7.0"}
result, err2 := params.Get(oids) // Get() accepts up to g.MAX_OIDS
if err2 != nil {
log.Fatalf("Get() err: %v", err2)
}
for i, variable := range result.Variables {
fmt.Printf("%d: oid: %s ", i, variable.Name)
// the Value of each variable returned by Get() implements
// interface{}. You could do a type switch...
switch variable.Type {
case g.OctetString:
fmt.Printf("string: %s\n", string(variable.Value.([]byte)))
default:
// ... or often you're just interested in numeric values.
// ToBigInt() will return the Value as a BigInt, for plugging
// into your calculations.
fmt.Printf("number: %d\n", g.ToBigInt(variable.Value))
}
}
}
1
https://gitee.com/mirrors/gosnmp.git
git@gitee.com:mirrors/gosnmp.git
mirrors
gosnmp
gosnmp
v1.17.0

搜索帮助