代码拉取完成,页面将自动刷新
// 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))
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。