3 Star 0 Fork 0

Gitee 极速下载 / gosnmp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/soniah/gosnmp
克隆/下载
walkexample.go 1.57 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.
// This program demonstrates BulkWalk.
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"time"
"github.com/soniah/gosnmp"
)
func main() {
flag.Usage = func() {
fmt.Printf("Usage:\n")
fmt.Printf(" %s [-community=<community>] host [oid]\n", filepath.Base(os.Args[0]))
fmt.Printf(" host - the host to walk/scan\n")
fmt.Printf(" oid - the MIB/Oid defining a subtree of values\n\n")
flag.PrintDefaults()
}
var community string
flag.StringVar(&community, "community", "public", "the community string for device")
flag.Parse()
if len(flag.Args()) < 1 {
flag.Usage()
os.Exit(1)
}
target := flag.Args()[0]
var oid string
if len(flag.Args()) > 1 {
oid = flag.Args()[1]
}
gosnmp.Default.Target = target
gosnmp.Default.Community = community
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 {
fmt.Printf("%s = ", pdu.Name)
switch pdu.Type {
case gosnmp.OctetString:
b := pdu.Value.([]byte)
fmt.Printf("STRING: %s\n", string(b))
default:
fmt.Printf("TYPE %d: %d\n", pdu.Type, gosnmp.ToBigInt(pdu.Value))
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/gosnmp.git
git@gitee.com:mirrors/gosnmp.git
mirrors
gosnmp
gosnmp
v1.15.0

搜索帮助

344bd9b3 5694891 D2dac590 5694891